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.4 2004/04/22 16:52:53 dillon Exp $
42 * Miscellaneous builtins.
45 #include <sys/types.h>
48 #include <sys/resource.h>
64 int readcmd(int, char **);
65 int umaskcmd(int, char **);
66 int ulimitcmd(int, char **);
71 * The read builtin. The -r option causes backslashes to be treated like
72 * ordinary characters.
74 * This uses unbuffered input, which may be avoidable in some cases.
78 readcmd(int argc __unused, char **argv __unused)
93 struct termios told, tnew;
100 while ((i = nextopt("erp:t:")) != '\0') {
111 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
112 if (tvptr == shoptarg)
113 error("timeout value");
125 error("timeout unit");
130 if (prompt && isatty(0)) {
134 if (*(ap = argptr) == NULL)
136 if ((ifs = bltinlookup("IFS", 1)) == NULL)
139 if (tv.tv_sec >= 0) {
141 * See if we can disable input processing; this will
142 * not give the desired result if we are in a pipeline
143 * and someone upstream is still in line-by-line mode.
146 if (tcgetattr(0, &told) == 0) {
147 memcpy(&tnew, &told, sizeof(told));
149 tcsetattr(0, TCSANOW, &tnew);
153 * Wait for something to become available.
157 status = select(1, &ifds, NULL, NULL, &tv);
159 tcsetattr(0, TCSANOW, &told);
161 * If there's nothing ready, return an error.
172 if (read(STDIN_FILENO, &c, 1) != 1) {
184 if (!rflag && c == '\\') {
190 if (startword && *ifs == ' ' && strchr(ifs, c)) {
194 if (backslash && c == '\\') {
195 if (read(STDIN_FILENO, &c, 1) != 1) {
200 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
202 setvar(*ap, stackblock(), 0);
211 setvar(*ap, stackblock(), 0);
212 while (*++ap != NULL)
213 setvar(*ap, nullstr, 0);
220 umaskcmd(int argc __unused, char **argv)
225 int symbolic_mode = 0;
227 while ((i = nextopt("S")) != '\0') {
236 if ((ap = *argptr) == NULL) {
238 char u[4], g[4], o[4];
241 if ((mask & S_IRUSR) == 0)
243 if ((mask & S_IWUSR) == 0)
245 if ((mask & S_IXUSR) == 0)
250 if ((mask & S_IRGRP) == 0)
252 if ((mask & S_IWGRP) == 0)
254 if ((mask & S_IXGRP) == 0)
259 if ((mask & S_IROTH) == 0)
261 if ((mask & S_IWOTH) == 0)
263 if ((mask & S_IXOTH) == 0)
267 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
269 out1fmt("%.4o\n", mask);
275 if (*ap >= '8' || *ap < '0')
276 error("Illegal number: %s", argv[1]);
277 mask = (mask << 3) + (*ap - '0');
278 } while (*++ap != '\0');
282 if ((set = setmode (ap)) == 0)
283 error("Illegal number: %s", ap);
285 mask = getmode (set, ~mask & 0777);
296 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
297 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
298 * ash by J.T. Conklin.
307 int factor; /* multiply by to get rlim_{cur,max} values */
311 static const struct limits limits[] = {
313 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
316 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
319 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
322 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
325 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
328 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
330 #ifdef RLIMIT_MEMLOCK
331 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
334 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
337 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
340 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
343 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
346 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
348 #ifdef RLIMIT_POSIXLOCK
349 { "posixlocks", (char *)0, RLIMIT_POSIXLOCK, 1, 'k' },
351 { (char *) 0, (char *)0, 0, 0, '\0' }
355 ulimitcmd(int argc __unused, char **argv __unused)
359 enum { SOFT = 0x1, HARD = 0x2 }
361 const struct limits *l;
367 while ((optc = nextopt("HSatfdsmcnuvlbk")) != '\0')
382 for (l = limits; l->name && l->option != what; l++)
385 error("ulimit: internal error (%c)", what);
387 set = *argptr ? 1 : 0;
391 if (all || argptr[1])
392 error("ulimit: too many arguments");
393 if (strcmp(p, "unlimited") == 0)
398 while ((c = *p++) >= '0' && c <= '9')
400 val = (val * 10) + (long)(c - '0');
401 if (val < (quad_t) 0)
405 error("ulimit: bad number");
410 for (l = limits; l->name; l++) {
412 if (getrlimit(l->cmd, &limit) < 0)
413 error("ulimit: can't get limit: %s", strerror(errno));
415 val = limit.rlim_cur;
417 val = limit.rlim_max;
420 snprintf(optbuf, sizeof(optbuf),
421 "(%s, -%c) ", l->units, l->option);
423 snprintf(optbuf, sizeof(optbuf),
424 "(-%c) ", l->option);
425 out1fmt("%-18s %18s ", l->name, optbuf);
426 if (val == RLIM_INFINITY)
427 out1fmt("unlimited\n");
431 out1fmt("%qd\n", (quad_t) val);
437 if (getrlimit(l->cmd, &limit) < 0)
438 error("ulimit: can't get limit: %s", strerror(errno));
441 limit.rlim_cur = val;
443 limit.rlim_max = val;
444 if (setrlimit(l->cmd, &limit) < 0)
445 error("ulimit: bad limit: %s", strerror(errno));
448 val = limit.rlim_cur;
450 val = limit.rlim_max;
452 if (val == RLIM_INFINITY)
453 out1fmt("unlimited\n");
457 out1fmt("%qd\n", (quad_t) val);