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*8944aeb2SAjaykumar Venkatesulu * Common Development and Distribution License (the "License"). 6*8944aeb2SAjaykumar 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 /* 23*8944aeb2SAjaykumar 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 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 #ifdef _iBCS2 /* SCO compatibility support */ 517c478bd9Sstevel@tonic-gate struct namnod *sysv3; 527c478bd9Sstevel@tonic-gate int do_sysv3 = 0; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate sysv3 = findnam("SYSV3"); 557c478bd9Sstevel@tonic-gate if (sysv3 && (sysv3->namflg & (N_EXPORT | N_ENVNAM))) 567c478bd9Sstevel@tonic-gate do_sysv3 = 1; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate /* Do the -n parsing if sysv3 is set or if ucb_builtsin is set */ 597c478bd9Sstevel@tonic-gate if (ucb_builtins && !do_sysv3) { 607c478bd9Sstevel@tonic-gate #else 617c478bd9Sstevel@tonic-gate if (ucb_builtins) { 627c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate nflg = 0; 65*8944aeb2SAjaykumar Venkatesulu if (argc > 1 && argv[1][0] == '-' && 66*8944aeb2SAjaykumar Venkatesulu argv[1][1] == 'n' && !argv[1][2]) { 677c478bd9Sstevel@tonic-gate nflg++; 687c478bd9Sstevel@tonic-gate argc--; 697c478bd9Sstevel@tonic-gate argv++; 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 737c478bd9Sstevel@tonic-gate sigchk(); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate for (cp = argv[i]; *cp; cp++) { 767c478bd9Sstevel@tonic-gate prc_buff(*cp); 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (i < argc-1) 807c478bd9Sstevel@tonic-gate prc_buff(' '); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate if (nflg == 0) 847c478bd9Sstevel@tonic-gate prc_buff('\n'); 857c478bd9Sstevel@tonic-gate exit(0); 867c478bd9Sstevel@tonic-gate } else { 877c478bd9Sstevel@tonic-gate if (--argc == 0) { 887c478bd9Sstevel@tonic-gate prc_buff('\n'); 897c478bd9Sstevel@tonic-gate exit(0); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate #ifdef _iBCS2 927c478bd9Sstevel@tonic-gate if (do_sysv3) { 937c478bd9Sstevel@tonic-gate if (argc > 1 && argv[1][0] == '-' && 94*8944aeb2SAjaykumar Venkatesulu argv[1][1] == 'n' && !argv[1][2]) { 957c478bd9Sstevel@tonic-gate nflg++; 967c478bd9Sstevel@tonic-gate /* Step past the -n */ 977c478bd9Sstevel@tonic-gate argc--; 987c478bd9Sstevel@tonic-gate argv++; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 1027c478bd9Sstevel@tonic-gate 103965005c8Schin for (i = 1; i <= argc; i++) { 1047c478bd9Sstevel@tonic-gate sigchk(); 1057c478bd9Sstevel@tonic-gate for (cp = argv[i]; *cp; cp++) { 1067c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cp, 1077c478bd9Sstevel@tonic-gate MB_LEN_MAX)) <= 0) { 1087c478bd9Sstevel@tonic-gate prc_buff(*cp); 1097c478bd9Sstevel@tonic-gate continue; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (wc == '\\') { 1137c478bd9Sstevel@tonic-gate switch (*++cp) { 1147c478bd9Sstevel@tonic-gate case 'b': 1157c478bd9Sstevel@tonic-gate prc_buff('\b'); 1167c478bd9Sstevel@tonic-gate continue; 1177c478bd9Sstevel@tonic-gate case 'c': 1187c478bd9Sstevel@tonic-gate exit(0); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate case 'f': 1217c478bd9Sstevel@tonic-gate prc_buff('\f'); 1227c478bd9Sstevel@tonic-gate continue; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate case 'n': 1257c478bd9Sstevel@tonic-gate prc_buff('\n'); 1267c478bd9Sstevel@tonic-gate continue; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate case 'r': 1297c478bd9Sstevel@tonic-gate prc_buff('\r'); 1307c478bd9Sstevel@tonic-gate continue; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate case 't': 1337c478bd9Sstevel@tonic-gate prc_buff('\t'); 1347c478bd9Sstevel@tonic-gate continue; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate case 'v': 1377c478bd9Sstevel@tonic-gate prc_buff('\v'); 1387c478bd9Sstevel@tonic-gate continue; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate case '\\': 1417c478bd9Sstevel@tonic-gate prc_buff('\\'); 1427c478bd9Sstevel@tonic-gate continue; 1437c478bd9Sstevel@tonic-gate case '0': 1447c478bd9Sstevel@tonic-gate j = wd = 0; 1457c478bd9Sstevel@tonic-gate while ((*++cp >= '0' && 1467c478bd9Sstevel@tonic-gate *cp <= '7') && j++ < 3) { 1477c478bd9Sstevel@tonic-gate wd <<= 3; 1487c478bd9Sstevel@tonic-gate wd |= (*cp - '0'); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate prc_buff(wd); 1517c478bd9Sstevel@tonic-gate --cp; 1527c478bd9Sstevel@tonic-gate continue; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate default: 1557c478bd9Sstevel@tonic-gate cp--; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate prc_buff(*cp); 1587c478bd9Sstevel@tonic-gate continue; 1597c478bd9Sstevel@tonic-gate } else { 1607c478bd9Sstevel@tonic-gate for (; len > 0; len--) 1617c478bd9Sstevel@tonic-gate prc_buff(*cp++); 1627c478bd9Sstevel@tonic-gate cp--; 1637c478bd9Sstevel@tonic-gate continue; 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate #ifdef _iBCS2 1677c478bd9Sstevel@tonic-gate /* Don't do if don't want newlines & out of args */ 1687c478bd9Sstevel@tonic-gate if (!(nflg && i == argc)) 1697c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 1707c478bd9Sstevel@tonic-gate prc_buff(i == argc? '\n': ' '); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate exit(0); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate } 175