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. 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.
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
36 * @(#)miscbltin.c 8.4 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/miscbltin.c,v 1.46 2012/05/15 22:50:47 jilles Exp $
41 * Miscellaneous builtins.
44 #include <sys/types.h>
47 #include <sys/resource.h>
63 int readcmd(int, char **);
64 int umaskcmd(int, char **);
65 int ulimitcmd(int, char **);
70 * The read builtin. The -r option causes backslashes to be treated like
71 * ordinary characters.
73 * This uses unbuffered input, which may be avoidable in some cases.
75 * Note that if IFS=' :' then read x y should work so that:
77 * ' a b ' x='a', y='b'
83 * ':b c:' x='', y='b c:'
87 readcmd(int argc __unused, char **argv __unused)
109 while ((i = nextopt("erp:t:")) != '\0') {
120 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
121 if (tvptr == shoptarg)
122 error("timeout value");
134 error("timeout unit");
139 if (prompt && isatty(0)) {
143 if (*(ap = argptr) == NULL)
145 if ((ifs = bltinlookup("IFS", 1)) == NULL)
148 if (tv.tv_sec >= 0) {
150 * Wait for something to become available.
154 status = select(1, &ifds, NULL, NULL, &tv);
156 * If there's nothing ready, return an error.
167 if (read(STDIN_FILENO, &c, 1) != 1) {
181 if (!rflag && c == '\\') {
188 is_ifs = strchr(" \t\n", c) ? 1 : 2;
192 if (startword != 0) {
194 /* Ignore leading IFS whitespace */
199 if (is_ifs == 2 && startword == 1) {
200 /* Only one non-whitespace IFS per word */
209 /* append this character to the current variable */
212 /* Not just a spare terminator */
218 /* end of variable... */
222 /* Last variable needs all IFS chars */
229 setvar(*ap, stackblock(), 0);
235 /* Remove trailing IFS chars */
236 for (; stackblock() <= --p; *p = 0) {
237 if (!strchr(ifs, *p))
239 if (strchr(" \t\n", *p))
240 /* Always remove whitespace */
243 /* Don't remove non-whitespace unless it was naked */
246 setvar(*ap, stackblock(), 0);
248 /* Set any remaining args to "" */
249 while (*++ap != NULL)
250 setvar(*ap, nullstr, 0);
257 umaskcmd(int argc __unused, char **argv __unused)
262 int symbolic_mode = 0;
264 while ((i = nextopt("S")) != '\0') {
273 if ((ap = *argptr) == NULL) {
275 char u[4], g[4], o[4];
278 if ((mask & S_IRUSR) == 0)
280 if ((mask & S_IWUSR) == 0)
282 if ((mask & S_IXUSR) == 0)
287 if ((mask & S_IRGRP) == 0)
289 if ((mask & S_IWGRP) == 0)
291 if ((mask & S_IXGRP) == 0)
296 if ((mask & S_IROTH) == 0)
298 if ((mask & S_IWOTH) == 0)
300 if ((mask & S_IXOTH) == 0)
304 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
306 out1fmt("%.4o\n", mask);
312 if (*ap >= '8' || *ap < '0')
313 error("Illegal number: %s", *argptr);
314 mask = (mask << 3) + (*ap - '0');
315 } while (*++ap != '\0');
320 if ((set = setmode (ap)) == NULL)
321 error("Illegal number: %s", ap);
323 mask = getmode (set, ~mask & 0777);
335 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
336 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
337 * ash by J.T. Conklin.
346 int factor; /* multiply by to get rlim_{cur,max} values */
350 static const struct limits limits[] = {
352 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
355 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
358 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
361 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
364 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
367 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
369 #ifdef RLIMIT_MEMLOCK
370 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
373 { "max user processes", NULL, RLIMIT_NPROC, 1, 'u' },
376 { "open files", NULL, RLIMIT_NOFILE, 1, 'n' },
379 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
382 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
385 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
387 #ifdef RLIMIT_POSIXLOCK
388 { "posixlocks", NULL, RLIMIT_POSIXLOCK, 1, 'k' },
390 { NULL, NULL, 0, 0, '\0' }
394 ulimitcmd(int argc __unused, char **argv __unused)
398 enum { SOFT = 0x1, HARD = 0x2 }
400 const struct limits *l;
406 while ((optc = nextopt("HSatfdsmcnuvlbk")) != '\0')
421 for (l = limits; l->name && l->option != what; l++)
424 error("internal error (%c)", what);
426 set = *argptr ? 1 : 0;
430 if (all || argptr[1])
431 error("too many arguments");
432 if (strcmp(p, "unlimited") == 0)
437 while ((c = *p++) >= '0' && c <= '9')
439 val = (val * 10) + (long)(c - '0');
449 for (l = limits; l->name; l++) {
451 if (getrlimit(l->cmd, &limit) < 0)
452 error("can't get limit: %s", strerror(errno));
454 val = limit.rlim_cur;
456 val = limit.rlim_max;
459 snprintf(optbuf, sizeof(optbuf),
460 "(%s, -%c) ", l->units, l->option);
462 snprintf(optbuf, sizeof(optbuf),
463 "(-%c) ", l->option);
464 out1fmt("%-18s %18s ", l->name, optbuf);
465 if (val == RLIM_INFINITY)
466 out1str("unlimited\n");
470 out1fmt("%jd\n", (intmax_t)val);
476 if (getrlimit(l->cmd, &limit) < 0)
477 error("can't get limit: %s", strerror(errno));
480 limit.rlim_cur = val;
482 limit.rlim_max = val;
483 if (setrlimit(l->cmd, &limit) < 0)
484 error("bad limit: %s", strerror(errno));
487 val = limit.rlim_cur;
489 val = limit.rlim_max;
491 if (val == RLIM_INFINITY)
492 out1str("unlimited\n");
496 out1fmt("%jd\n", (intmax_t)val);