miscbltin.c revision 1.12 1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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[] = "from: @(#)miscbltin.c 8.2 (Berkeley) 4/16/94";*/
39 static char *rcsid = "$Id: miscbltin.c,v 1.12 1994/12/05 19:07:45 cgd Exp $";
40 #endif /* not lint */
41
42 /*
43 * Miscelaneous builtins.
44 */
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <unistd.h>
49 #include <ctype.h>
50 #include "shell.h"
51 #include "options.h"
52 #include "var.h"
53 #include "output.h"
54 #include "memalloc.h"
55 #include "error.h"
56 #include "mystring.h"
57
58 #undef eflag
59
60 extern char **argptr; /* argument list for builtin command */
61
62
63 /*
64 * The read builtin. The -e option causes backslashes to escape the
65 * following character.
66 *
67 * This uses unbuffered input, which may be avoidable in some cases.
68 */
69
70 int
71 readcmd(argc, argv)
72 int argc;
73 char **argv;
74 {
75 char **ap;
76 int backslash;
77 char c;
78 int eflag;
79 char *prompt;
80 char *ifs;
81 char *p;
82 int startword;
83 int status;
84 int i;
85
86 eflag = 0;
87 prompt = NULL;
88 while ((i = nextopt("ep:")) != '\0') {
89 if (i == 'p')
90 prompt = optarg;
91 else
92 eflag = 1;
93 }
94 if (prompt && isatty(0)) {
95 out2str(prompt);
96 flushall();
97 }
98 if (*(ap = argptr) == NULL)
99 error("arg count");
100 if ((ifs = bltinlookup("IFS", 1)) == NULL)
101 ifs = nullstr;
102 status = 0;
103 startword = 1;
104 backslash = 0;
105 STARTSTACKSTR(p);
106 for (;;) {
107 if (read(0, &c, 1) != 1) {
108 status = 1;
109 break;
110 }
111 if (c == '\0')
112 continue;
113 if (backslash) {
114 backslash = 0;
115 if (c != '\n')
116 STPUTC(c, p);
117 continue;
118 }
119 if (eflag && c == '\\') {
120 backslash++;
121 continue;
122 }
123 if (c == '\n')
124 break;
125 if (startword && *ifs == ' ' && strchr(ifs, c)) {
126 continue;
127 }
128 startword = 0;
129 if (backslash && c == '\\') {
130 if (read(0, &c, 1) != 1) {
131 status = 1;
132 break;
133 }
134 STPUTC(c, p);
135 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
136 STACKSTRNUL(p);
137 setvar(*ap, stackblock(), 0);
138 ap++;
139 startword = 1;
140 STARTSTACKSTR(p);
141 } else {
142 STPUTC(c, p);
143 }
144 }
145 STACKSTRNUL(p);
146 setvar(*ap, stackblock(), 0);
147 while (*++ap != NULL)
148 setvar(*ap, nullstr, 0);
149 return status;
150 }
151
152
153
154 int
155 umaskcmd(argc, argv)
156 int argc;
157 char **argv;
158 {
159 char *ap;
160 int mask;
161 int i;
162 int symbolic_mode = 0;
163
164 while ((i = nextopt("S")) != '\0') {
165 symbolic_mode = 1;
166 }
167
168 INTOFF;
169 mask = umask(0);
170 umask(mask);
171 INTON;
172
173 if ((ap = *argptr) == NULL) {
174 if (symbolic_mode) {
175 char u[4], g[4], o[4];
176
177 i = 0;
178 if ((mask & S_IRUSR) == 0)
179 u[i++] = 'r';
180 if ((mask & S_IWUSR) == 0)
181 u[i++] = 'w';
182 if ((mask & S_IXUSR) == 0)
183 u[i++] = 'x';
184 u[i] = '\0';
185
186 i = 0;
187 if ((mask & S_IRGRP) == 0)
188 g[i++] = 'r';
189 if ((mask & S_IWGRP) == 0)
190 g[i++] = 'w';
191 if ((mask & S_IXGRP) == 0)
192 g[i++] = 'x';
193 g[i] = '\0';
194
195 i = 0;
196 if ((mask & S_IROTH) == 0)
197 o[i++] = 'r';
198 if ((mask & S_IWOTH) == 0)
199 o[i++] = 'w';
200 if ((mask & S_IXOTH) == 0)
201 o[i++] = 'x';
202 o[i] = '\0';
203
204 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
205 } else {
206 out1fmt("%.4o\n", mask);
207 }
208 } else {
209 if (isdigit(*ap)) {
210 mask = 0;
211 do {
212 if (*ap >= '8' || *ap < '0')
213 error("Illegal number: %s", argv[1]);
214 mask = (mask << 3) + (*ap - '0');
215 } while (*++ap != '\0');
216 umask(mask);
217 } else {
218 void *set;
219 if ((set = setmode (ap)) == 0)
220 error("Illegal number: %s", ap);
221
222 mask = getmode (set, ~mask & 0777);
223 umask(~mask & 0777);
224 }
225 }
226 return 0;
227 }
228