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 * 4. 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)
111 while ((i = nextopt("erp:t:")) != '\0') {
122 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
123 if (tvptr == shoptarg)
124 error("timeout value");
136 error("timeout unit");
141 if (prompt && isatty(0)) {
145 if (*(ap = argptr) == NULL)
147 if ((ifs = bltinlookup("IFS", 1)) == NULL)
150 if (tv.tv_sec >= 0) {
152 * Wait for something to become available.
156 status = select(1, &ifds, NULL, NULL, &tv);
158 * If there's nothing ready, return an error.
169 if (read(STDIN_FILENO, &c, 1) != 1) {
182 if (!rflag && c == '\\') {
189 is_ifs = strchr(" \t\n", c) ? 1 : 2;
193 if (startword != 0) {
195 /* Ignore leading IFS whitespace */
200 if (is_ifs == 2 && startword == 1) {
201 /* Only one non-whitespace IFS per word */
210 /* append this character to the current variable */
213 /* Not just a spare terminator */
219 /* end of variable... */
223 /* Last variable needs all IFS chars */
230 setvar(*ap, stackblock(), 0);
236 /* Remove trailing IFS chars */
237 for (; stackblock() <= --p; *p = 0) {
238 if (!strchr(ifs, *p))
240 if (strchr(" \t\n", *p))
241 /* Always remove whitespace */
244 /* Don't remove non-whitespace unless it was naked */
247 setvar(*ap, stackblock(), 0);
249 /* Set any remaining args to "" */
250 while (*++ap != NULL)
251 setvar(*ap, nullstr, 0);
258 umaskcmd(int argc __unused, char **argv __unused)
263 int symbolic_mode = 0;
265 while ((i = nextopt("S")) != '\0') {
274 if ((ap = *argptr) == NULL) {
276 char u[4], g[4], o[4];
279 if ((mask & S_IRUSR) == 0)
281 if ((mask & S_IWUSR) == 0)
283 if ((mask & S_IXUSR) == 0)
288 if ((mask & S_IRGRP) == 0)
290 if ((mask & S_IWGRP) == 0)
292 if ((mask & S_IXGRP) == 0)
297 if ((mask & S_IROTH) == 0)
299 if ((mask & S_IWOTH) == 0)
301 if ((mask & S_IXOTH) == 0)
305 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
307 out1fmt("%.4o\n", mask);
313 if (*ap >= '8' || *ap < '0')
314 error("Illegal number: %s", *argptr);
315 mask = (mask << 3) + (*ap - '0');
316 } while (*++ap != '\0');
321 if ((set = setmode (ap)) == 0)
322 error("Illegal number: %s", ap);
324 mask = getmode (set, ~mask & 0777);
336 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
337 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
338 * ash by J.T. Conklin.
347 int factor; /* multiply by to get rlim_{cur,max} values */
351 static const struct limits limits[] = {
353 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
356 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
359 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
362 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
365 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
368 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
370 #ifdef RLIMIT_MEMLOCK
371 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
374 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
377 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
380 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
383 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
386 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
389 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' },
391 { (char *) 0, (char *)0, 0, 0, '\0' }
395 ulimitcmd(int argc __unused, char **argv __unused)
399 enum { SOFT = 0x1, HARD = 0x2 }
401 const struct limits *l;
407 while ((optc = nextopt("HSatfdsmcnuvlbpw")) != '\0')
422 for (l = limits; l->name && l->option != what; l++)
425 error("internal error (%c)", what);
427 set = *argptr ? 1 : 0;
431 if (all || argptr[1])
432 error("too many arguments");
433 if (strcmp(p, "unlimited") == 0)
438 while ((c = *p++) >= '0' && c <= '9')
440 val = (val * 10) + (long)(c - '0');
450 for (l = limits; l->name; l++) {
452 if (getrlimit(l->cmd, &limit) < 0)
453 error("can't get limit: %s", strerror(errno));
455 val = limit.rlim_cur;
457 val = limit.rlim_max;
460 snprintf(optbuf, sizeof(optbuf),
461 "(%s, -%c) ", l->units, l->option);
463 snprintf(optbuf, sizeof(optbuf),
464 "(-%c) ", l->option);
465 out1fmt("%-18s %18s ", l->name, optbuf);
466 if (val == RLIM_INFINITY)
467 out1fmt("unlimited\n");
471 out1fmt("%jd\n", (intmax_t)val);
477 if (getrlimit(l->cmd, &limit) < 0)
478 error("can't get limit: %s", strerror(errno));
481 limit.rlim_cur = val;
483 limit.rlim_max = val;
484 if (setrlimit(l->cmd, &limit) < 0)
485 error("bad limit: %s", strerror(errno));
488 val = limit.rlim_cur;
490 val = limit.rlim_max;
492 if (val == RLIM_INFINITY)
493 out1fmt("unlimited\n");
497 out1fmt("%jd\n", (intmax_t)val);