1 | /*-
|
---|
2 | * Copyright (c) 1991 The Regents of the University of California.
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * This code is derived from software contributed to Berkeley by
|
---|
6 | * Kenneth Almquist.
|
---|
7 | *
|
---|
8 | * Redistribution and use in source and binary forms, with or without
|
---|
9 | * modification, are permitted provided that the following conditions
|
---|
10 | * are met:
|
---|
11 | * 1. Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * 3. All advertising materials mentioning features or use of this software
|
---|
17 | * must display the following acknowledgement:
|
---|
18 | * This product includes software developed by the University of
|
---|
19 | * California, Berkeley and its contributors.
|
---|
20 | * 4. Neither the name of the University nor the names of its contributors
|
---|
21 | * may be used to endorse or promote products derived from this software
|
---|
22 | * without specific prior written permission.
|
---|
23 | *
|
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
34 | * SUCH DAMAGE.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef lint
|
---|
38 | static char sccsid[] = "@(#)miscbltin.c 5.2 (Berkeley) 3/13/91";
|
---|
39 | #endif /* not lint */
|
---|
40 |
|
---|
41 | /*
|
---|
42 | * Miscelaneous builtins.
|
---|
43 | */
|
---|
44 |
|
---|
45 | #include "shell.h"
|
---|
46 | #include "options.h"
|
---|
47 | #include "var.h"
|
---|
48 | #include "output.h"
|
---|
49 | #include "memalloc.h"
|
---|
50 | #include "error.h"
|
---|
51 | #include "mystring.h"
|
---|
52 |
|
---|
53 | #undef eflag
|
---|
54 |
|
---|
55 | extern char **argptr; /* argument list for builtin command */
|
---|
56 |
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * The read builtin. The -e option causes backslashes to escape the
|
---|
60 | * following character.
|
---|
61 | *
|
---|
62 | * This uses unbuffered input, which may be avoidable in some cases.
|
---|
63 | */
|
---|
64 |
|
---|
65 | readcmd(argc, argv) char **argv; {
|
---|
66 | char **ap;
|
---|
67 | int backslash;
|
---|
68 | char c;
|
---|
69 | int eflag;
|
---|
70 | char *prompt;
|
---|
71 | char *ifs;
|
---|
72 | char *p;
|
---|
73 | int startword;
|
---|
74 | int status;
|
---|
75 | int i;
|
---|
76 |
|
---|
77 | eflag = 0;
|
---|
78 | prompt = NULL;
|
---|
79 | while ((i = nextopt("ep:")) != '\0') {
|
---|
80 | if (i == 'p')
|
---|
81 | prompt = optarg;
|
---|
82 | else
|
---|
83 | eflag = 1;
|
---|
84 | }
|
---|
85 | if (prompt && isatty(0)) {
|
---|
86 | out2str(prompt);
|
---|
87 | flushall();
|
---|
88 | }
|
---|
89 | if (*(ap = argptr) == NULL)
|
---|
90 | error("arg count");
|
---|
91 | if ((ifs = bltinlookup("IFS", 1)) == NULL)
|
---|
92 | ifs = nullstr;
|
---|
93 | status = 0;
|
---|
94 | startword = 1;
|
---|
95 | backslash = 0;
|
---|
96 | STARTSTACKSTR(p);
|
---|
97 | for (;;) {
|
---|
98 | if (read(0, &c, 1) != 1) {
|
---|
99 | status = 1;
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | if (c == '\0')
|
---|
103 | continue;
|
---|
104 | if (backslash) {
|
---|
105 | backslash = 0;
|
---|
106 | if (c != '\n')
|
---|
107 | STPUTC(c, p);
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 | if (eflag && c == '\\') {
|
---|
111 | backslash++;
|
---|
112 | continue;
|
---|
113 | }
|
---|
114 | if (c == '\n')
|
---|
115 | break;
|
---|
116 | if (startword && *ifs == ' ' && strchr(ifs, c)) {
|
---|
117 | continue;
|
---|
118 | }
|
---|
119 | startword = 0;
|
---|
120 | if (backslash && c == '\\') {
|
---|
121 | if (read(0, &c, 1) != 1) {
|
---|
122 | status = 1;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 | STPUTC(c, p);
|
---|
126 | } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
|
---|
127 | STACKSTRNUL(p);
|
---|
128 | setvar(*ap, stackblock(), 0);
|
---|
129 | ap++;
|
---|
130 | startword = 1;
|
---|
131 | STARTSTACKSTR(p);
|
---|
132 | } else {
|
---|
133 | STPUTC(c, p);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | STACKSTRNUL(p);
|
---|
137 | setvar(*ap, stackblock(), 0);
|
---|
138 | while (*++ap != NULL)
|
---|
139 | setvar(*ap, nullstr, 0);
|
---|
140 | return status;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 |
|
---|
145 | umaskcmd(argc, argv) char **argv; {
|
---|
146 | int mask;
|
---|
147 | char *p;
|
---|
148 | int i;
|
---|
149 |
|
---|
150 | if ((p = argv[1]) == NULL) {
|
---|
151 | INTOFF;
|
---|
152 | mask = umask(0);
|
---|
153 | umask(mask);
|
---|
154 | INTON;
|
---|
155 | out1fmt("%.4o\n", mask); /* %#o might be better */
|
---|
156 | } else {
|
---|
157 | mask = 0;
|
---|
158 | do {
|
---|
159 | if ((unsigned)(i = *p - '0') >= 8)
|
---|
160 | error("Illegal number: %s", argv[1]);
|
---|
161 | mask = (mask << 3) + i;
|
---|
162 | } while (*++p != '\0');
|
---|
163 | umask(mask);
|
---|
164 | }
|
---|
165 | return 0;
|
---|
166 | }
|
---|