miscbltin.c revision 1.42 1 1.42 njoly /* $NetBSD: miscbltin.c,v 1.42 2012/06/11 18:28:10 njoly Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.32 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 christos static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
39 1.13 cgd #else
40 1.42 njoly __RCSID("$NetBSD: miscbltin.c,v 1.42 2012/06/11 18:28:10 njoly Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * Miscelaneous builtins.
46 1.1 cgd */
47 1.1 cgd
48 1.23 christos #include <sys/types.h> /* quad_t */
49 1.23 christos #include <sys/param.h> /* BSD4_4 */
50 1.8 jtc #include <sys/stat.h>
51 1.14 christos #include <sys/time.h>
52 1.14 christos #include <sys/resource.h>
53 1.9 jtc #include <unistd.h>
54 1.27 christos #include <stdlib.h>
55 1.9 jtc #include <ctype.h>
56 1.28 christos #include <errno.h>
57 1.14 christos
58 1.1 cgd #include "shell.h"
59 1.1 cgd #include "options.h"
60 1.1 cgd #include "var.h"
61 1.1 cgd #include "output.h"
62 1.1 cgd #include "memalloc.h"
63 1.1 cgd #include "error.h"
64 1.39 christos #include "builtins.h"
65 1.1 cgd #include "mystring.h"
66 1.1 cgd
67 1.20 kleink #undef rflag
68 1.1 cgd
69 1.1 cgd
70 1.1 cgd
71 1.1 cgd /*
72 1.35 dsl * The read builtin.
73 1.35 dsl * Backslahes escape the next char unless -r is specified.
74 1.1 cgd *
75 1.1 cgd * This uses unbuffered input, which may be avoidable in some cases.
76 1.35 dsl *
77 1.35 dsl * Note that if IFS=' :' then read x y should work so that:
78 1.35 dsl * 'a b' x='a', y='b'
79 1.35 dsl * ' a b ' x='a', y='b'
80 1.35 dsl * ':b' x='', y='b'
81 1.35 dsl * ':' x='', y=''
82 1.35 dsl * '::' x='', y=''
83 1.35 dsl * ': :' x='', y=''
84 1.35 dsl * ':::' x='', y='::'
85 1.35 dsl * ':b c:' x='', y='b c:'
86 1.1 cgd */
87 1.1 cgd
88 1.12 cgd int
89 1.31 christos readcmd(int argc, char **argv)
90 1.12 cgd {
91 1.1 cgd char **ap;
92 1.1 cgd char c;
93 1.20 kleink int rflag;
94 1.1 cgd char *prompt;
95 1.35 dsl const char *ifs;
96 1.1 cgd char *p;
97 1.1 cgd int startword;
98 1.1 cgd int status;
99 1.1 cgd int i;
100 1.35 dsl int is_ifs;
101 1.35 dsl int saveall = 0;
102 1.1 cgd
103 1.20 kleink rflag = 0;
104 1.1 cgd prompt = NULL;
105 1.20 kleink while ((i = nextopt("p:r")) != '\0') {
106 1.1 cgd if (i == 'p')
107 1.30 christos prompt = optionarg;
108 1.1 cgd else
109 1.20 kleink rflag = 1;
110 1.1 cgd }
111 1.35 dsl
112 1.1 cgd if (prompt && isatty(0)) {
113 1.1 cgd out2str(prompt);
114 1.1 cgd flushall();
115 1.1 cgd }
116 1.35 dsl
117 1.6 cgd if (*(ap = argptr) == NULL)
118 1.1 cgd error("arg count");
119 1.35 dsl
120 1.1 cgd if ((ifs = bltinlookup("IFS", 1)) == NULL)
121 1.35 dsl ifs = " \t\n";
122 1.35 dsl
123 1.1 cgd status = 0;
124 1.35 dsl startword = 2;
125 1.1 cgd STARTSTACKSTR(p);
126 1.1 cgd for (;;) {
127 1.1 cgd if (read(0, &c, 1) != 1) {
128 1.1 cgd status = 1;
129 1.1 cgd break;
130 1.1 cgd }
131 1.1 cgd if (c == '\0')
132 1.1 cgd continue;
133 1.35 dsl if (c == '\\' && !rflag) {
134 1.35 dsl if (read(0, &c, 1) != 1) {
135 1.35 dsl status = 1;
136 1.35 dsl break;
137 1.35 dsl }
138 1.1 cgd if (c != '\n')
139 1.1 cgd STPUTC(c, p);
140 1.1 cgd continue;
141 1.1 cgd }
142 1.1 cgd if (c == '\n')
143 1.1 cgd break;
144 1.35 dsl if (strchr(ifs, c))
145 1.35 dsl is_ifs = strchr(" \t\n", c) ? 1 : 2;
146 1.35 dsl else
147 1.35 dsl is_ifs = 0;
148 1.35 dsl
149 1.35 dsl if (startword != 0) {
150 1.35 dsl if (is_ifs == 1) {
151 1.35 dsl /* Ignore leading IFS whitespace */
152 1.35 dsl if (saveall)
153 1.35 dsl STPUTC(c, p);
154 1.35 dsl continue;
155 1.35 dsl }
156 1.35 dsl if (is_ifs == 2 && startword == 1) {
157 1.35 dsl /* Only one non-whitespace IFS per word */
158 1.35 dsl startword = 2;
159 1.35 dsl if (saveall)
160 1.35 dsl STPUTC(c, p);
161 1.35 dsl continue;
162 1.35 dsl }
163 1.35 dsl }
164 1.35 dsl
165 1.35 dsl if (is_ifs == 0) {
166 1.35 dsl /* append this character to the current variable */
167 1.35 dsl startword = 0;
168 1.35 dsl if (saveall)
169 1.35 dsl /* Not just a spare terminator */
170 1.35 dsl saveall++;
171 1.35 dsl STPUTC(c, p);
172 1.1 cgd continue;
173 1.1 cgd }
174 1.35 dsl
175 1.35 dsl /* end of variable... */
176 1.35 dsl startword = is_ifs;
177 1.35 dsl
178 1.35 dsl if (ap[1] == NULL) {
179 1.35 dsl /* Last variable needs all IFS chars */
180 1.35 dsl saveall++;
181 1.1 cgd STPUTC(c, p);
182 1.35 dsl continue;
183 1.1 cgd }
184 1.35 dsl
185 1.35 dsl STACKSTRNUL(p);
186 1.35 dsl setvar(*ap, stackblock(), 0);
187 1.35 dsl ap++;
188 1.35 dsl STARTSTACKSTR(p);
189 1.1 cgd }
190 1.1 cgd STACKSTRNUL(p);
191 1.35 dsl
192 1.35 dsl /* Remove trailing IFS chars */
193 1.35 dsl for (; stackblock() <= --p; *p = 0) {
194 1.35 dsl if (!strchr(ifs, *p))
195 1.35 dsl break;
196 1.35 dsl if (strchr(" \t\n", *p))
197 1.35 dsl /* Always remove whitespace */
198 1.35 dsl continue;
199 1.35 dsl if (saveall > 1)
200 1.35 dsl /* Don't remove non-whitespace unless it was naked */
201 1.35 dsl break;
202 1.35 dsl }
203 1.1 cgd setvar(*ap, stackblock(), 0);
204 1.35 dsl
205 1.35 dsl /* Set any remaining args to "" */
206 1.1 cgd while (*++ap != NULL)
207 1.1 cgd setvar(*ap, nullstr, 0);
208 1.1 cgd return status;
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd
212 1.1 cgd
213 1.12 cgd int
214 1.31 christos umaskcmd(int argc, char **argv)
215 1.12 cgd {
216 1.8 jtc char *ap;
217 1.1 cgd int mask;
218 1.1 cgd int i;
219 1.8 jtc int symbolic_mode = 0;
220 1.8 jtc
221 1.8 jtc while ((i = nextopt("S")) != '\0') {
222 1.8 jtc symbolic_mode = 1;
223 1.8 jtc }
224 1.1 cgd
225 1.8 jtc INTOFF;
226 1.8 jtc mask = umask(0);
227 1.8 jtc umask(mask);
228 1.8 jtc INTON;
229 1.8 jtc
230 1.8 jtc if ((ap = *argptr) == NULL) {
231 1.8 jtc if (symbolic_mode) {
232 1.8 jtc char u[4], g[4], o[4];
233 1.8 jtc
234 1.8 jtc i = 0;
235 1.8 jtc if ((mask & S_IRUSR) == 0)
236 1.8 jtc u[i++] = 'r';
237 1.8 jtc if ((mask & S_IWUSR) == 0)
238 1.8 jtc u[i++] = 'w';
239 1.8 jtc if ((mask & S_IXUSR) == 0)
240 1.8 jtc u[i++] = 'x';
241 1.8 jtc u[i] = '\0';
242 1.8 jtc
243 1.8 jtc i = 0;
244 1.8 jtc if ((mask & S_IRGRP) == 0)
245 1.8 jtc g[i++] = 'r';
246 1.8 jtc if ((mask & S_IWGRP) == 0)
247 1.8 jtc g[i++] = 'w';
248 1.8 jtc if ((mask & S_IXGRP) == 0)
249 1.8 jtc g[i++] = 'x';
250 1.8 jtc g[i] = '\0';
251 1.8 jtc
252 1.8 jtc i = 0;
253 1.8 jtc if ((mask & S_IROTH) == 0)
254 1.8 jtc o[i++] = 'r';
255 1.8 jtc if ((mask & S_IWOTH) == 0)
256 1.8 jtc o[i++] = 'w';
257 1.8 jtc if ((mask & S_IXOTH) == 0)
258 1.8 jtc o[i++] = 'x';
259 1.8 jtc o[i] = '\0';
260 1.8 jtc
261 1.8 jtc out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
262 1.8 jtc } else {
263 1.8 jtc out1fmt("%.4o\n", mask);
264 1.8 jtc }
265 1.1 cgd } else {
266 1.25 christos if (isdigit((unsigned char)*ap)) {
267 1.8 jtc mask = 0;
268 1.8 jtc do {
269 1.8 jtc if (*ap >= '8' || *ap < '0')
270 1.8 jtc error("Illegal number: %s", argv[1]);
271 1.8 jtc mask = (mask << 3) + (*ap - '0');
272 1.8 jtc } while (*++ap != '\0');
273 1.8 jtc umask(mask);
274 1.8 jtc } else {
275 1.16 christos void *set;
276 1.8 jtc
277 1.26 itohy INTOFF;
278 1.26 itohy if ((set = setmode(ap)) != 0) {
279 1.26 itohy mask = getmode(set, ~mask & 0777);
280 1.26 itohy ckfree(set);
281 1.26 itohy }
282 1.26 itohy INTON;
283 1.26 itohy if (!set)
284 1.36 christos error("Cannot set mode `%s' (%s)", ap,
285 1.36 christos strerror(errno));
286 1.26 itohy
287 1.8 jtc umask(~mask & 0777);
288 1.14 christos }
289 1.14 christos }
290 1.14 christos return 0;
291 1.14 christos }
292 1.14 christos
293 1.14 christos /*
294 1.14 christos * ulimit builtin
295 1.14 christos *
296 1.14 christos * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
297 1.14 christos * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
298 1.14 christos * ash by J.T. Conklin.
299 1.14 christos *
300 1.14 christos * Public domain.
301 1.14 christos */
302 1.14 christos
303 1.14 christos struct limits {
304 1.14 christos const char *name;
305 1.40 christos const char *unit;
306 1.14 christos int cmd;
307 1.14 christos int factor; /* multiply by to get rlim_{cur,max} values */
308 1.14 christos char option;
309 1.14 christos };
310 1.14 christos
311 1.14 christos static const struct limits limits[] = {
312 1.14 christos #ifdef RLIMIT_CPU
313 1.40 christos { "time", "seconds", RLIMIT_CPU, 1, 't' },
314 1.14 christos #endif
315 1.14 christos #ifdef RLIMIT_FSIZE
316 1.40 christos { "file", "blocks", RLIMIT_FSIZE, 512, 'f' },
317 1.14 christos #endif
318 1.14 christos #ifdef RLIMIT_DATA
319 1.40 christos { "data", "kbytes", RLIMIT_DATA, 1024, 'd' },
320 1.14 christos #endif
321 1.14 christos #ifdef RLIMIT_STACK
322 1.40 christos { "stack", "kbytes", RLIMIT_STACK, 1024, 's' },
323 1.14 christos #endif
324 1.14 christos #ifdef RLIMIT_CORE
325 1.40 christos { "coredump", "blocks", RLIMIT_CORE, 512, 'c' },
326 1.14 christos #endif
327 1.14 christos #ifdef RLIMIT_RSS
328 1.40 christos { "memory", "kbytes", RLIMIT_RSS, 1024, 'm' },
329 1.14 christos #endif
330 1.14 christos #ifdef RLIMIT_MEMLOCK
331 1.40 christos { "locked memory","kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
332 1.14 christos #endif
333 1.41 christos #ifdef RLIMIT_NTHR
334 1.41 christos { "thread", "threads", RLIMIT_NTHR, 1, 'r' },
335 1.41 christos #endif
336 1.14 christos #ifdef RLIMIT_NPROC
337 1.40 christos { "process", "processes", RLIMIT_NPROC, 1, 'p' },
338 1.14 christos #endif
339 1.14 christos #ifdef RLIMIT_NOFILE
340 1.40 christos { "nofiles", "descriptors", RLIMIT_NOFILE, 1, 'n' },
341 1.14 christos #endif
342 1.14 christos #ifdef RLIMIT_VMEM
343 1.40 christos { "vmemory", "kbytes", RLIMIT_VMEM, 1024, 'v' },
344 1.14 christos #endif
345 1.14 christos #ifdef RLIMIT_SWAP
346 1.40 christos { "swap", "kbytes", RLIMIT_SWAP, 1024, 'w' },
347 1.14 christos #endif
348 1.33 christos #ifdef RLIMIT_SBSIZE
349 1.40 christos { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
350 1.33 christos #endif
351 1.40 christos { NULL, NULL, 0, 0, '\0' }
352 1.14 christos };
353 1.14 christos
354 1.14 christos int
355 1.31 christos ulimitcmd(int argc, char **argv)
356 1.14 christos {
357 1.17 tls int c;
358 1.19 christos rlim_t val = 0;
359 1.14 christos enum { SOFT = 0x1, HARD = 0x2 }
360 1.14 christos how = SOFT | HARD;
361 1.14 christos const struct limits *l;
362 1.14 christos int set, all = 0;
363 1.14 christos int optc, what;
364 1.14 christos struct rlimit limit;
365 1.14 christos
366 1.14 christos what = 'f';
367 1.42 njoly while ((optc = nextopt("HSabtfdscmlrpnv")) != '\0')
368 1.14 christos switch (optc) {
369 1.14 christos case 'H':
370 1.14 christos how = HARD;
371 1.14 christos break;
372 1.14 christos case 'S':
373 1.14 christos how = SOFT;
374 1.14 christos break;
375 1.14 christos case 'a':
376 1.14 christos all = 1;
377 1.14 christos break;
378 1.14 christos default:
379 1.14 christos what = optc;
380 1.14 christos }
381 1.14 christos
382 1.14 christos for (l = limits; l->name && l->option != what; l++)
383 1.14 christos ;
384 1.14 christos if (!l->name)
385 1.28 christos error("internal error (%c)", what);
386 1.14 christos
387 1.14 christos set = *argptr ? 1 : 0;
388 1.14 christos if (set) {
389 1.14 christos char *p = *argptr;
390 1.14 christos
391 1.14 christos if (all || argptr[1])
392 1.28 christos error("too many arguments");
393 1.14 christos if (strcmp(p, "unlimited") == 0)
394 1.14 christos val = RLIM_INFINITY;
395 1.14 christos else {
396 1.15 jtc val = (rlim_t) 0;
397 1.14 christos
398 1.14 christos while ((c = *p++) >= '0' && c <= '9')
399 1.14 christos val = (val * 10) + (long)(c - '0');
400 1.14 christos if (c)
401 1.28 christos error("bad number");
402 1.14 christos val *= l->factor;
403 1.14 christos }
404 1.14 christos }
405 1.14 christos if (all) {
406 1.14 christos for (l = limits; l->name; l++) {
407 1.14 christos getrlimit(l->cmd, &limit);
408 1.14 christos if (how & SOFT)
409 1.14 christos val = limit.rlim_cur;
410 1.14 christos else if (how & HARD)
411 1.14 christos val = limit.rlim_max;
412 1.14 christos
413 1.40 christos out1fmt("%-13s (-%c %-11s) ", l->name, l->option,
414 1.40 christos l->unit);
415 1.14 christos if (val == RLIM_INFINITY)
416 1.14 christos out1fmt("unlimited\n");
417 1.14 christos else
418 1.14 christos {
419 1.14 christos val /= l->factor;
420 1.18 christos #ifdef BSD4_4
421 1.29 lukem out1fmt("%lld\n", (long long) val);
422 1.18 christos #else
423 1.18 christos out1fmt("%ld\n", (long) val);
424 1.18 christos #endif
425 1.14 christos }
426 1.14 christos }
427 1.14 christos return 0;
428 1.14 christos }
429 1.14 christos
430 1.14 christos getrlimit(l->cmd, &limit);
431 1.14 christos if (set) {
432 1.28 christos if (how & HARD)
433 1.28 christos limit.rlim_max = val;
434 1.14 christos if (how & SOFT)
435 1.14 christos limit.rlim_cur = val;
436 1.14 christos if (setrlimit(l->cmd, &limit) < 0)
437 1.28 christos error("error setting limit (%s)", strerror(errno));
438 1.14 christos } else {
439 1.14 christos if (how & SOFT)
440 1.14 christos val = limit.rlim_cur;
441 1.14 christos else if (how & HARD)
442 1.14 christos val = limit.rlim_max;
443 1.14 christos
444 1.14 christos if (val == RLIM_INFINITY)
445 1.14 christos out1fmt("unlimited\n");
446 1.14 christos else
447 1.14 christos {
448 1.14 christos val /= l->factor;
449 1.18 christos #ifdef BSD4_4
450 1.29 lukem out1fmt("%lld\n", (long long) val);
451 1.18 christos #else
452 1.18 christos out1fmt("%ld\n", (long) val);
453 1.18 christos #endif
454 1.8 jtc }
455 1.1 cgd }
456 1.1 cgd return 0;
457 1.1 cgd }
458