miscbltin.c revision 1.17 1 /* $NetBSD: miscbltin.c,v 1.17 1997/01/11 02:04:38 tls 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
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
42 #else
43 static char rcsid[] = "$NetBSD: miscbltin.c,v 1.17 1997/01/11 02:04:38 tls Exp $";
44 #endif
45 #endif /* not lint */
46
47 /*
48 * Miscelaneous builtins.
49 */
50
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/time.h>
54 #include <sys/resource.h>
55 #include <unistd.h>
56 #include <ctype.h>
57
58 #include "shell.h"
59 #include "options.h"
60 #include "var.h"
61 #include "output.h"
62 #include "memalloc.h"
63 #include "error.h"
64 #include "mystring.h"
65
66 #undef eflag
67
68 extern char **argptr; /* argument list for builtin command */
69
70
71 /*
72 * The read builtin. The -e option causes backslashes to escape the
73 * following character.
74 *
75 * This uses unbuffered input, which may be avoidable in some cases.
76 */
77
78 int
79 readcmd(argc, argv)
80 int argc;
81 char **argv;
82 {
83 char **ap;
84 int backslash;
85 char c;
86 int eflag;
87 char *prompt;
88 char *ifs;
89 char *p;
90 int startword;
91 int status;
92 int i;
93
94 eflag = 0;
95 prompt = NULL;
96 while ((i = nextopt("ep:")) != '\0') {
97 if (i == 'p')
98 prompt = optarg;
99 else
100 eflag = 1;
101 }
102 if (prompt && isatty(0)) {
103 out2str(prompt);
104 flushall();
105 }
106 if (*(ap = argptr) == NULL)
107 error("arg count");
108 if ((ifs = bltinlookup("IFS", 1)) == NULL)
109 ifs = nullstr;
110 status = 0;
111 startword = 1;
112 backslash = 0;
113 STARTSTACKSTR(p);
114 for (;;) {
115 if (read(0, &c, 1) != 1) {
116 status = 1;
117 break;
118 }
119 if (c == '\0')
120 continue;
121 if (backslash) {
122 backslash = 0;
123 if (c != '\n')
124 STPUTC(c, p);
125 continue;
126 }
127 if (eflag && c == '\\') {
128 backslash++;
129 continue;
130 }
131 if (c == '\n')
132 break;
133 if (startword && *ifs == ' ' && strchr(ifs, c)) {
134 continue;
135 }
136 startword = 0;
137 if (backslash && c == '\\') {
138 if (read(0, &c, 1) != 1) {
139 status = 1;
140 break;
141 }
142 STPUTC(c, p);
143 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
144 STACKSTRNUL(p);
145 setvar(*ap, stackblock(), 0);
146 ap++;
147 startword = 1;
148 STARTSTACKSTR(p);
149 } else {
150 STPUTC(c, p);
151 }
152 }
153 STACKSTRNUL(p);
154 setvar(*ap, stackblock(), 0);
155 while (*++ap != NULL)
156 setvar(*ap, nullstr, 0);
157 return status;
158 }
159
160
161
162 int
163 umaskcmd(argc, argv)
164 int argc;
165 char **argv;
166 {
167 char *ap;
168 int mask;
169 int i;
170 int symbolic_mode = 0;
171
172 while ((i = nextopt("S")) != '\0') {
173 symbolic_mode = 1;
174 }
175
176 INTOFF;
177 mask = umask(0);
178 umask(mask);
179 INTON;
180
181 if ((ap = *argptr) == NULL) {
182 if (symbolic_mode) {
183 char u[4], g[4], o[4];
184
185 i = 0;
186 if ((mask & S_IRUSR) == 0)
187 u[i++] = 'r';
188 if ((mask & S_IWUSR) == 0)
189 u[i++] = 'w';
190 if ((mask & S_IXUSR) == 0)
191 u[i++] = 'x';
192 u[i] = '\0';
193
194 i = 0;
195 if ((mask & S_IRGRP) == 0)
196 g[i++] = 'r';
197 if ((mask & S_IWGRP) == 0)
198 g[i++] = 'w';
199 if ((mask & S_IXGRP) == 0)
200 g[i++] = 'x';
201 g[i] = '\0';
202
203 i = 0;
204 if ((mask & S_IROTH) == 0)
205 o[i++] = 'r';
206 if ((mask & S_IWOTH) == 0)
207 o[i++] = 'w';
208 if ((mask & S_IXOTH) == 0)
209 o[i++] = 'x';
210 o[i] = '\0';
211
212 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
213 } else {
214 out1fmt("%.4o\n", mask);
215 }
216 } else {
217 if (isdigit(*ap)) {
218 mask = 0;
219 do {
220 if (*ap >= '8' || *ap < '0')
221 error("Illegal number: %s", argv[1]);
222 mask = (mask << 3) + (*ap - '0');
223 } while (*++ap != '\0');
224 umask(mask);
225 } else {
226 void *set;
227 if ((set = setmode (ap)) == 0)
228 error("Illegal number: %s", ap);
229
230 mask = getmode (set, ~mask & 0777);
231 umask(~mask & 0777);
232 }
233 }
234 return 0;
235 }
236
237 /*
238 * ulimit builtin
239 *
240 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
241 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
242 * ash by J.T. Conklin.
243 *
244 * Public domain.
245 */
246
247 struct limits {
248 const char *name;
249 int cmd;
250 int factor; /* multiply by to get rlim_{cur,max} values */
251 char option;
252 };
253
254 static const struct limits limits[] = {
255 #ifdef RLIMIT_CPU
256 { "time(seconds)", RLIMIT_CPU, 1, 't' },
257 #endif
258 #ifdef RLIMIT_FSIZE
259 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
260 #endif
261 #ifdef RLIMIT_DATA
262 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
263 #endif
264 #ifdef RLIMIT_STACK
265 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
266 #endif
267 #ifdef RLIMIT_CORE
268 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
269 #endif
270 #ifdef RLIMIT_RSS
271 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
272 #endif
273 #ifdef RLIMIT_MEMLOCK
274 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
275 #endif
276 #ifdef RLIMIT_NPROC
277 { "process(processes)", RLIMIT_NPROC, 1, 'p' },
278 #endif
279 #ifdef RLIMIT_NOFILE
280 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
281 #endif
282 #ifdef RLIMIT_VMEM
283 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
284 #endif
285 #ifdef RLIMIT_SWAP
286 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
287 #endif
288 { (char *) 0, 0, 0, '\0' }
289 };
290
291 int
292 ulimitcmd(argc, argv)
293 int argc;
294 char **argv;
295 {
296 int c;
297 rlim_t val;
298 enum { SOFT = 0x1, HARD = 0x2 }
299 how = SOFT | HARD;
300 const struct limits *l;
301 int set, all = 0;
302 int optc, what;
303 struct rlimit limit;
304
305 what = 'f';
306 while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
307 switch (optc) {
308 case 'H':
309 how = HARD;
310 break;
311 case 'S':
312 how = SOFT;
313 break;
314 case 'a':
315 all = 1;
316 break;
317 default:
318 what = optc;
319 }
320
321 for (l = limits; l->name && l->option != what; l++)
322 ;
323 if (!l->name)
324 error("ulimit: internal error (%c)\n", what);
325
326 set = *argptr ? 1 : 0;
327 if (set) {
328 char *p = *argptr;
329
330 if (all || argptr[1])
331 error("ulimit: too many arguments\n");
332 if (strcmp(p, "unlimited") == 0)
333 val = RLIM_INFINITY;
334 else {
335 val = (rlim_t) 0;
336
337 while ((c = *p++) >= '0' && c <= '9')
338 {
339 val = (val * 10) + (long)(c - '0');
340 if (val < (rlim_t) 0)
341 break;
342 }
343 if (c)
344 error("ulimit: bad number\n");
345 val *= l->factor;
346 }
347 }
348 if (all) {
349 for (l = limits; l->name; l++) {
350 getrlimit(l->cmd, &limit);
351 if (how & SOFT)
352 val = limit.rlim_cur;
353 else if (how & HARD)
354 val = limit.rlim_max;
355
356 out1fmt("%-20s ", l->name);
357 if (val == RLIM_INFINITY)
358 out1fmt("unlimited\n");
359 else
360 {
361 val /= l->factor;
362 out1fmt("%qd\n", (quad_t) val);
363 }
364 }
365 return 0;
366 }
367
368 getrlimit(l->cmd, &limit);
369 if (set) {
370 if (how & SOFT)
371 limit.rlim_cur = val;
372 if (how & HARD)
373 limit.rlim_max = val;
374 if (setrlimit(l->cmd, &limit) < 0)
375 error("ulimit: bad limit\n");
376 } else {
377 if (how & SOFT)
378 val = limit.rlim_cur;
379 else if (how & HARD)
380 val = limit.rlim_max;
381
382 if (val == RLIM_INFINITY)
383 out1fmt("unlimited\n");
384 else
385 {
386 val /= l->factor;
387 out1fmt("%qd\n", (quad_t) val);
388 }
389 }
390 return 0;
391 }
392