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.22.2.3 2002/07/19 04:38:51 tjr Exp $
38 * $DragonFly: src/bin/sh/miscbltin.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
42 * Miscellaneous builtins.
45 #include <sys/types.h>
48 #include <sys/resource.h>
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.
74 readcmd(int argc __unused, char **argv __unused)
89 struct termios told, tnew;
96 while ((i = nextopt("erp:t:")) != '\0') {
107 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
108 if (tvptr == shoptarg)
109 error("timeout value");
121 error("timeout unit");
126 if (prompt && isatty(0)) {
130 if (*(ap = argptr) == NULL)
132 if ((ifs = bltinlookup("IFS", 1)) == NULL)
135 if (tv.tv_sec >= 0) {
137 * See if we can disable input processing; this will
138 * not give the desired result if we are in a pipeline
139 * and someone upstream is still in line-by-line mode.
142 if (tcgetattr(0, &told) == 0) {
143 memcpy(&tnew, &told, sizeof(told));
145 tcsetattr(0, TCSANOW, &tnew);
149 * Wait for something to become available.
153 status = select(1, &ifds, NULL, NULL, &tv);
155 tcsetattr(0, TCSANOW, &told);
157 * If there's nothing ready, return an error.
168 if (read(STDIN_FILENO, &c, 1) != 1) {
180 if (!rflag && c == '\\') {
186 if (startword && *ifs == ' ' && strchr(ifs, c)) {
190 if (backslash && c == '\\') {
191 if (read(STDIN_FILENO, &c, 1) != 1) {
196 } else 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", argv[1]);
273 mask = (mask << 3) + (*ap - '0');
274 } while (*++ap != '\0');
278 if ((set = setmode (ap)) == 0)
279 error("Illegal number: %s", ap);
281 mask = getmode (set, ~mask & 0777);
292 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
293 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
294 * ash by J.T. Conklin.
303 int factor; /* multiply by to get rlim_{cur,max} values */
307 static const struct limits limits[] = {
309 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
312 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
315 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
318 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
321 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
324 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
326 #ifdef RLIMIT_MEMLOCK
327 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
330 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
333 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
336 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
339 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
342 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
344 { (char *) 0, (char *)0, 0, 0, '\0' }
348 ulimitcmd(int argc __unused, char **argv __unused)
352 enum { SOFT = 0x1, HARD = 0x2 }
354 const struct limits *l;
360 while ((optc = nextopt("HSatfdsmcnuvlb")) != '\0')
375 for (l = limits; l->name && l->option != what; l++)
378 error("ulimit: internal error (%c)", what);
380 set = *argptr ? 1 : 0;
384 if (all || argptr[1])
385 error("ulimit: too many arguments");
386 if (strcmp(p, "unlimited") == 0)
391 while ((c = *p++) >= '0' && c <= '9')
393 val = (val * 10) + (long)(c - '0');
394 if (val < (quad_t) 0)
398 error("ulimit: bad number");
403 for (l = limits; l->name; l++) {
405 if (getrlimit(l->cmd, &limit) < 0)
406 error("ulimit: can't get limit: %s", strerror(errno));
408 val = limit.rlim_cur;
410 val = limit.rlim_max;
413 snprintf(optbuf, sizeof(optbuf),
414 "(%s, -%c) ", l->units, l->option);
416 snprintf(optbuf, sizeof(optbuf),
417 "(-%c) ", l->option);
418 out1fmt("%-18s %18s ", l->name, optbuf);
419 if (val == RLIM_INFINITY)
420 out1fmt("unlimited\n");
424 out1fmt("%qd\n", (quad_t) val);
430 if (getrlimit(l->cmd, &limit) < 0)
431 error("ulimit: can't get limit: %s", strerror(errno));
434 limit.rlim_cur = val;
436 limit.rlim_max = val;
437 if (setrlimit(l->cmd, &limit) < 0)
438 error("ulimit: bad limit: %s", strerror(errno));
441 val = limit.rlim_cur;
443 val = limit.rlim_max;
445 if (val == RLIM_INFINITY)
446 out1fmt("unlimited\n");
450 out1fmt("%qd\n", (quad_t) val);