xref: /titanic_41/usr/src/cmd/sh/echo.c (revision 236cbc79a831053e1ce0340d0fab0cd452fbcd61)
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 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 
31 /*
32  *	UNIX shell
33  */
34 #include	"defs.h"
35 
36 #define	exit(a)	flushb(); return (a)
37 
38 extern int exitval;
39 
40 int
41 echo(int argc, unsigned char **argv)
42 {
43 	unsigned char	*cp;
44 	int	i, wd;
45 	int	nflg = 0;
46 	int	j;
47 	int	len;
48 	wchar_t	wc;
49 
50 #ifdef	_iBCS2		/* SCO compatibility support */
51 	struct namnod   *sysv3;
52 	int	do_sysv3 = 0;
53 
54 	sysv3 = findnam("SYSV3");
55 	if (sysv3 && (sysv3->namflg & (N_EXPORT | N_ENVNAM)))
56 		do_sysv3 = 1;
57 
58 	/* Do the -n parsing if sysv3 is set or if ucb_builtsin is set */
59 	if (ucb_builtins && !do_sysv3) {
60 #else
61 	if (ucb_builtins) {
62 #endif /* _iBCS2 */
63 
64 		nflg = 0;
65 		if (argc > 1 && argv[1][0] == '-' &&
66 		    argv[1][1] == 'n' && !argv[1][2]) {
67 			nflg++;
68 			argc--;
69 			argv++;
70 		}
71 
72 		for (i = 1; i < argc; i++) {
73 			sigchk();
74 
75 			for (cp = argv[i]; *cp; cp++) {
76 				prc_buff(*cp);
77 			}
78 
79 			if (i < argc-1)
80 				prc_buff(' ');
81 		}
82 
83 		if (nflg == 0)
84 			prc_buff('\n');
85 		exit(0);
86 	} else {
87 		if (--argc == 0) {
88 			prc_buff('\n');
89 			exit(0);
90 		}
91 #ifdef  _iBCS2
92 		if (do_sysv3) {
93 			if (argc > 1 && argv[1][0] == '-' &&
94 			    argv[1][1] == 'n' && !argv[1][2]) {
95 				nflg++;
96 				/* Step past the -n */
97 				argc--;
98 				argv++;
99 			}
100 		}
101 #endif /* _iBCS2 */
102 
103 		for (i = 1; i <= argc; i++) {
104 			sigchk();
105 			for (cp = argv[i]; *cp; cp++) {
106 				if ((len = mbtowc(&wc, (char *)cp,
107 				    MB_LEN_MAX)) <= 0) {
108 					prc_buff(*cp);
109 					continue;
110 				}
111 
112 				if (wc == '\\') {
113 					switch (*++cp) {
114 					case 'b':
115 						prc_buff('\b');
116 						continue;
117 					case 'c':
118 						exit(0);
119 
120 					case 'f':
121 						prc_buff('\f');
122 						continue;
123 
124 					case 'n':
125 						prc_buff('\n');
126 						continue;
127 
128 					case 'r':
129 						prc_buff('\r');
130 						continue;
131 
132 					case 't':
133 						prc_buff('\t');
134 						continue;
135 
136 					case 'v':
137 						prc_buff('\v');
138 						continue;
139 
140 					case '\\':
141 						prc_buff('\\');
142 						continue;
143 					case '0':
144 						j = wd = 0;
145 						while ((*++cp >= '0' &&
146 						    *cp <= '7') && j++ < 3) {
147 							wd <<= 3;
148 							wd |= (*cp - '0');
149 						}
150 						prc_buff(wd);
151 						--cp;
152 						continue;
153 
154 					default:
155 						cp--;
156 					}
157 					prc_buff(*cp);
158 					continue;
159 				} else {
160 					for (; len > 0; len--)
161 						prc_buff(*cp++);
162 					cp--;
163 					continue;
164 				}
165 			}
166 #ifdef	_iBCS2
167 			/* Don't do if don't want newlines & out of args */
168 			if (!(nflg && i == argc))
169 #endif /* _iBCS2 */
170 				prc_buff(i == argc? '\n': ' ');
171 		}
172 		exit(0);
173 	}
174 }
175