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: head/bin/sh/miscbltin.c 361384 2020-05-22 14:46:23Z jilles $");
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 return (128 + (sig
!= 0 ? sig
: SIGALRM
));
243 lastnonifs
= lastnonifsws
= -1;
244 fdctx_init(STDIN_FILENO
, &fdctx
);
247 nread
= fdgetc(&fdctx
, &c
);
249 if (errno
== EINTR
) {
256 warning("read error: %s", strerror(errno
));
259 } else if (nread
!= 1) {
270 lastnonifs
= lastnonifsws
= p
- stackblock();
275 if (!rflag
&& c
== '\\') {
282 is_ifs
= strchr(" \t\n", c
) ? 1 : 2;
286 if (startword
!= 0) {
288 /* Ignore leading IFS whitespace */
293 if (is_ifs
== 2 && startword
== 1) {
294 /* Only one non-whitespace IFS per word */
297 lastnonifsws
= p
- stackblock();
305 /* append this character to the current variable */
308 /* Not just a spare terminator */
310 lastnonifs
= lastnonifsws
= p
- stackblock();
315 /* end of variable... */
319 /* Last variable needs all IFS chars */
322 lastnonifsws
= p
- stackblock();
328 setvar(*ap
, stackblock(), 0);
331 lastnonifs
= lastnonifsws
= -1;
333 fdctx_destroy(&fdctx
);
337 * Remove trailing IFS chars: always remove whitespace, don't remove
338 * non-whitespace unless it was naked
341 lastnonifsws
= lastnonifs
;
342 stackblock()[lastnonifsws
+ 1] = '\0';
343 setvar(*ap
, stackblock(), 0);
345 /* Set any remaining args to "" */
346 while (*++ap
!= NULL
)
354 umaskcmd(int argc __unused
, char **argv __unused
)
359 int symbolic_mode
= 0;
361 while ((i
= nextopt("S")) != '\0') {
370 if ((ap
= *argptr
) == NULL
) {
372 char u
[4], g
[4], o
[4];
375 if ((mask
& S_IRUSR
) == 0)
377 if ((mask
& S_IWUSR
) == 0)
379 if ((mask
& S_IXUSR
) == 0)
384 if ((mask
& S_IRGRP
) == 0)
386 if ((mask
& S_IWGRP
) == 0)
388 if ((mask
& S_IXGRP
) == 0)
393 if ((mask
& S_IROTH
) == 0)
395 if ((mask
& S_IWOTH
) == 0)
397 if ((mask
& S_IXOTH
) == 0)
401 out1fmt("u=%s,g=%s,o=%s\n", u
, g
, o
);
403 out1fmt("%.4o\n", mask
);
409 if (*ap
>= '8' || *ap
< '0')
410 error("Illegal number: %s", *argptr
);
411 mask
= (mask
<< 3) + (*ap
- '0');
412 } while (*++ap
!= '\0');
417 if ((set
= setmode (ap
)) == NULL
)
418 error("Illegal number: %s", ap
);
420 mask
= getmode (set
, ~mask
& 0777);
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.
443 short factor
; /* multiply by to get rlim_{cur,max} values */
447 static const struct limits limits
[] = {
449 { "cpu time", "seconds", RLIMIT_CPU
, 1, 't' },
452 { "file size", "512-blocks", RLIMIT_FSIZE
, 512, 'f' },
455 { "data seg size", "kbytes", RLIMIT_DATA
, 1024, 'd' },
458 { "stack size", "kbytes", RLIMIT_STACK
, 1024, 's' },
461 { "core file size", "512-blocks", RLIMIT_CORE
, 512, 'c' },
464 { "max memory size", "kbytes", RLIMIT_RSS
, 1024, 'm' },
466 #ifdef RLIMIT_MEMLOCK
467 { "locked memory", "kbytes", RLIMIT_MEMLOCK
, 1024, 'l' },
470 { "max user processes", (char *)0, RLIMIT_NPROC
, 1, 'u' },
473 { "open files", (char *)0, RLIMIT_NOFILE
, 1, 'n' },
476 { "virtual mem size", "kbytes", RLIMIT_VMEM
, 1024, 'v' },
479 { "swap limit", "kbytes", RLIMIT_SWAP
, 1024, 'w' },
482 { "socket buffer size", "bytes", RLIMIT_SBSIZE
, 1, 'b' },
485 { "pseudo-terminals", (char *)0, RLIMIT_NPTS
, 1, 'p' },
487 #ifdef RLIMIT_KQUEUES
488 { "kqueues", (char *)0, RLIMIT_KQUEUES
, 1, 'k' },
491 { "umtx shared locks", (char *)0, RLIMIT_UMTXP
, 1, 'o' },
493 { (char *) 0, (char *)0, 0, 0, '\0' }
496 enum limithow
{ SOFT
= 0x1, HARD
= 0x2 };
499 printlimit(enum limithow how
, const struct rlimit
*limit
,
500 const struct limits
*l
)
505 val
= limit
->rlim_cur
;
507 val
= limit
->rlim_max
;
508 if (val
== RLIM_INFINITY
)
509 out1str("unlimited\n");
513 out1fmt("%jd\n", (intmax_t)val
);
518 ulimitcmd(int argc __unused
, char **argv __unused
)
521 enum limithow how
= SOFT
| HARD
;
522 const struct limits
*l
;
528 while ((optc
= nextopt("HSatfdsmcnuvlbpwko")) != '\0')
543 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
546 error("internal error (%c)", what
);
548 set
= *argptr
? 1 : 0;
552 if (all
|| argptr
[1])
553 error("too many arguments");
554 if (strcmp(p
, "unlimited") == 0)
560 if (*p
< '0' || *p
> '9')
563 uval
= strtoumax(p
, &end
, 10);
564 if (errno
!= 0 || *end
!= '\0')
566 if (uval
> UINTMAX_MAX
/ l
->factor
)
570 if (val
< 0 || (uintmax_t)val
!= uval
||
571 val
== RLIM_INFINITY
)
576 for (l
= limits
; l
->name
; l
++) {
578 if (getrlimit(l
->cmd
, &limit
) < 0)
579 error("can't get limit: %s", strerror(errno
));
582 snprintf(optbuf
, sizeof(optbuf
),
583 "(%s, -%c) ", l
->units
, l
->option
);
585 snprintf(optbuf
, sizeof(optbuf
),
586 "(-%c) ", l
->option
);
587 out1fmt("%-18s %18s ", l
->name
, optbuf
);
588 printlimit(how
, &limit
, l
);
593 if (getrlimit(l
->cmd
, &limit
) < 0)
594 error("can't get limit: %s", strerror(errno
));
597 limit
.rlim_cur
= val
;
599 limit
.rlim_max
= val
;
600 if (setrlimit(l
->cmd
, &limit
) < 0)
601 error("bad limit: %s", strerror(errno
));
603 printlimit(how
, &limit
, l
);