coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/echo.c (43 lines, 1.3 KiB) -rw-r--r-- file download

0b0be97 Jamozed 2020-07-09 03:01:28
0
// echo.c, version 1.1.0
ba7d1e0 Jamozed 2020-06-26 16:58:05
1
// OMKOV coreutils implementation of POSIX echo
ba7d1e0 Jamozed 2020-06-26 16:58:05
2
// Copyright (C) 2020, Jakob Wakeling
e2140ec Jamozed 2022-03-06 15:27:45
3
// MIT Licence
ba7d1e0 Jamozed 2020-06-26 16:58:05
4
ba7d1e0 Jamozed 2020-06-26 16:58:05
5
#include <stdio.h>
0b0be97 Jamozed 2020-07-09 03:01:28
6
#include <stdlib.h>
0b0be97 Jamozed 2020-07-09 03:01:28
7
0b0be97 Jamozed 2020-07-09 03:01:28
8
static inline void echo(const char *s);
ba7d1e0 Jamozed 2020-06-26 16:58:05
9
c3d67f0 Jamozed 2020-07-06 22:26:36
10
int main(int ac, char *av[]) { (void)ac;
0b0be97 Jamozed 2020-07-09 03:01:28
11
	register char **p = &av[1]; if (*p) { echo(*p);
0b0be97 Jamozed 2020-07-09 03:01:28
12
		for (++p; *p; ++p) { fputc(' ', stdout); echo(*p); }
ba7d1e0 Jamozed 2020-06-26 16:58:05
13
	} fputc('\n', stdout); return 0;
ba7d1e0 Jamozed 2020-06-26 16:58:05
14
}
0b0be97 Jamozed 2020-07-09 03:01:28
15
e65742c Jamozed 2020-09-16 19:36:37
16
/*
e65742c Jamozed 2020-09-16 19:36:37
17
	Echo a string to stdout character by character.
e65742c Jamozed 2020-09-16 19:36:37
18
	Escape sequences are processed according to XSI.
e65742c Jamozed 2020-09-16 19:36:37
19
*/
0b0be97 Jamozed 2020-07-09 03:01:28
20
static inline void echo(const char *s) {
0b0be97 Jamozed 2020-07-09 03:01:28
21
	for (const char *p = s; *p; ++p) {
0b0be97 Jamozed 2020-07-09 03:01:28
22
		if (*p == '\\' && p[1]) switch (*++p) {
0b0be97 Jamozed 2020-07-09 03:01:28
23
		case 'a': { fputc('\a', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
24
		case 'b': { fputc('\b', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
25
		case 'c': { exit(0); }
0b0be97 Jamozed 2020-07-09 03:01:28
26
		case 'f': { fputc('\f', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
27
		case 'n': { fputc('\n', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
28
		case 'r': { fputc('\r', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
29
		case 't': { fputc('\t', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
30
		case 'v': { fputc('\v', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
31
		case '\\': { fputc('\\', stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
32
		case '0': {
0b0be97 Jamozed 2020-07-09 03:01:28
33
			register int n = 0, d = 0;
0b0be97 Jamozed 2020-07-09 03:01:28
34
			for (++p; (*p >= '0' && *p <= '7') && d++ != 3; ++p) {
0b0be97 Jamozed 2020-07-09 03:01:28
35
				n <<= 3; n |= (*p - '0');
0b0be97 Jamozed 2020-07-09 03:01:28
36
			} fputc(n, stdout); --p; continue;
0b0be97 Jamozed 2020-07-09 03:01:28
37
		}
0b0be97 Jamozed 2020-07-09 03:01:28
38
		default: { fputc('\\', stdout); fputc(*p, stdout); }
0b0be97 Jamozed 2020-07-09 03:01:28
39
		} else { fputc(*p, stdout); continue; }
0b0be97 Jamozed 2020-07-09 03:01:28
40
	} return;
0b0be97 Jamozed 2020-07-09 03:01:28
41
}
42