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.
79 readcmd(int argc __unused, char **argv __unused)
94 struct termios told, tnew;
101 while ((i = nextopt("erp:t:")) != '\0') {
112 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
113 if (tvptr == shoptarg)
114 error("timeout value");
126 error("timeout unit");
131 if (prompt && isatty(0)) {
135 if (*(ap = argptr) == NULL)
137 if ((ifs = bltinlookup("IFS", 1)) == NULL)
140 if (tv.tv_sec >= 0) {
142 * See if we can disable input processing; this will
143 * not give the desired result if we are in a pipeline
144 * and someone upstream is still in line-by-line mode.
147 if (tcgetattr(0, &told) == 0) {
148 memcpy(&tnew, &told, sizeof(told));
150 tnew.c_iflag |= told.c_iflag & ICRNL;
151 tcsetattr(0, TCSANOW, &tnew);
155 * Wait for something to become available.
159 status = select(1, &ifds, NULL, NULL, &tv);
161 tcsetattr(0, TCSANOW, &told);
163 * If there's nothing ready, return an error.
174 if (read(STDIN_FILENO, &c, 1) != 1) {
186 if (!rflag && c == '\\') {
192 if (startword && *ifs == ' ' && strchr(ifs, c)) {
196 if (ap[1] != NULL && strchr(ifs, c) != NULL) {
198 setvar(*ap, stackblock(), 0);
207 setvar(*ap, stackblock(), 0);
208 while (*++ap != NULL)
209 setvar(*ap, nullstr, 0);
216 umaskcmd(int argc __unused, char **argv)
221 int symbolic_mode = 0;
223 while ((i = nextopt("S")) != '\0') {
232 if ((ap = *argptr) == NULL) {
234 char u[4], g[4], o[4];
237 if ((mask & S_IRUSR) == 0)
239 if ((mask & S_IWUSR) == 0)
241 if ((mask & S_IXUSR) == 0)
246 if ((mask & S_IRGRP) == 0)
248 if ((mask & S_IWGRP) == 0)
250 if ((mask & S_IXGRP) == 0)
255 if ((mask & S_IROTH) == 0)
257 if ((mask & S_IWOTH) == 0)
259 if ((mask & S_IXOTH) == 0)
263 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
265 out1fmt("%.4o\n", mask);
271 if (*ap >= '8' || *ap < '0')
272 error("Illegal number: %s", *argptr);
273 mask = (mask << 3) + (*ap - '0');
274 } while (*++ap != '\0');
279 if ((set = setmode (ap)) == 0)
280 error("Illegal number: %s", ap);
282 mask = getmode (set, ~mask & 0777);
294 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
295 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
296 * ash by J.T. Conklin.
305 int factor; /* multiply by to get rlim_{cur,max} values */
309 static const struct limits limits[] = {
311 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
314 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
317 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
320 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
323 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
326 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
328 #ifdef RLIMIT_MEMLOCK
329 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
332 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
335 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
338 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
341 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
344 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
346 { (char *) 0, (char *)0, 0, 0, '\0' }
350 ulimitcmd(int argc __unused, char **argv __unused)
354 enum { SOFT = 0x1, HARD = 0x2 }
356 const struct limits *l;
362 while ((optc = nextopt("HSatfdsmcnuvlb")) != '\0')
377 for (l = limits; l->name && l->option != what; l++)
380 error("internal error (%c)", what);
382 set = *argptr ? 1 : 0;
386 if (all || argptr[1])
387 error("too many arguments");
388 if (strcmp(p, "unlimited") == 0)
393 while ((c = *p++) >= '0' && c <= '9')
395 val = (val * 10) + (long)(c - '0');
405 for (l = limits; l->name; l++) {
407 if (getrlimit(l->cmd, &limit) < 0)
408 error("can't get limit: %s", strerror(errno));
410 val = limit.rlim_cur;
412 val = limit.rlim_max;
415 snprintf(optbuf, sizeof(optbuf),
416 "(%s, -%c) ", l->units, l->option);
418 snprintf(optbuf, sizeof(optbuf),
419 "(-%c) ", l->option);
420 out1fmt("%-18s %18s ", l->name, optbuf);
421 if (val == RLIM_INFINITY)
422 out1fmt("unlimited\n");
426 out1fmt("%jd\n", (intmax_t)val);
432 if (getrlimit(l->cmd, &limit) < 0)
433 error("can't get limit: %s", strerror(errno));
436 limit.rlim_cur = val;
438 limit.rlim_max = val;
439 if (setrlimit(l->cmd, &limit) < 0)
440 error("bad limit: %s", strerror(errno));
443 val = limit.rlim_cur;
445 val = limit.rlim_max;
447 if (val == RLIM_INFINITY)
448 out1fmt("unlimited\n");
452 out1fmt("%jd\n", (intmax_t)val);