yes.c (948B)
- // utils-std: Collection of commonly available Unix tools
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _POSIX_C_SOURCE 200809L
- #include <assert.h>
- #include <stdbool.h>
- #include <stdio.h> // fwrite, perror, ferror
- #include <string.h> // strlen
- int
- main(int argc, char *argv[])
- {
- size_t arg_len = 0;
- assert(argc >= 1);
- if(argc == 1)
- {
- argv[0][0] = 'y';
- argv[0][1] = '\n';
- argv[0][2] = '\0';
- arg_len = 2;
- }
- else
- {
- argv++;
- argc--;
- for(int i = 0; i < argc; i++)
- {
- size_t len = strlen(argv[i]);
- argv[i][len] = ' ';
- arg_len += len + 1; // str + space
- }
- if(arg_len == 0)
- {
- argv[0][0] = '\n';
- argv[0][1] = '\0';
- arg_len = 1;
- }
- else
- argv[0][arg_len - 1] = '\n';
- }
- while(fwrite(*argv, arg_len, 1, stdout) == 1)
- ;
- if(ferror(stdout))
- {
- perror("yes: error: Failed writing");
- return 1;
- }
- return 0;
- }