echo.c revision 1.12
11.12Sperry/* $NetBSD: echo.c,v 1.12 2005/02/06 04:43:43 perry Exp $ */ 21.7Scgd 31.1Scgd/*- 41.5Sjtc * Copyright (c) 1991, 1993 51.5Sjtc * The Regents of the University of California. All rights reserved. 61.1Scgd * 71.1Scgd * This code is derived from software contributed to Berkeley by 81.1Scgd * Kenneth Almquist. 91.1Scgd * 101.1Scgd * Redistribution and use in source and binary forms, with or without 111.1Scgd * modification, are permitted provided that the following conditions 121.1Scgd * are met: 131.1Scgd * 1. Redistributions of source code must retain the above copyright 141.1Scgd * notice, this list of conditions and the following disclaimer. 151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright 161.1Scgd * notice, this list of conditions and the following disclaimer in the 171.1Scgd * documentation and/or other materials provided with the distribution. 181.11Sagc * 3. Neither the name of the University nor the names of its contributors 191.1Scgd * may be used to endorse or promote products derived from this software 201.1Scgd * without specific prior written permission. 211.1Scgd * 221.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251.1Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321.1Scgd * SUCH DAMAGE. 331.5Sjtc * 341.7Scgd * @(#)echo.c 8.1 (Berkeley) 5/31/93 351.1Scgd */ 361.1Scgd 371.1Scgd/* 381.1Scgd * Echo command. 391.9Schristos * 401.9Schristos * echo is steeped in tradition - several of them! 411.9Schristos * netbsd has supported 'echo [-n | -e] args' in spite of -e not being 421.9Schristos * documented anywhere. 431.9Schristos * Posix requires that -n be supported, output from strings containing 441.9Schristos * \ is implementation defined 451.9Schristos * The Single Unix Spec requires that \ escapes be treated as if -e 461.9Schristos * were set, but that -n not be treated as an option. 471.9Schristos * (ksh supports 'echo [-eEn] args', but not -- so that it is actually 481.10Swiz * impossible to actually output '-n') 491.9Schristos * 501.9Schristos * It is suggested that 'printf "%b" "string"' be used to get \ sequences 511.9Schristos * expanded. printf is now a builtin of netbsd's sh and csh. 521.1Scgd */ 531.1Scgd 541.1Scgd#define main echocmd 551.1Scgd 561.1Scgd#include "bltin.h" 571.1Scgd 581.8Schristosint 591.9Schristosmain(int argc, char **argv) 601.9Schristos{ 611.12Sperry char **ap; 621.12Sperry char *p; 631.12Sperry char c; 641.1Scgd int count; 651.1Scgd int nflag = 0; 661.1Scgd int eflag = 0; 671.1Scgd 681.1Scgd ap = argv; 691.1Scgd if (argc) 701.1Scgd ap++; 711.9Schristos 721.1Scgd if ((p = *ap) != NULL) { 731.1Scgd if (equal(p, "-n")) { 741.9Schristos nflag = 1; 751.1Scgd ap++; 761.1Scgd } else if (equal(p, "-e")) { 771.9Schristos eflag = 1; 781.1Scgd ap++; 791.1Scgd } 801.1Scgd } 811.9Schristos 821.1Scgd while ((p = *ap++) != NULL) { 831.1Scgd while ((c = *p++) != '\0') { 841.1Scgd if (c == '\\' && eflag) { 851.1Scgd switch (*p++) { 861.9Schristos case 'a': c = '\a'; break; /* bell */ 871.1Scgd case 'b': c = '\b'; break; 881.1Scgd case 'c': return 0; /* exit */ 891.9Schristos case 'e': c = 033; break; /* escape */ 901.1Scgd case 'f': c = '\f'; break; 911.1Scgd case 'n': c = '\n'; break; 921.1Scgd case 'r': c = '\r'; break; 931.1Scgd case 't': c = '\t'; break; 941.1Scgd case 'v': c = '\v'; break; 951.1Scgd case '\\': break; /* c = '\\' */ 961.1Scgd case '0': 971.1Scgd c = 0; 981.1Scgd count = 3; 991.1Scgd while (--count >= 0 && (unsigned)(*p - '0') < 8) 1001.1Scgd c = (c << 3) + (*p++ - '0'); 1011.1Scgd break; 1021.1Scgd default: 1031.9Schristos /* Output the '/' and char following */ 1041.1Scgd p--; 1051.1Scgd break; 1061.1Scgd } 1071.1Scgd } 1081.1Scgd putchar(c); 1091.1Scgd } 1101.1Scgd if (*ap) 1111.1Scgd putchar(' '); 1121.1Scgd } 1131.1Scgd if (! nflag) 1141.1Scgd putchar('\n'); 1151.1Scgd return 0; 1161.1Scgd} 117