]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/miscbltin.c
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 326025 2017-11-20 19:49:47Z pfg $");
44 * Miscellaneous builtins.
47 #include <sys/types.h>
50 #include <sys/resource.h>
69 int readcmd(int, char **);
70 int umaskcmd(int, char **);
71 int ulimitcmd(int, char **);
74 * The read builtin. The -r option causes backslashes to be treated like
75 * ordinary characters.
77 * This uses unbuffered input, which may be avoidable in some cases.
79 * Note that if IFS=' :' then read x y should work so that:
81 * ' a b ' x='a', y='b'
87 * ':b c:' x='', y='b c:'
91 readcmd(int argc __unused
, char **argv __unused
)
105 ptrdiff_t lastnonifs
, lastnonifsws
;
116 while ((i
= nextopt("erp:t:")) != '\0') {
127 tv
.tv_sec
= strtol(shoptarg
, &tvptr
, 0);
128 if (tvptr
== shoptarg
)
129 error("timeout value");
141 error("timeout unit");
146 if (prompt
&& isatty(0)) {
150 if (*(ap
= argptr
) == NULL
)
152 if ((ifs
= bltinlookup("IFS", 1)) == NULL
)
155 if (tv
.tv_sec
>= 0) {
157 * Wait for something to become available.
161 status
= select(1, &ifds
, NULL
, NULL
, &tv
);
163 * If there's nothing ready, return an error.
167 return (128 + (sig
!= 0 ? sig
: SIGALRM
));
175 lastnonifs
= lastnonifsws
= -1;
177 nread
= read(STDIN_FILENO
, &c
, 1);
179 if (errno
== EINTR
) {
186 warning("read error: %s", strerror(errno
));
189 } else if (nread
!= 1) {
200 lastnonifs
= lastnonifsws
= p
- stackblock();
205 if (!rflag
&& c
== '\\') {
212 is_ifs
= strchr(" \t\n", c
) ? 1 : 2;
216 if (startword
!= 0) {
218 /* Ignore leading IFS whitespace */
223 if (is_ifs
== 2 && startword
== 1) {
224 /* Only one non-whitespace IFS per word */
227 lastnonifsws
= p
- stackblock();
235 /* append this character to the current variable */
238 /* Not just a spare terminator */
240 lastnonifs
= lastnonifsws
= p
- stackblock();
245 /* end of variable... */
249 /* Last variable needs all IFS chars */
252 lastnonifsws
= p
- stackblock();
258 setvar(*ap
, stackblock(), 0);
261 lastnonifs
= lastnonifsws
= -1;
266 * Remove trailing IFS chars: always remove whitespace, don't remove
267 * non-whitespace unless it was naked
270 lastnonifsws
= lastnonifs
;
271 stackblock()[lastnonifsws
+ 1] = '\0';
272 setvar(*ap
, stackblock(), 0);
274 /* Set any remaining args to "" */
275 while (*++ap
!= NULL
)
283 umaskcmd(int argc __unused
, char **argv __unused
)
288 int symbolic_mode
= 0;
290 while ((i
= nextopt("S")) != '\0') {
299 if ((ap
= *argptr
) == NULL
) {
301 char u
[4], g
[4], o
[4];
304 if ((mask
& S_IRUSR
) == 0)
306 if ((mask
& S_IWUSR
) == 0)
308 if ((mask
& S_IXUSR
) == 0)
313 if ((mask
& S_IRGRP
) == 0)
315 if ((mask
& S_IWGRP
) == 0)
317 if ((mask
& S_IXGRP
) == 0)
322 if ((mask
& S_IROTH
) == 0)
324 if ((mask
& S_IWOTH
) == 0)
326 if ((mask
& S_IXOTH
) == 0)
330 out1fmt("u=%s,g=%s,o=%s\n", u
, g
, o
);
332 out1fmt("%.4o\n", mask
);
338 if (*ap
>= '8' || *ap
< '0')
339 error("Illegal number: %s", *argptr
);
340 mask
= (mask
<< 3) + (*ap
- '0');
341 } while (*++ap
!= '\0');
346 if ((set
= setmode (ap
)) == NULL
)
347 error("Illegal number: %s", ap
);
349 mask
= getmode (set
, ~mask
& 0777);
361 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
362 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
363 * ash by J.T. Conklin.
372 short factor
; /* multiply by to get rlim_{cur,max} values */
376 static const struct limits limits
[] = {
378 { "cpu time", "seconds", RLIMIT_CPU
, 1, 't' },
381 { "file size", "512-blocks", RLIMIT_FSIZE
, 512, 'f' },
384 { "data seg size", "kbytes", RLIMIT_DATA
, 1024, 'd' },
387 { "stack size", "kbytes", RLIMIT_STACK
, 1024, 's' },
390 { "core file size", "512-blocks", RLIMIT_CORE
, 512, 'c' },
393 { "max memory size", "kbytes", RLIMIT_RSS
, 1024, 'm' },
395 #ifdef RLIMIT_MEMLOCK
396 { "locked memory", "kbytes", RLIMIT_MEMLOCK
, 1024, 'l' },
399 { "max user processes", (char *)0, RLIMIT_NPROC
, 1, 'u' },
402 { "open files", (char *)0, RLIMIT_NOFILE
, 1, 'n' },
405 { "virtual mem size", "kbytes", RLIMIT_VMEM
, 1024, 'v' },
408 { "swap limit", "kbytes", RLIMIT_SWAP
, 1024, 'w' },
411 { "socket buffer size", "bytes", RLIMIT_SBSIZE
, 1, 'b' },
414 { "pseudo-terminals", (char *)0, RLIMIT_NPTS
, 1, 'p' },
416 #ifdef RLIMIT_KQUEUES
417 { "kqueues", (char *)0, RLIMIT_KQUEUES
, 1, 'k' },
420 { "umtx shared locks", (char *)0, RLIMIT_UMTXP
, 1, 'o' },
422 { (char *) 0, (char *)0, 0, 0, '\0' }
425 enum limithow
{ SOFT
= 0x1, HARD
= 0x2 };
428 printlimit(enum limithow how
, const struct rlimit
*limit
,
429 const struct limits
*l
)
434 val
= limit
->rlim_cur
;
436 val
= limit
->rlim_max
;
437 if (val
== RLIM_INFINITY
)
438 out1str("unlimited\n");
442 out1fmt("%jd\n", (intmax_t)val
);
447 ulimitcmd(int argc __unused
, char **argv __unused
)
450 enum limithow how
= SOFT
| HARD
;
451 const struct limits
*l
;
457 while ((optc
= nextopt("HSatfdsmcnuvlbpwko")) != '\0')
472 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
475 error("internal error (%c)", what
);
477 set
= *argptr
? 1 : 0;
481 if (all
|| argptr
[1])
482 error("too many arguments");
483 if (strcmp(p
, "unlimited") == 0)
489 if (*p
< '0' || *p
> '9')
492 uval
= strtoumax(p
, &end
, 10);
493 if (errno
!= 0 || *end
!= '\0')
495 if (uval
> UINTMAX_MAX
/ l
->factor
)
499 if ((uintmax_t)val
!= uval
||
500 val
== RLIM_INFINITY
)
505 for (l
= limits
; l
->name
; l
++) {
507 if (getrlimit(l
->cmd
, &limit
) < 0)
508 error("can't get limit: %s", strerror(errno
));
511 snprintf(optbuf
, sizeof(optbuf
),
512 "(%s, -%c) ", l
->units
, l
->option
);
514 snprintf(optbuf
, sizeof(optbuf
),
515 "(-%c) ", l
->option
);
516 out1fmt("%-18s %18s ", l
->name
, optbuf
);
517 printlimit(how
, &limit
, l
);
522 if (getrlimit(l
->cmd
, &limit
) < 0)
523 error("can't get limit: %s", strerror(errno
));
526 limit
.rlim_cur
= val
;
528 limit
.rlim_max
= val
;
529 if (setrlimit(l
->cmd
, &limit
) < 0)
530 error("bad limit: %s", strerror(errno
));
532 printlimit(how
, &limit
, l
);