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.35 2006/02/04 14:37:50 schweikh Exp $
38 * $DragonFly: src/bin/sh/miscbltin.c,v 1.5 2007/01/13 22:39:30 pavalos Exp $
42 * Miscellaneous builtins.
45 #include <sys/types.h>
48 #include <sys/resource.h>
65 int readcmd(int, char **);
66 int umaskcmd(int, char **);
67 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 tcsetattr(0, TCSANOW, &tnew);
154 * Wait for something to become available.
158 status = select(1, &ifds, NULL, NULL, &tv);
160 tcsetattr(0, TCSANOW, &told);
162 * If there's nothing ready, return an error.
173 if (read(STDIN_FILENO, &c, 1) != 1) {
185 if (!rflag && c == '\\') {
191 if (startword && *ifs == ' ' && strchr(ifs, c)) {
195 if (ap[1] != NULL && strchr(ifs, c) != NULL) {
197 setvar(*ap, stackblock(), 0);
206 setvar(*ap, stackblock(), 0);
207 while (*++ap != NULL)
208 setvar(*ap, nullstr, 0);
215 umaskcmd(int argc __unused, char **argv __unused)
220 int symbolic_mode = 0;
222 while ((i = nextopt("S")) != '\0') {
231 if ((ap = *argptr) == NULL) {
233 char u[4], g[4], o[4];
236 if ((mask & S_IRUSR) == 0)
238 if ((mask & S_IWUSR) == 0)
240 if ((mask & S_IXUSR) == 0)
245 if ((mask & S_IRGRP) == 0)
247 if ((mask & S_IWGRP) == 0)
249 if ((mask & S_IXGRP) == 0)
254 if ((mask & S_IROTH) == 0)
256 if ((mask & S_IWOTH) == 0)
258 if ((mask & S_IXOTH) == 0)
262 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
264 out1fmt("%.4o\n", mask);
270 if (*ap >= '8' || *ap < '0')
271 error("Illegal number: %s", *argptr);
272 mask = (mask << 3) + (*ap - '0');
273 } while (*++ap != '\0');
278 if ((set = setmode (ap)) == 0)
279 error("Illegal number: %s", ap);
281 mask = getmode (set, ~mask & 0777);
293 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
294 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
295 * ash by J.T. Conklin.
304 int factor; /* multiply by to get rlim_{cur,max} values */
308 static const struct limits limits[] = {
310 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
313 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
316 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
319 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
322 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
325 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
327 #ifdef RLIMIT_MEMLOCK
328 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
331 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
334 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
337 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
340 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
343 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
345 #ifdef RLIMIT_POSIXLOCK
346 { "posixlocks", (char *)0, RLIMIT_POSIXLOCK, 1, 'k' },
348 { (char *) 0, (char *)0, 0, 0, '\0' }
352 ulimitcmd(int argc __unused, char **argv __unused)
356 enum { SOFT = 0x1, HARD = 0x2 }
358 const struct limits *l;
364 while ((optc = nextopt("HSatfdsmcnuvlbk")) != '\0')
379 for (l = limits; l->name && l->option != what; l++)
382 error("internal error (%c)", what);
384 set = *argptr ? 1 : 0;
388 if (all || argptr[1])
389 error("too many arguments");
390 if (strcmp(p, "unlimited") == 0)
395 while ((c = *p++) >= '0' && c <= '9')
397 val = (val * 10) + (long)(c - '0');
407 for (l = limits; l->name; l++) {
409 if (getrlimit(l->cmd, &limit) < 0)
410 error("can't get limit: %s", strerror(errno));
412 val = limit.rlim_cur;
414 val = limit.rlim_max;
417 snprintf(optbuf, sizeof(optbuf),
418 "(%s, -%c) ", l->units, l->option);
420 snprintf(optbuf, sizeof(optbuf),
421 "(-%c) ", l->option);
422 out1fmt("%-18s %18s ", l->name, optbuf);
423 if (val == RLIM_INFINITY)
424 out1fmt("unlimited\n");
428 out1fmt("%jd\n", (intmax_t)val);
434 if (getrlimit(l->cmd, &limit) < 0)
435 error("can't get limit: %s", strerror(errno));
438 limit.rlim_cur = val;
440 limit.rlim_max = val;
441 if (setrlimit(l->cmd, &limit) < 0)
442 error("bad limit: %s", strerror(errno));
445 val = limit.rlim_cur;
447 val = limit.rlim_max;
449 if (val == RLIM_INFINITY)
450 out1fmt("unlimited\n");
454 out1fmt("%jd\n", (intmax_t)val);