xref: /illumos-gate/usr/src/cmd/sh/echo.c (revision 6a634c9d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58944aeb2SAjaykumar Venkatesulu  * Common Development and Distribution License (the "License").
68944aeb2SAjaykumar Venkatesulu  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21965005c8Schin 
22965005c8Schin /*
238944aeb2SAjaykumar Venkatesulu  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24965005c8Schin  */
25965005c8Schin 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	UNIX shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate #include	"defs.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	exit(a)	flushb(); return (a)
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate extern int exitval;
397c478bd9Sstevel@tonic-gate 
40965005c8Schin int
echo(int argc,unsigned char ** argv)41965005c8Schin echo(int argc, unsigned char **argv)
427c478bd9Sstevel@tonic-gate {
43965005c8Schin 	unsigned char	*cp;
44965005c8Schin 	int	i, wd;
457c478bd9Sstevel@tonic-gate 	int	nflg = 0;
467c478bd9Sstevel@tonic-gate 	int	j;
477c478bd9Sstevel@tonic-gate 	int	len;
487c478bd9Sstevel@tonic-gate 	wchar_t	wc;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (ucb_builtins) {
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 		nflg = 0;
538944aeb2SAjaykumar Venkatesulu 		if (argc > 1 && argv[1][0] == '-' &&
548944aeb2SAjaykumar Venkatesulu 		    argv[1][1] == 'n' && !argv[1][2]) {
557c478bd9Sstevel@tonic-gate 			nflg++;
567c478bd9Sstevel@tonic-gate 			argc--;
577c478bd9Sstevel@tonic-gate 			argv++;
587c478bd9Sstevel@tonic-gate 		}
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 		for (i = 1; i < argc; i++) {
617c478bd9Sstevel@tonic-gate 			sigchk();
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
647c478bd9Sstevel@tonic-gate 				prc_buff(*cp);
657c478bd9Sstevel@tonic-gate 			}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 			if (i < argc-1)
687c478bd9Sstevel@tonic-gate 				prc_buff(' ');
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		if (nflg == 0)
727c478bd9Sstevel@tonic-gate 			prc_buff('\n');
737c478bd9Sstevel@tonic-gate 		exit(0);
747c478bd9Sstevel@tonic-gate 	} else {
757c478bd9Sstevel@tonic-gate 		if (--argc == 0) {
767c478bd9Sstevel@tonic-gate 			prc_buff('\n');
777c478bd9Sstevel@tonic-gate 			exit(0);
787c478bd9Sstevel@tonic-gate 		}
797c478bd9Sstevel@tonic-gate 
80965005c8Schin 		for (i = 1; i <= argc; i++) {
817c478bd9Sstevel@tonic-gate 			sigchk();
827c478bd9Sstevel@tonic-gate 			for (cp = argv[i]; *cp; cp++) {
837c478bd9Sstevel@tonic-gate 				if ((len = mbtowc(&wc, (char *)cp,
848944aeb2SAjaykumar Venkatesulu 				    MB_LEN_MAX)) <= 0) {
857c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
867c478bd9Sstevel@tonic-gate 					continue;
877c478bd9Sstevel@tonic-gate 				}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 				if (wc == '\\') {
907c478bd9Sstevel@tonic-gate 					switch (*++cp) {
917c478bd9Sstevel@tonic-gate 					case 'b':
927c478bd9Sstevel@tonic-gate 						prc_buff('\b');
937c478bd9Sstevel@tonic-gate 						continue;
947c478bd9Sstevel@tonic-gate 					case 'c':
957c478bd9Sstevel@tonic-gate 						exit(0);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 					case 'f':
987c478bd9Sstevel@tonic-gate 						prc_buff('\f');
997c478bd9Sstevel@tonic-gate 						continue;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 					case 'n':
1027c478bd9Sstevel@tonic-gate 						prc_buff('\n');
1037c478bd9Sstevel@tonic-gate 						continue;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 					case 'r':
1067c478bd9Sstevel@tonic-gate 						prc_buff('\r');
1077c478bd9Sstevel@tonic-gate 						continue;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 					case 't':
1107c478bd9Sstevel@tonic-gate 						prc_buff('\t');
1117c478bd9Sstevel@tonic-gate 						continue;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 					case 'v':
1147c478bd9Sstevel@tonic-gate 						prc_buff('\v');
1157c478bd9Sstevel@tonic-gate 						continue;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 					case '\\':
1187c478bd9Sstevel@tonic-gate 						prc_buff('\\');
1197c478bd9Sstevel@tonic-gate 						continue;
1207c478bd9Sstevel@tonic-gate 					case '0':
1217c478bd9Sstevel@tonic-gate 						j = wd = 0;
1227c478bd9Sstevel@tonic-gate 						while ((*++cp >= '0' &&
1238944aeb2SAjaykumar Venkatesulu 						    *cp <= '7') && j++ < 3) {
1247c478bd9Sstevel@tonic-gate 							wd <<= 3;
1257c478bd9Sstevel@tonic-gate 							wd |= (*cp - '0');
1267c478bd9Sstevel@tonic-gate 						}
1277c478bd9Sstevel@tonic-gate 						prc_buff(wd);
1287c478bd9Sstevel@tonic-gate 						--cp;
1297c478bd9Sstevel@tonic-gate 						continue;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 					default:
1327c478bd9Sstevel@tonic-gate 						cp--;
1337c478bd9Sstevel@tonic-gate 					}
1347c478bd9Sstevel@tonic-gate 					prc_buff(*cp);
1357c478bd9Sstevel@tonic-gate 					continue;
1367c478bd9Sstevel@tonic-gate 				} else {
1377c478bd9Sstevel@tonic-gate 					for (; len > 0; len--)
1387c478bd9Sstevel@tonic-gate 						prc_buff(*cp++);
1397c478bd9Sstevel@tonic-gate 					cp--;
1407c478bd9Sstevel@tonic-gate 					continue;
1417c478bd9Sstevel@tonic-gate 				}
1427c478bd9Sstevel@tonic-gate 			}
143*cc22b130SRich Burridge 			prc_buff(i == argc? '\n': ' ');
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		exit(0);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate }
148