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
32 * @(#)miscbltin.c 8.4 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/miscbltin.c 250214 2013-05-03 15:28:31Z jilles $
37 * Miscellaneous builtins.
40 #include <sys/types.h>
43 #include <sys/resource.h>
60 int readcmd(int, char **);
61 int umaskcmd(int, char **);
62 int ulimitcmd(int, char **);
67 * The read builtin. The -r option causes backslashes to be treated like
68 * ordinary characters.
70 * This uses unbuffered input, which may be avoidable in some cases.
72 * Note that if IFS=' :' then read x y should work so that:
74 * ' a b ' x='a', y='b'
80 * ':b c:' x='', y='b c:'
84 readcmd(int argc __unused, char **argv __unused)
108 while ((i = nextopt("erp:t:")) != '\0') {
119 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
120 if (tvptr == shoptarg)
121 error("timeout value");
133 error("timeout unit");
138 if (prompt && isatty(0)) {
142 if (*(ap = argptr) == NULL)
144 if ((ifs = bltinlookup("IFS", 1)) == NULL)
147 if (tv.tv_sec >= 0) {
149 * Wait for something to become available.
153 status = select(1, &ifds, NULL, NULL, &tv);
155 * If there's nothing ready, return an error.
159 return (128 + (sig != 0 ? sig : SIGALRM));
168 nread = read(STDIN_FILENO, &c, 1);
170 if (errno == EINTR) {
177 warning("read error: %s", strerror(errno));
180 } else if (nread != 1) {
194 if (!rflag && c == '\\') {
201 is_ifs = strchr(" \t\n", c) ? 1 : 2;
205 if (startword != 0) {
207 /* Ignore leading IFS whitespace */
212 if (is_ifs == 2 && startword == 1) {
213 /* Only one non-whitespace IFS per word */
222 /* append this character to the current variable */
225 /* Not just a spare terminator */
231 /* end of variable... */
235 /* Last variable needs all IFS chars */
242 setvar(*ap, stackblock(), 0);
248 /* Remove trailing IFS chars */
249 for (; stackblock() <= --p; *p = 0) {
250 if (!strchr(ifs, *p))
252 if (strchr(" \t\n", *p))
253 /* Always remove whitespace */
256 /* Don't remove non-whitespace unless it was naked */
259 setvar(*ap, stackblock(), 0);
261 /* Set any remaining args to "" */
262 while (*++ap != NULL)
263 setvar(*ap, nullstr, 0);
270 umaskcmd(int argc __unused, char **argv __unused)
275 int symbolic_mode = 0;
277 while ((i = nextopt("S")) != '\0') {
286 if ((ap = *argptr) == NULL) {
288 char u[4], g[4], o[4];
291 if ((mask & S_IRUSR) == 0)
293 if ((mask & S_IWUSR) == 0)
295 if ((mask & S_IXUSR) == 0)
300 if ((mask & S_IRGRP) == 0)
302 if ((mask & S_IWGRP) == 0)
304 if ((mask & S_IXGRP) == 0)
309 if ((mask & S_IROTH) == 0)
311 if ((mask & S_IWOTH) == 0)
313 if ((mask & S_IXOTH) == 0)
317 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
319 out1fmt("%.4o\n", mask);
325 if (*ap >= '8' || *ap < '0')
326 error("Illegal number: %s", *argptr);
327 mask = (mask << 3) + (*ap - '0');
328 } while (*++ap != '\0');
333 if ((set = setmode (ap)) == NULL)
334 error("Illegal number: %s", ap);
336 mask = getmode (set, ~mask & 0777);
348 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
349 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
350 * ash by J.T. Conklin.
359 int factor; /* multiply by to get rlim_{cur,max} values */
363 static const struct limits limits[] = {
365 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
368 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
371 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
374 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
377 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
380 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
382 #ifdef RLIMIT_MEMLOCK
383 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
386 { "max user processes", NULL, RLIMIT_NPROC, 1, 'u' },
389 { "open files", NULL, RLIMIT_NOFILE, 1, 'n' },
392 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
395 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
398 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
400 #ifdef RLIMIT_POSIXLOCK
401 { "posixlocks", NULL, RLIMIT_POSIXLOCK, 1, 'k' },
403 { NULL, NULL, 0, 0, '\0' }
407 ulimitcmd(int argc __unused, char **argv __unused)
411 enum { SOFT = 0x1, HARD = 0x2 }
413 const struct limits *l;
419 while ((optc = nextopt("HSatfdsmcnuvlbk")) != '\0')
434 for (l = limits; l->name && l->option != what; l++)
437 error("internal error (%c)", what);
439 set = *argptr ? 1 : 0;
443 if (all || argptr[1])
444 error("too many arguments");
445 if (strcmp(p, "unlimited") == 0)
450 while ((c = *p++) >= '0' && c <= '9')
452 val = (val * 10) + (long)(c - '0');
462 for (l = limits; l->name; l++) {
464 if (getrlimit(l->cmd, &limit) < 0)
465 error("can't get limit: %s", strerror(errno));
467 val = limit.rlim_cur;
469 val = limit.rlim_max;
472 snprintf(optbuf, sizeof(optbuf),
473 "(%s, -%c) ", l->units, l->option);
475 snprintf(optbuf, sizeof(optbuf),
476 "(-%c) ", l->option);
477 out1fmt("%-18s %18s ", l->name, optbuf);
478 if (val == RLIM_INFINITY)
479 out1str("unlimited\n");
483 out1fmt("%jd\n", (intmax_t)val);
489 if (getrlimit(l->cmd, &limit) < 0)
490 error("can't get limit: %s", strerror(errno));
493 limit.rlim_cur = val;
495 limit.rlim_max = val;
496 if (setrlimit(l->cmd, &limit) < 0)
497 error("bad limit: %s", strerror(errno));
500 val = limit.rlim_cur;
502 val = limit.rlim_max;
504 if (val == RLIM_INFINITY)
505 out1str("unlimited\n");
509 out1fmt("%jd\n", (intmax_t)val);