Rev 2714 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2714 | cejka | 1 | /* $NetBSD: echo.c,v 1.8 1996/11/02 18:26:06 christos Exp $ */ |
2 | |||
3 | /*- |
||
4 | * Copyright (c) 1991, 1993 |
||
5 | * The Regents of the University of California. All rights reserved. |
||
6 | * |
||
7 | * This code is derived from software contributed to Berkeley by |
||
8 | * Kenneth Almquist. |
||
9 | * |
||
10 | * Redistribution and use in source and binary forms, with or without |
||
11 | * modification, are permitted provided that the following conditions |
||
12 | * are met: |
||
13 | * 1. Redistributions of source code must retain the above copyright |
||
14 | * notice, this list of conditions and the following disclaimer. |
||
15 | * 2. Redistributions in binary form must reproduce the above copyright |
||
16 | * notice, this list of conditions and the following disclaimer in the |
||
17 | * documentation and/or other materials provided with the distribution. |
||
18 | * 3. All advertising materials mentioning features or use of this software |
||
19 | * must display the following acknowledgement: |
||
20 | * This product includes software developed by the University of |
||
21 | * California, Berkeley and its contributors. |
||
22 | * 4. Neither the name of the University nor the names of its contributors |
||
23 | * may be used to endorse or promote products derived from this software |
||
24 | * without specific prior written permission. |
||
25 | * |
||
26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
36 | * SUCH DAMAGE. |
||
37 | * |
||
38 | * @(#)echo.c 8.1 (Berkeley) 5/31/93 |
||
39 | */ |
||
40 | |||
41 | /* |
||
42 | * Echo command. |
||
43 | */ |
||
44 | |||
45 | #define main echocmd |
||
46 | |||
47 | #ifdef _GNU_SOURCE |
||
48 | #include <stdio.h> |
||
49 | |||
50 | #include "../mystring.h" |
||
51 | #else |
||
52 | #include "bltin.h" |
||
53 | #endif |
||
54 | |||
55 | /* #define eflag 1 */ |
||
56 | |||
57 | int |
||
58 | main(argc, argv) char **argv; { |
||
59 | register char **ap; |
||
60 | register char *p; |
||
61 | register char c; |
||
62 | int nflag = 0; |
||
63 | #ifndef eflag |
||
64 | int eflag = 0; |
||
65 | #endif |
||
66 | |||
67 | ap = argv; |
||
68 | if (argc) |
||
69 | ap++; |
||
70 | while ((p = *ap) != NULL && *p == '-') { |
||
71 | if (equal(p, "-n")) { |
||
72 | nflag = 1; |
||
73 | } else if (equal(p, "-e")) { |
||
74 | #ifndef eflag |
||
75 | eflag = 1; |
||
76 | #endif |
||
77 | } else if (equal(p, "-E")) { |
||
78 | #ifndef eflag |
||
79 | eflag = 0; |
||
80 | #endif |
||
81 | } |
||
82 | else break; |
||
83 | ap++; |
||
84 | } |
||
85 | while ((p = *ap++) != NULL) { |
||
86 | while ((c = *p++) != '\0') { |
||
87 | if (c == '\\' && eflag) { |
||
88 | switch (c = *p++) { |
||
89 | case 'a': c = '\007'; break; |
||
90 | case 'b': c = '\b'; break; |
||
91 | case 'c': return 0; /* exit */ |
||
92 | case 'e': c = '\033'; break; |
||
93 | case 'f': c = '\f'; break; |
||
94 | case 'n': c = '\n'; break; |
||
95 | case 'r': c = '\r'; break; |
||
96 | case 't': c = '\t'; break; |
||
97 | case 'v': c = '\v'; break; |
||
98 | case '\\': break; /* c = '\\' */ |
||
99 | case '0': case '1': case '2': case '3': |
||
100 | case '4': case '5': case '6': case '7': |
||
101 | c -= '0'; |
||
102 | if (*p >= '0' && *p <= '7') |
||
103 | c = c * 8 + (*p++ - '0'); |
||
104 | if (*p >= '0' && *p <= '7') |
||
105 | c = c * 8 + (*p++ - '0'); |
||
106 | break; |
||
107 | default: |
||
108 | p--; |
||
109 | break; |
||
110 | } |
||
111 | } |
||
112 | putchar(c); |
||
113 | } |
||
114 | if (*ap) |
||
115 | putchar(' '); |
||
116 | } |
||
117 | if (! nflag) |
||
118 | putchar('\n'); |
||
119 | #ifdef _GNU_SOURCE |
||
120 | fflush(stdout); |
||
121 | if (ferror(stdout)) { |
||
122 | clearerr(stdout); |
||
123 | return 1; |
||
124 | } |
||
125 | #endif |
||
126 | return 0; |
||
127 | } |