2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Miscellaneous builtins.
47 #include <sys/types.h>
50 #include <sys/resource.h>
69 #define READ_BUFLEN 1024
72 size_t off; /* offset in buf */
74 char *ep; /* tail pointer */
75 char buf[READ_BUFLEN];
78 static void fdctx_init(int, struct fdctx *);
79 static void fdctx_destroy(struct fdctx *);
80 static ssize_t fdgetc(struct fdctx *, char *);
81 int readcmd(int, char **);
82 int umaskcmd(int, char **);
83 int ulimitcmd(int, char **);
86 fdctx_init(int fd, struct fdctx *fdc)
90 /* Check if fd is seekable. */
91 cur = lseek(fd, 0, SEEK_CUR);
92 *fdc = (struct fdctx){
94 .buflen = (cur != -1) ? READ_BUFLEN : 1,
95 .ep = &fdc->buf[0], /* No data */
100 fdgetc(struct fdctx *fdc, char *c)
104 if (&fdc->buf[fdc->off] == fdc->ep) {
105 nread = read(fdc->fd, fdc->buf, fdc->buflen);
108 fdc->ep = fdc->buf + nread;
112 *c = fdc->buf[fdc->off++];
118 fdctx_destroy(struct fdctx *fdc)
122 if (fdc->buflen > 1) {
124 * Reposition the file offset. Here is the layout of buf:
128 * |*****************|-------|
132 * off: current character
133 * ep: offset just after read(2)
134 * residue: length for reposition
136 residue = (fdc->ep - fdc->buf) - fdc->off;
138 (void) lseek(fdc->fd, -residue, SEEK_CUR);
143 * The read builtin. The -r option causes backslashes to be treated like
144 * ordinary characters.
146 * Note that if IFS=' :' then read x y should work so that:
148 * ' a b ' x='a', y='b'
154 * ':b c:' x='', y='b c:'
158 readcmd(int argc __unused, char **argv __unused)
172 ptrdiff_t lastnonifs, lastnonifsws;
184 while ((i = nextopt("erp:t:")) != '\0') {
195 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
196 if (tvptr == shoptarg)
197 error("timeout value");
209 error("timeout unit");
214 if (prompt && isatty(0)) {
218 if (*(ap = argptr) == NULL)
220 if ((ifs = bltinlookup("IFS", 1)) == NULL)
223 if (tv.tv_sec >= 0) {
225 * Wait for something to become available.
229 status = select(1, &ifds, NULL, NULL, &tv);
231 * If there's nothing ready, return an error.
235 setvar(*ap++, "", 0);
237 return (128 + (sig != 0 ? sig : SIGALRM));
245 lastnonifs = lastnonifsws = -1;
246 fdctx_init(STDIN_FILENO, &fdctx);
249 nread = fdgetc(&fdctx, &c);
251 if (errno == EINTR) {
258 warning("read error: %s", strerror(errno));
261 } else if (nread != 1) {
272 lastnonifs = lastnonifsws = p - stackblock();
277 if (!rflag && c == '\\') {
284 is_ifs = strchr(" \t\n", c) ? 1 : 2;
288 if (startword != 0) {
290 /* Ignore leading IFS whitespace */
295 if (is_ifs == 2 && startword == 1) {
296 /* Only one non-whitespace IFS per word */
299 lastnonifsws = p - stackblock();
307 /* append this character to the current variable */
310 /* Not just a spare terminator */
312 lastnonifs = lastnonifsws = p - stackblock();
317 /* end of variable... */
321 /* Last variable needs all IFS chars */
324 lastnonifsws = p - stackblock();
330 setvar(*ap, stackblock(), 0);
333 lastnonifs = lastnonifsws = -1;
335 fdctx_destroy(&fdctx);
339 * Remove trailing IFS chars: always remove whitespace, don't remove
340 * non-whitespace unless it was naked
343 lastnonifsws = lastnonifs;
344 stackblock()[lastnonifsws + 1] = '\0';
345 setvar(*ap, stackblock(), 0);
347 /* Set any remaining args to "" */
348 while (*++ap != NULL)
356 umaskcmd(int argc __unused, char **argv __unused)
361 int symbolic_mode = 0;
363 while ((i = nextopt("S")) != '\0') {
372 if ((ap = *argptr) == NULL) {
374 char u[4], g[4], o[4];
377 if ((mask & S_IRUSR) == 0)
379 if ((mask & S_IWUSR) == 0)
381 if ((mask & S_IXUSR) == 0)
386 if ((mask & S_IRGRP) == 0)
388 if ((mask & S_IWGRP) == 0)
390 if ((mask & S_IXGRP) == 0)
395 if ((mask & S_IROTH) == 0)
397 if ((mask & S_IWOTH) == 0)
399 if ((mask & S_IXOTH) == 0)
403 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
405 out1fmt("%.4o\n", mask);
411 if (*ap >= '8' || *ap < '0')
412 error("Illegal number: %s", *argptr);
413 mask = (mask << 3) + (*ap - '0');
414 } while (*++ap != '\0');
419 if ((set = setmode (ap)) == NULL)
420 error("Illegal number: %s", ap);
422 mask = getmode (set, ~mask & 0777);
434 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
435 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
436 * ash by J.T. Conklin.
445 short factor; /* multiply by to get rlim_{cur,max} values */
449 static const struct limits limits[] = {
451 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
454 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
457 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
460 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
463 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
466 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
468 #ifdef RLIMIT_MEMLOCK
469 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
472 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
475 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
478 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
481 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
484 { "socket buffer size", "bytes", RLIMIT_SBSIZE, 1, 'b' },
487 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' },
489 #ifdef RLIMIT_KQUEUES
490 { "kqueues", (char *)0, RLIMIT_KQUEUES, 1, 'k' },
493 { "umtx shared locks", (char *)0, RLIMIT_UMTXP, 1, 'o' },
495 { (char *) 0, (char *)0, 0, 0, '\0' }
498 enum limithow { SOFT = 0x1, HARD = 0x2 };
501 printlimit(enum limithow how, const struct rlimit *limit,
502 const struct limits *l)
507 val = limit->rlim_cur;
509 val = limit->rlim_max;
510 if (val == RLIM_INFINITY)
511 out1str("unlimited\n");
515 out1fmt("%jd\n", (intmax_t)val);
520 ulimitcmd(int argc __unused, char **argv __unused)
523 enum limithow how = SOFT | HARD;
524 const struct limits *l;
530 while ((optc = nextopt("HSatfdsmcnuvlbpwko")) != '\0')
545 for (l = limits; l->name && l->option != what; l++)
548 error("internal error (%c)", what);
550 set = *argptr ? 1 : 0;
554 if (all || argptr[1])
555 error("too many arguments");
556 if (strcmp(p, "unlimited") == 0)
562 if (*p < '0' || *p > '9')
565 uval = strtoumax(p, &end, 10);
566 if (errno != 0 || *end != '\0')
568 if (uval > UINTMAX_MAX / l->factor)
572 if (val < 0 || (uintmax_t)val != uval ||
573 val == RLIM_INFINITY)
578 for (l = limits; l->name; l++) {
580 if (getrlimit(l->cmd, &limit) < 0)
581 error("can't get limit: %s", strerror(errno));
584 snprintf(optbuf, sizeof(optbuf),
585 "(%s, -%c) ", l->units, l->option);
587 snprintf(optbuf, sizeof(optbuf),
588 "(-%c) ", l->option);
589 out1fmt("%-18s %18s ", l->name, optbuf);
590 printlimit(how, &limit, l);
595 if (getrlimit(l->cmd, &limit) < 0)
596 error("can't get limit: %s", strerror(errno));
599 limit.rlim_cur = val;
601 limit.rlim_max = val;
602 if (setrlimit(l->cmd, &limit) < 0)
603 error("bad limit: %s", strerror(errno));
605 printlimit(how, &limit, l);