echo.c revision 1.14
11.14Sdholland/*	$NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland 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.13Slukem#include <sys/cdefs.h>
551.14Sdholland__RCSID("$NetBSD: echo.c,v 1.14 2008/10/12 01:40:37 dholland Exp $");
561.13Slukem
571.1Scgd#define main echocmd
581.1Scgd
591.1Scgd#include "bltin.h"
601.1Scgd
611.8Schristosint
621.9Schristosmain(int argc, char **argv)
631.9Schristos{
641.12Sperry	char **ap;
651.12Sperry	char *p;
661.12Sperry	char c;
671.1Scgd	int count;
681.1Scgd	int nflag = 0;
691.1Scgd	int eflag = 0;
701.1Scgd
711.1Scgd	ap = argv;
721.1Scgd	if (argc)
731.1Scgd		ap++;
741.9Schristos
751.1Scgd	if ((p = *ap) != NULL) {
761.1Scgd		if (equal(p, "-n")) {
771.9Schristos			nflag = 1;
781.1Scgd			ap++;
791.1Scgd		} else if (equal(p, "-e")) {
801.9Schristos			eflag = 1;
811.1Scgd			ap++;
821.1Scgd		}
831.1Scgd	}
841.9Schristos
851.1Scgd	while ((p = *ap++) != NULL) {
861.1Scgd		while ((c = *p++) != '\0') {
871.1Scgd			if (c == '\\' && eflag) {
881.1Scgd				switch (*p++) {
891.9Schristos				case 'a':  c = '\a';  break;	/* bell */
901.1Scgd				case 'b':  c = '\b';  break;
911.1Scgd				case 'c':  return 0;		/* exit */
921.9Schristos				case 'e':  c =  033;  break;	/* escape */
931.1Scgd				case 'f':  c = '\f';  break;
941.1Scgd				case 'n':  c = '\n';  break;
951.1Scgd				case 'r':  c = '\r';  break;
961.1Scgd				case 't':  c = '\t';  break;
971.1Scgd				case 'v':  c = '\v';  break;
981.1Scgd				case '\\':  break;		/* c = '\\' */
991.1Scgd				case '0':
1001.1Scgd					c = 0;
1011.1Scgd					count = 3;
1021.1Scgd					while (--count >= 0 && (unsigned)(*p - '0') < 8)
1031.1Scgd						c = (c << 3) + (*p++ - '0');
1041.1Scgd					break;
1051.1Scgd				default:
1061.9Schristos					/* Output the '/' and char following */
1071.1Scgd					p--;
1081.1Scgd					break;
1091.1Scgd				}
1101.1Scgd			}
1111.1Scgd			putchar(c);
1121.1Scgd		}
1131.1Scgd		if (*ap)
1141.1Scgd			putchar(' ');
1151.1Scgd	}
1161.1Scgd	if (! nflag)
1171.1Scgd		putchar('\n');
1181.14Sdholland	fflush(stdout);
1191.14Sdholland	if (ferror(stdout))
1201.14Sdholland		return 1;
1211.1Scgd	return 0;
1221.1Scgd}
123