usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / bin / sh / miscbltin.c
blobbd4164811d42762b5aa384a787c1a1693363f5b0
1 /*-
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
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
35 #ifndef lint
36 #if 0
37 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
38 #endif
39 #endif /* not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/miscbltin.c 361384 2020-05-22 14:46:23Z jilles $");
44 * Miscellaneous builtins.
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <unistd.h>
52 #include <errno.h>
53 #include <stdint.h>
54 #include <stdio.h>
55 #include <stdlib.h>
57 #include "shell.h"
58 #include "options.h"
59 #include "var.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "mystring.h"
64 #include "syntax.h"
65 #include "trap.h"
67 #undef eflag
69 #define READ_BUFLEN 1024
70 struct fdctx {
71 int fd;
72 size_t off; /* offset in buf */
73 size_t buflen;
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 **);
85 static void
86 fdctx_init(int fd, struct fdctx *fdc)
88 off_t cur;
90 /* Check if fd is seekable. */
91 cur = lseek(fd, 0, SEEK_CUR);
92 *fdc = (struct fdctx){
93 .fd = fd,
94 .buflen = (cur != -1) ? READ_BUFLEN : 1,
95 .ep = &fdc->buf[0], /* No data */
99 static ssize_t
100 fdgetc(struct fdctx *fdc, char *c)
102 ssize_t nread;
104 if (&fdc->buf[fdc->off] == fdc->ep) {
105 nread = read(fdc->fd, fdc->buf, fdc->buflen);
106 if (nread > 0) {
107 fdc->off = 0;
108 fdc->ep = fdc->buf + nread;
109 } else
110 return (nread);
112 *c = fdc->buf[fdc->off++];
114 return (1);
117 static void
118 fdctx_destroy(struct fdctx *fdc)
120 off_t residue;
122 if (fdc->buflen > 1) {
124 * Reposition the file offset. Here is the layout of buf:
126 * | off
128 * |*****************|-------|
129 * buf ep buf+buflen
130 * |<- residue ->|
132 * off: current character
133 * ep: offset just after read(2)
134 * residue: length for reposition
136 residue = (fdc->ep - fdc->buf) - fdc->off;
137 if (residue > 0)
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:
147 * 'a b' x='a', y='b'
148 * ' a b ' x='a', y='b'
149 * ':b' x='', y='b'
150 * ':' x='', y=''
151 * '::' x='', y=''
152 * ': :' x='', y=''
153 * ':::' x='', y='::'
154 * ':b c:' x='', y='b c:'
158 readcmd(int argc __unused, char **argv __unused)
160 char **ap;
161 int backslash;
162 char c;
163 int rflag;
164 char *prompt;
165 const char *ifs;
166 char *p;
167 int startword;
168 int status;
169 int i;
170 int is_ifs;
171 int saveall = 0;
172 ptrdiff_t lastnonifs, lastnonifsws;
173 struct timeval tv;
174 char *tvptr;
175 fd_set ifds;
176 ssize_t nread;
177 int sig;
178 struct fdctx fdctx;
180 rflag = 0;
181 prompt = NULL;
182 tv.tv_sec = -1;
183 tv.tv_usec = 0;
184 while ((i = nextopt("erp:t:")) != '\0') {
185 switch(i) {
186 case 'p':
187 prompt = shoptarg;
188 break;
189 case 'e':
190 break;
191 case 'r':
192 rflag = 1;
193 break;
194 case 't':
195 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
196 if (tvptr == shoptarg)
197 error("timeout value");
198 switch(*tvptr) {
199 case 0:
200 case 's':
201 break;
202 case 'h':
203 tv.tv_sec *= 60;
204 /* FALLTHROUGH */
205 case 'm':
206 tv.tv_sec *= 60;
207 break;
208 default:
209 error("timeout unit");
211 break;
214 if (prompt && isatty(0)) {
215 out2str(prompt);
216 flushall();
218 if (*(ap = argptr) == NULL)
219 error("arg count");
220 if ((ifs = bltinlookup("IFS", 1)) == NULL)
221 ifs = " \t\n";
223 if (tv.tv_sec >= 0) {
225 * Wait for something to become available.
227 FD_ZERO(&ifds);
228 FD_SET(0, &ifds);
229 status = select(1, &ifds, NULL, NULL, &tv);
231 * If there's nothing ready, return an error.
233 if (status <= 0) {
234 sig = pendingsig;
235 return (128 + (sig != 0 ? sig : SIGALRM));
239 status = 0;
240 startword = 2;
241 backslash = 0;
242 STARTSTACKSTR(p);
243 lastnonifs = lastnonifsws = -1;
244 fdctx_init(STDIN_FILENO, &fdctx);
245 for (;;) {
246 c = 0;
247 nread = fdgetc(&fdctx, &c);
248 if (nread == -1) {
249 if (errno == EINTR) {
250 sig = pendingsig;
251 if (sig == 0)
252 continue;
253 status = 128 + sig;
254 break;
256 warning("read error: %s", strerror(errno));
257 status = 2;
258 break;
259 } else if (nread != 1) {
260 status = 1;
261 break;
263 if (c == '\0')
264 continue;
265 CHECKSTRSPACE(1, p);
266 if (backslash) {
267 backslash = 0;
268 if (c != '\n') {
269 startword = 0;
270 lastnonifs = lastnonifsws = p - stackblock();
271 USTPUTC(c, p);
273 continue;
275 if (!rflag && c == '\\') {
276 backslash++;
277 continue;
279 if (c == '\n')
280 break;
281 if (strchr(ifs, c))
282 is_ifs = strchr(" \t\n", c) ? 1 : 2;
283 else
284 is_ifs = 0;
286 if (startword != 0) {
287 if (is_ifs == 1) {
288 /* Ignore leading IFS whitespace */
289 if (saveall)
290 USTPUTC(c, p);
291 continue;
293 if (is_ifs == 2 && startword == 1) {
294 /* Only one non-whitespace IFS per word */
295 startword = 2;
296 if (saveall) {
297 lastnonifsws = p - stackblock();
298 USTPUTC(c, p);
300 continue;
304 if (is_ifs == 0) {
305 /* append this character to the current variable */
306 startword = 0;
307 if (saveall)
308 /* Not just a spare terminator */
309 saveall++;
310 lastnonifs = lastnonifsws = p - stackblock();
311 USTPUTC(c, p);
312 continue;
315 /* end of variable... */
316 startword = is_ifs;
318 if (ap[1] == NULL) {
319 /* Last variable needs all IFS chars */
320 saveall++;
321 if (is_ifs == 2)
322 lastnonifsws = p - stackblock();
323 USTPUTC(c, p);
324 continue;
327 STACKSTRNUL(p);
328 setvar(*ap, stackblock(), 0);
329 ap++;
330 STARTSTACKSTR(p);
331 lastnonifs = lastnonifsws = -1;
333 fdctx_destroy(&fdctx);
334 STACKSTRNUL(p);
337 * Remove trailing IFS chars: always remove whitespace, don't remove
338 * non-whitespace unless it was naked
340 if (saveall <= 1)
341 lastnonifsws = lastnonifs;
342 stackblock()[lastnonifsws + 1] = '\0';
343 setvar(*ap, stackblock(), 0);
345 /* Set any remaining args to "" */
346 while (*++ap != NULL)
347 setvar(*ap, "", 0);
348 return status;
354 umaskcmd(int argc __unused, char **argv __unused)
356 char *ap;
357 int mask;
358 int i;
359 int symbolic_mode = 0;
361 while ((i = nextopt("S")) != '\0') {
362 symbolic_mode = 1;
365 INTOFF;
366 mask = umask(0);
367 umask(mask);
368 INTON;
370 if ((ap = *argptr) == NULL) {
371 if (symbolic_mode) {
372 char u[4], g[4], o[4];
374 i = 0;
375 if ((mask & S_IRUSR) == 0)
376 u[i++] = 'r';
377 if ((mask & S_IWUSR) == 0)
378 u[i++] = 'w';
379 if ((mask & S_IXUSR) == 0)
380 u[i++] = 'x';
381 u[i] = '\0';
383 i = 0;
384 if ((mask & S_IRGRP) == 0)
385 g[i++] = 'r';
386 if ((mask & S_IWGRP) == 0)
387 g[i++] = 'w';
388 if ((mask & S_IXGRP) == 0)
389 g[i++] = 'x';
390 g[i] = '\0';
392 i = 0;
393 if ((mask & S_IROTH) == 0)
394 o[i++] = 'r';
395 if ((mask & S_IWOTH) == 0)
396 o[i++] = 'w';
397 if ((mask & S_IXOTH) == 0)
398 o[i++] = 'x';
399 o[i] = '\0';
401 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
402 } else {
403 out1fmt("%.4o\n", mask);
405 } else {
406 if (is_digit(*ap)) {
407 mask = 0;
408 do {
409 if (*ap >= '8' || *ap < '0')
410 error("Illegal number: %s", *argptr);
411 mask = (mask << 3) + (*ap - '0');
412 } while (*++ap != '\0');
413 umask(mask);
414 } else {
415 void *set;
416 INTOFF;
417 if ((set = setmode (ap)) == NULL)
418 error("Illegal number: %s", ap);
420 mask = getmode (set, ~mask & 0777);
421 umask(~mask & 0777);
422 free(set);
423 INTON;
426 return 0;
430 * ulimit builtin
432 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
433 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
434 * ash by J.T. Conklin.
436 * Public domain.
439 struct limits {
440 const char *name;
441 const char *units;
442 int cmd;
443 short factor; /* multiply by to get rlim_{cur,max} values */
444 char option;
447 static const struct limits limits[] = {
448 #ifdef RLIMIT_CPU
449 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
450 #endif
451 #ifdef RLIMIT_FSIZE
452 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
453 #endif
454 #ifdef RLIMIT_DATA
455 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
456 #endif
457 #ifdef RLIMIT_STACK
458 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
459 #endif
460 #ifdef RLIMIT_CORE
461 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
462 #endif
463 #ifdef RLIMIT_RSS
464 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
465 #endif
466 #ifdef RLIMIT_MEMLOCK
467 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
468 #endif
469 #ifdef RLIMIT_NPROC
470 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
471 #endif
472 #ifdef RLIMIT_NOFILE
473 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
474 #endif
475 #ifdef RLIMIT_VMEM
476 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
477 #endif
478 #ifdef RLIMIT_SWAP
479 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
480 #endif
481 #ifdef RLIMIT_SBSIZE
482 { "socket buffer size", "bytes", RLIMIT_SBSIZE, 1, 'b' },
483 #endif
484 #ifdef RLIMIT_NPTS
485 { "pseudo-terminals", (char *)0, RLIMIT_NPTS, 1, 'p' },
486 #endif
487 #ifdef RLIMIT_KQUEUES
488 { "kqueues", (char *)0, RLIMIT_KQUEUES, 1, 'k' },
489 #endif
490 #ifdef RLIMIT_UMTXP
491 { "umtx shared locks", (char *)0, RLIMIT_UMTXP, 1, 'o' },
492 #endif
493 { (char *) 0, (char *)0, 0, 0, '\0' }
496 enum limithow { SOFT = 0x1, HARD = 0x2 };
498 static void
499 printlimit(enum limithow how, const struct rlimit *limit,
500 const struct limits *l)
502 rlim_t val = 0;
504 if (how & SOFT)
505 val = limit->rlim_cur;
506 else if (how & HARD)
507 val = limit->rlim_max;
508 if (val == RLIM_INFINITY)
509 out1str("unlimited\n");
510 else
512 val /= l->factor;
513 out1fmt("%jd\n", (intmax_t)val);
518 ulimitcmd(int argc __unused, char **argv __unused)
520 rlim_t val = 0;
521 enum limithow how = SOFT | HARD;
522 const struct limits *l;
523 int set, all = 0;
524 int optc, what;
525 struct rlimit limit;
527 what = 'f';
528 while ((optc = nextopt("HSatfdsmcnuvlbpwko")) != '\0')
529 switch (optc) {
530 case 'H':
531 how = HARD;
532 break;
533 case 'S':
534 how = SOFT;
535 break;
536 case 'a':
537 all = 1;
538 break;
539 default:
540 what = optc;
543 for (l = limits; l->name && l->option != what; l++)
545 if (!l->name)
546 error("internal error (%c)", what);
548 set = *argptr ? 1 : 0;
549 if (set) {
550 char *p = *argptr;
552 if (all || argptr[1])
553 error("too many arguments");
554 if (strcmp(p, "unlimited") == 0)
555 val = RLIM_INFINITY;
556 else {
557 char *end;
558 uintmax_t uval;
560 if (*p < '0' || *p > '9')
561 error("bad number");
562 errno = 0;
563 uval = strtoumax(p, &end, 10);
564 if (errno != 0 || *end != '\0')
565 error("bad number");
566 if (uval > UINTMAX_MAX / l->factor)
567 error("bad number");
568 uval *= l->factor;
569 val = (rlim_t)uval;
570 if (val < 0 || (uintmax_t)val != uval ||
571 val == RLIM_INFINITY)
572 error("bad number");
575 if (all) {
576 for (l = limits; l->name; l++) {
577 char optbuf[40];
578 if (getrlimit(l->cmd, &limit) < 0)
579 error("can't get limit: %s", strerror(errno));
581 if (l->units)
582 snprintf(optbuf, sizeof(optbuf),
583 "(%s, -%c) ", l->units, l->option);
584 else
585 snprintf(optbuf, sizeof(optbuf),
586 "(-%c) ", l->option);
587 out1fmt("%-18s %18s ", l->name, optbuf);
588 printlimit(how, &limit, l);
590 return 0;
593 if (getrlimit(l->cmd, &limit) < 0)
594 error("can't get limit: %s", strerror(errno));
595 if (set) {
596 if (how & SOFT)
597 limit.rlim_cur = val;
598 if (how & HARD)
599 limit.rlim_max = val;
600 if (setrlimit(l->cmd, &limit) < 0)
601 error("bad limit: %s", strerror(errno));
602 } else
603 printlimit(how, &limit, l);
604 return 0;