xref: /onnv-gate/usr/src/cmd/echo/echo.c (revision 12836:0d0733997434)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*12836Srich.burridge@oracle.com  * Common Development and Distribution License (the "License").
6*12836Srich.burridge@oracle.com  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12836Srich.burridge@oracle.com  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <wchar.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate int
main(int argc,char * argv[])360Sstevel@tonic-gate main(int argc, char *argv[])
370Sstevel@tonic-gate {
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 	register char	*cp;
400Sstevel@tonic-gate 	register int	i, wd;
410Sstevel@tonic-gate 	int	j;
420Sstevel@tonic-gate 	wchar_t		wc;
430Sstevel@tonic-gate 	int		b_len;
440Sstevel@tonic-gate 	char		*ep;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	if (--argc == 0) {
490Sstevel@tonic-gate 		(void) putchar('\n');
500Sstevel@tonic-gate 		if (fflush(stdout) != 0)
510Sstevel@tonic-gate 			return (1);
520Sstevel@tonic-gate 		return (0);
530Sstevel@tonic-gate 	}
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	for (i = 1; i <= argc; i++) {
560Sstevel@tonic-gate 		for (cp = argv[i], ep = cp + (int)strlen(cp);
57*12836Srich.burridge@oracle.com 		    cp < ep; cp += b_len) {
580Sstevel@tonic-gate 		if ((b_len = mbtowc(&wc, cp, MB_CUR_MAX)) <= 0) {
590Sstevel@tonic-gate 			(void) putchar(*cp);
600Sstevel@tonic-gate 			b_len = 1;
610Sstevel@tonic-gate 			continue;
620Sstevel@tonic-gate 		}
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 		if (wc != '\\') {
650Sstevel@tonic-gate 			(void) putwchar(wc);
660Sstevel@tonic-gate 			continue;
670Sstevel@tonic-gate 		}
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 			cp += b_len;
700Sstevel@tonic-gate 			b_len = 1;
710Sstevel@tonic-gate 			switch (*cp) {
720Sstevel@tonic-gate 				case 'a':	/* alert - XCU4 */
730Sstevel@tonic-gate 					(void) putchar('\a');
740Sstevel@tonic-gate 					continue;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 				case 'b':
770Sstevel@tonic-gate 					(void) putchar('\b');
780Sstevel@tonic-gate 					continue;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 				case 'c':
810Sstevel@tonic-gate 					if (fflush(stdout) != 0)
820Sstevel@tonic-gate 						return (1);
830Sstevel@tonic-gate 					return (0);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 				case 'f':
860Sstevel@tonic-gate 					(void) putchar('\f');
870Sstevel@tonic-gate 					continue;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 				case 'n':
900Sstevel@tonic-gate 					(void) putchar('\n');
910Sstevel@tonic-gate 					continue;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 				case 'r':
940Sstevel@tonic-gate 					(void) putchar('\r');
950Sstevel@tonic-gate 					continue;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 				case 't':
980Sstevel@tonic-gate 					(void) putchar('\t');
990Sstevel@tonic-gate 					continue;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 				case 'v':
1020Sstevel@tonic-gate 					(void) putchar('\v');
1030Sstevel@tonic-gate 					continue;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 				case '\\':
1060Sstevel@tonic-gate 					(void) putchar('\\');
1070Sstevel@tonic-gate 					continue;
1080Sstevel@tonic-gate 				case '0':
1090Sstevel@tonic-gate 					j = wd = 0;
1100Sstevel@tonic-gate 					while ((*++cp >= '0' && *cp <= '7') &&
111*12836Srich.burridge@oracle.com 					    j++ < 3) {
1120Sstevel@tonic-gate 						wd <<= 3;
1130Sstevel@tonic-gate 						wd |= (*cp - '0');
1140Sstevel@tonic-gate 					}
1150Sstevel@tonic-gate 					(void) putchar(wd);
1160Sstevel@tonic-gate 					--cp;
1170Sstevel@tonic-gate 					continue;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 				default:
1200Sstevel@tonic-gate 					cp--;
1210Sstevel@tonic-gate 					(void) putchar(*cp);
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 		}
124*12836Srich.burridge@oracle.com 		(void) putchar(i == argc? '\n': ' ');
125*12836Srich.burridge@oracle.com 		if (fflush(stdout) != 0)
126*12836Srich.burridge@oracle.com 			return (1);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	return (0);
1290Sstevel@tonic-gate }
130