xref: /illumos-gate/usr/src/cmd/echo/echo.c (revision c3377ee9a5b3bff76dbf51347a8de3d215eb6cca)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved  	*/
27 
28 /*
29  * Copyright (c) 2018, Joyent, Inc.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <wchar.h>
35 #include <string.h>
36 #include <locale.h>
37 
38 int
39 main(int argc, char *argv[])
40 {
41 
42 	register char	*cp;
43 	register int	i, wd;
44 	int	j;
45 	wchar_t		wc;
46 	int		b_len;
47 	char		*ep;
48 
49 	(void) setlocale(LC_ALL, "");
50 
51 	if (--argc == 0) {
52 		(void) putchar('\n');
53 		if (fflush(stdout) != 0)
54 			return (1);
55 		return (0);
56 	}
57 
58 	for (i = 1; i <= argc; i++) {
59 		for (cp = argv[i], ep = cp + (int)strlen(cp);
60 		    cp < ep; cp += b_len) {
61 			if ((b_len = mbtowc(&wc, cp, MB_CUR_MAX)) <= 0) {
62 				(void) putchar(*cp);
63 				b_len = 1;
64 				continue;
65 			}
66 
67 			if (wc != '\\') {
68 				(void) putwchar(wc);
69 				continue;
70 			}
71 
72 			cp += b_len;
73 			b_len = 1;
74 			switch (*cp) {
75 				case 'a':	/* alert - XCU4 */
76 					(void) putchar('\a');
77 					continue;
78 
79 				case 'b':
80 					(void) putchar('\b');
81 					continue;
82 
83 				case 'c':
84 					if (fflush(stdout) != 0)
85 						return (1);
86 					return (0);
87 
88 				case 'f':
89 					(void) putchar('\f');
90 					continue;
91 
92 				case 'n':
93 					(void) putchar('\n');
94 					continue;
95 
96 				case 'r':
97 					(void) putchar('\r');
98 					continue;
99 
100 				case 't':
101 					(void) putchar('\t');
102 					continue;
103 
104 				case 'v':
105 					(void) putchar('\v');
106 					continue;
107 
108 				case '\\':
109 					(void) putchar('\\');
110 					continue;
111 				case '0':
112 					j = wd = 0;
113 					while ((*++cp >= '0' && *cp <= '7') &&
114 					    j++ < 3) {
115 						wd <<= 3;
116 						wd |= (*cp - '0');
117 					}
118 					(void) putchar(wd);
119 					--cp;
120 					continue;
121 
122 				default:
123 					cp--;
124 					(void) putchar(*cp);
125 			}
126 		}
127 		(void) putchar(i == argc? '\n': ' ');
128 		if (fflush(stdout) != 0)
129 			return (1);
130 	}
131 	return (0);
132 }
133