2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Miscellaneous builtins.
45 #include <sys/types.h>
48 #include <sys/resource.h>
67 int readcmd(int, char **);
68 int umaskcmd(int, char **);
69 int ulimitcmd(int, char **);
72 * The read builtin. The -r option causes backslashes to be treated like
73 * ordinary characters.
75 * This uses unbuffered input, which may be avoidable in some cases.
77 * Note that if IFS=' :' then read x y should work so that:
79 * ' a b ' x='a', y='b'
85 * ':b c:' x='', y='b c:'
89 readcmd(int argc __unused, char **argv __unused)
113 while ((i = nextopt("erp:t:")) != '\0') {
124 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
125 if (tvptr == shoptarg)
126 error("timeout value");
138 error("timeout unit");
143 if (prompt && isatty(0)) {
147 if (*(ap = argptr) == NULL)
149 if ((ifs = bltinlookup("IFS", 1)) == NULL)
152 if (tv.tv_sec >= 0) {
154 * Wait for something to become available.
158 status = select(1, &ifds, NULL, NULL, &tv);
160 * If there's nothing ready, return an error.
164 return (128 + (sig != 0 ? sig : SIGALRM));
173 nread = read(STDIN_FILENO, &c, 1);
175 if (errno == EINTR) {
182 warning("read error: %s", strerror(errno));
185 } else if (nread != 1) {
199 if (!rflag && c == '\\') {
206 is_ifs = strchr(" \t\n", c) ? 1 : 2;
210 if (startword != 0) {
212 /* Ignore leading IFS whitespace */
217 if (is_ifs == 2 && startword == 1) {
218 /* Only one non-whitespace IFS per word */
227 /* append this character to the current variable */
230 /* Not just a spare terminator */
236 /* end of variable... */
240 /* Last variable needs all IFS chars */
247 setvar(*ap, stackblock(), 0);
253 /* Remove trailing IFS chars */
254 for (; stackblock() <= --p; *p = 0) {
255 if (!strchr(ifs, *p))
257 if (strchr(" \t\n", *p))
258 /* Always remove whitespace */
261 /* Don't remove non-whitespace unless it was naked */
264 setvar(*ap, stackblock(), 0);
266 /* Set any remaining args to "" */
267 while (*++ap != NULL)
275 umaskcmd(int argc __unused, char **argv __unused)
280 int symbolic_mode = 0;
282 while ((i = nextopt("S")) != '\0') {
291 if ((ap = *argptr) == NULL) {
293 char u[4], g[4], o[4];
296 if ((mask & S_IRUSR) == 0)
298 if ((mask & S_IWUSR) == 0)
300 if ((mask & S_IXUSR) == 0)
305 if ((mask & S_IRGRP) == 0)
307 if ((mask & S_IWGRP) == 0)
309 if ((mask & S_IXGRP) == 0)
314 if ((mask & S_IROTH) == 0)
316 if ((mask & S_IWOTH) == 0)
318 if ((mask & S_IXOTH) == 0)
322 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
324 out1fmt("%.4o\n", mask);
330 if (*ap >= '8' || *ap < '0')
331 error("Illegal number: %s", *argptr);
332 mask = (mask << 3) + (*ap - '0');
333 } while (*++ap != '\0');
338 if ((set = setmode (ap)) == 0)
339 error("Illegal number: %s", ap);
341 mask = getmode (set, ~mask & 0777);
353 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
354 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
355 * ash by J.T. Conklin.
364 int factor; /* multiply by to get rlim_{cur,max} values */
368 static const struct limits limits[] = {
370 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
373 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
376 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
379 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
382 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
385 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
387 #ifdef RLIMIT_MEMLOCK
388 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
391 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
394 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
397 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
400 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
403 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
406 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' },
408 #ifdef RLIMIT_KQUEUES
409 { "kqueues", (char *)0, RLIMIT_KQUEUES, 1, 'k' },
411 { (char *) 0, (char *)0, 0, 0, '\0' }
414 enum limithow { SOFT = 0x1, HARD = 0x2 };
417 printlimit(enum limithow how, const struct rlimit *limit,
418 const struct limits *l)
423 val = limit->rlim_cur;
425 val = limit->rlim_max;
426 if (val == RLIM_INFINITY)
427 out1str("unlimited\n");
431 out1fmt("%jd\n", (intmax_t)val);
436 ulimitcmd(int argc __unused, char **argv __unused)
439 enum limithow how = SOFT | HARD;
440 const struct limits *l;
446 while ((optc = nextopt("HSatfdsmcnuvlbpwk")) != '\0')
461 for (l = limits; l->name && l->option != what; l++)
464 error("internal error (%c)", what);
466 set = *argptr ? 1 : 0;
470 if (all || argptr[1])
471 error("too many arguments");
472 if (strcmp(p, "unlimited") == 0)
478 if (*p < '0' || *p > '9')
481 uval = strtoumax(p, &end, 10);
482 if (errno != 0 || *end != '\0')
484 if (uval > UINTMAX_MAX / l->factor)
488 if (val < 0 || (uintmax_t)val != uval ||
489 val == RLIM_INFINITY)
494 for (l = limits; l->name; l++) {
496 if (getrlimit(l->cmd, &limit) < 0)
497 error("can't get limit: %s", strerror(errno));
500 snprintf(optbuf, sizeof(optbuf),
501 "(%s, -%c) ", l->units, l->option);
503 snprintf(optbuf, sizeof(optbuf),
504 "(-%c) ", l->option);
505 out1fmt("%-18s %18s ", l->name, optbuf);
506 printlimit(how, &limit, l);
511 if (getrlimit(l->cmd, &limit) < 0)
512 error("can't get limit: %s", strerror(errno));
515 limit.rlim_cur = val;
517 limit.rlim_max = val;
518 if (setrlimit(l->cmd, &limit) < 0)
519 error("bad limit: %s", strerror(errno));
521 printlimit(how, &limit, l);