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