xref: /titanic_41/usr/src/cmd/echo/echo.c (revision cc22b130832529204c03214239a57aaadd05101f)
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
5*cc22b130SRich Burridge  * Common Development and Distribution License (the "License").
6*cc22b130SRich Burridge  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*cc22b130SRich Burridge  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <wchar.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate int
367c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	register char	*cp;
407c478bd9Sstevel@tonic-gate 	register int	i, wd;
417c478bd9Sstevel@tonic-gate 	int	j;
427c478bd9Sstevel@tonic-gate 	wchar_t		wc;
437c478bd9Sstevel@tonic-gate 	int		b_len;
447c478bd9Sstevel@tonic-gate 	char		*ep;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if (--argc == 0) {
497c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
507c478bd9Sstevel@tonic-gate 		if (fflush(stdout) != 0)
517c478bd9Sstevel@tonic-gate 			return (1);
527c478bd9Sstevel@tonic-gate 		return (0);
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	for (i = 1; i <= argc; i++) {
567c478bd9Sstevel@tonic-gate 		for (cp = argv[i], ep = cp + (int)strlen(cp);
577c478bd9Sstevel@tonic-gate 		    cp < ep; cp += b_len) {
587c478bd9Sstevel@tonic-gate 		if ((b_len = mbtowc(&wc, cp, MB_CUR_MAX)) <= 0) {
597c478bd9Sstevel@tonic-gate 			(void) putchar(*cp);
607c478bd9Sstevel@tonic-gate 			b_len = 1;
617c478bd9Sstevel@tonic-gate 			continue;
627c478bd9Sstevel@tonic-gate 		}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		if (wc != '\\') {
657c478bd9Sstevel@tonic-gate 			(void) putwchar(wc);
667c478bd9Sstevel@tonic-gate 			continue;
677c478bd9Sstevel@tonic-gate 		}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 			cp += b_len;
707c478bd9Sstevel@tonic-gate 			b_len = 1;
717c478bd9Sstevel@tonic-gate 			switch (*cp) {
727c478bd9Sstevel@tonic-gate 				case 'a':	/* alert - XCU4 */
737c478bd9Sstevel@tonic-gate 					(void) putchar('\a');
747c478bd9Sstevel@tonic-gate 					continue;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 				case 'b':
777c478bd9Sstevel@tonic-gate 					(void) putchar('\b');
787c478bd9Sstevel@tonic-gate 					continue;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 				case 'c':
817c478bd9Sstevel@tonic-gate 					if (fflush(stdout) != 0)
827c478bd9Sstevel@tonic-gate 						return (1);
837c478bd9Sstevel@tonic-gate 					return (0);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 				case 'f':
867c478bd9Sstevel@tonic-gate 					(void) putchar('\f');
877c478bd9Sstevel@tonic-gate 					continue;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 				case 'n':
907c478bd9Sstevel@tonic-gate 					(void) putchar('\n');
917c478bd9Sstevel@tonic-gate 					continue;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 				case 'r':
947c478bd9Sstevel@tonic-gate 					(void) putchar('\r');
957c478bd9Sstevel@tonic-gate 					continue;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 				case 't':
987c478bd9Sstevel@tonic-gate 					(void) putchar('\t');
997c478bd9Sstevel@tonic-gate 					continue;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 				case 'v':
1027c478bd9Sstevel@tonic-gate 					(void) putchar('\v');
1037c478bd9Sstevel@tonic-gate 					continue;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 				case '\\':
1067c478bd9Sstevel@tonic-gate 					(void) putchar('\\');
1077c478bd9Sstevel@tonic-gate 					continue;
1087c478bd9Sstevel@tonic-gate 				case '0':
1097c478bd9Sstevel@tonic-gate 					j = wd = 0;
1107c478bd9Sstevel@tonic-gate 					while ((*++cp >= '0' && *cp <= '7') &&
1117c478bd9Sstevel@tonic-gate 					    j++ < 3) {
1127c478bd9Sstevel@tonic-gate 						wd <<= 3;
1137c478bd9Sstevel@tonic-gate 						wd |= (*cp - '0');
1147c478bd9Sstevel@tonic-gate 					}
1157c478bd9Sstevel@tonic-gate 					(void) putchar(wd);
1167c478bd9Sstevel@tonic-gate 					--cp;
1177c478bd9Sstevel@tonic-gate 					continue;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 				default:
1207c478bd9Sstevel@tonic-gate 					cp--;
1217c478bd9Sstevel@tonic-gate 					(void) putchar(*cp);
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 		(void) putchar(i == argc? '\n': ' ');
1257c478bd9Sstevel@tonic-gate 		if (fflush(stdout) != 0)
1267c478bd9Sstevel@tonic-gate 			return (1);
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	return (0);
1297c478bd9Sstevel@tonic-gate }
130