echo.c (2799B)
- // 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 <stdbool.h>
- #include <stdio.h> // perror
- #include <stdlib.h> // malloc
- #include <string.h> // strlen
- #include <unistd.h> // write
- int
- main(int argc, char *argv[])
- {
- size_t arg_len = 0;
- bool opt_n = false, opt_e = false;
- argc--;
- argv++;
- for(; argc > 0;)
- {
- if(argv[0][0] != '-') break;
- /* '-' and '--' needs to be passed as-is so no argc--,argv++ */
- if(argv[0][1] == '\0' || (argv[0][1] == '-' && argv[0][2] == '\0')) break;
- for(int i = 1; argv[0][i] != '\0'; i++)
- {
- switch(argv[0][i])
- {
- case 'n':
- opt_n = true;
- break;
- case 'e':
- opt_e = true;
- break;
- case 'E':
- opt_e = false;
- break;
- default:
- fprintf(stderr, "echo: warning: unknown option '-%c'\n", argv[0][i]);
- break;
- }
- }
- argc--;
- argv++;
- }
- 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)
- {
- if(opt_n) return 0;
- if(write(1, "\n", 1) < 1)
- {
- perror("echo: error: Failed writing");
- return 1;
- }
- return 0;
- }
- else
- argv[0][arg_len - 1] = '\n';
- if(opt_n) arg_len--; // no newline
- char *d = *argv;
- size_t d_len = arg_len;
- char *newd = NULL;
- if(opt_e)
- {
- newd = malloc(arg_len);
- if(!newd)
- {
- fprintf(stderr, "echo: error: Failed to allocate for a copy of the strings\n");
- return 1;
- }
- int di = 0;
- for(size_t argi = 0; argi < arg_len; argi++)
- {
- if(d[argi] != '\\')
- {
- newd[di++] = d[argi];
- continue;
- }
- switch(d[++argi])
- {
- case 'a':
- newd[di++] = '\a';
- break;
- case 'b':
- newd[di++] = '\b';
- break;
- case 'c':
- newd[di++] = '\0';
- goto p_break;
- case 'f':
- newd[di++] = '\f';
- break;
- case 'n':
- newd[di++] = '\n';
- break;
- case 'r':
- newd[di++] = '\r';
- break;
- case 't':
- newd[di++] = '\t';
- break;
- case 'v':
- newd[di++] = '\v';
- break;
- case '\\':
- newd[di++] = '\\';
- break;
- case '0': /* \0 \0n \0nn \0nnn */
- {
- int nl = 1; // skip leading 0
- int num = 0;
- for(; nl < 4; nl++)
- {
- if(d[argi + nl] >= '0' && d[argi + nl] <= '7')
- {
- num = (num * 8) + (d[argi + nl] - '0');
- }
- else
- break;
- }
- newd[di++] = num;
- argi += (nl - 1);
- break;
- }
- default:
- newd[di++] = d[argi];
- break;
- }
- }
- newd[di] = '\0';
- p_break:
- d = newd;
- d_len = di;
- }
- ssize_t nwrite = write(1, d, d_len);
- if(nwrite < (ssize_t)d_len)
- {
- perror("echo: error: Failed writing");
- if(opt_e) free(newd);
- return 1;
- }
- if(opt_e) free(newd);
- return 0;
- }