xref: /illumos-gate/usr/src/cmd/echo/echo.c (revision defc4c8acfa01dba1ef3c13ca0cafccfcede51c0)
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 #include <stdio.h>
30 #include <stdlib.h>
31 #include <wchar.h>
32 #include <string.h>
33 #include <locale.h>
34 
35 int
36 main(int argc, char *argv[])
37 {
38 
39 	register char	*cp;
40 	register int	i, wd;
41 	int	j;
42 	wchar_t		wc;
43 	int		b_len;
44 	char		*ep;
45 
46 	(void) setlocale(LC_ALL, "");
47 
48 	if (--argc == 0) {
49 		(void) putchar('\n');
50 		if (fflush(stdout) != 0)
51 			return (1);
52 		return (0);
53 	}
54 
55 	for (i = 1; i <= argc; i++) {
56 		for (cp = argv[i], ep = cp + (int)strlen(cp);
57 		    cp < ep; cp += b_len) {
58 		if ((b_len = mbtowc(&wc, cp, MB_CUR_MAX)) <= 0) {
59 			(void) putchar(*cp);
60 			b_len = 1;
61 			continue;
62 		}
63 
64 		if (wc != '\\') {
65 			(void) putwchar(wc);
66 			continue;
67 		}
68 
69 			cp += b_len;
70 			b_len = 1;
71 			switch (*cp) {
72 				case 'a':	/* alert - XCU4 */
73 					(void) putchar('\a');
74 					continue;
75 
76 				case 'b':
77 					(void) putchar('\b');
78 					continue;
79 
80 				case 'c':
81 					if (fflush(stdout) != 0)
82 						return (1);
83 					return (0);
84 
85 				case 'f':
86 					(void) putchar('\f');
87 					continue;
88 
89 				case 'n':
90 					(void) putchar('\n');
91 					continue;
92 
93 				case 'r':
94 					(void) putchar('\r');
95 					continue;
96 
97 				case 't':
98 					(void) putchar('\t');
99 					continue;
100 
101 				case 'v':
102 					(void) putchar('\v');
103 					continue;
104 
105 				case '\\':
106 					(void) putchar('\\');
107 					continue;
108 				case '0':
109 					j = wd = 0;
110 					while ((*++cp >= '0' && *cp <= '7') &&
111 					    j++ < 3) {
112 						wd <<= 3;
113 						wd |= (*cp - '0');
114 					}
115 					(void) putchar(wd);
116 					--cp;
117 					continue;
118 
119 				default:
120 					cp--;
121 					(void) putchar(*cp);
122 			}
123 		}
124 		(void) putchar(i == argc? '\n': ' ');
125 		if (fflush(stdout) != 0)
126 			return (1);
127 	}
128 	return (0);
129 }
130