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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)miscbltin.c 8.4 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/miscbltin.c 246167 2013-01-31 22:10:57Z jilles $
37 * Miscellaneous builtins.
40 #include <sys/types.h>
43 #include <sys/resource.h>
59 int readcmd(int, char **);
60 int umaskcmd(int, char **);
61 int ulimitcmd(int, char **);
66 * The read builtin. The -r option causes backslashes to be treated like
67 * ordinary characters.
69 * This uses unbuffered input, which may be avoidable in some cases.
71 * Note that if IFS=' :' then read x y should work so that:
73 * ' a b ' x='a', y='b'
79 * ':b c:' x='', y='b c:'
83 readcmd(int argc __unused
, char **argv __unused
)
105 while ((i
= nextopt("erp:t:")) != '\0') {
116 tv
.tv_sec
= strtol(shoptarg
, &tvptr
, 0);
117 if (tvptr
== shoptarg
)
118 error("timeout value");
130 error("timeout unit");
135 if (prompt
&& isatty(0)) {
139 if (*(ap
= argptr
) == NULL
)
141 if ((ifs
= bltinlookup("IFS", 1)) == NULL
)
144 if (tv
.tv_sec
>= 0) {
146 * Wait for something to become available.
150 status
= select(1, &ifds
, NULL
, NULL
, &tv
);
152 * If there's nothing ready, return an error.
163 if (read(STDIN_FILENO
, &c
, 1) != 1) {
177 if (!rflag
&& c
== '\\') {
184 is_ifs
= strchr(" \t\n", c
) ? 1 : 2;
188 if (startword
!= 0) {
190 /* Ignore leading IFS whitespace */
195 if (is_ifs
== 2 && startword
== 1) {
196 /* Only one non-whitespace IFS per word */
205 /* append this character to the current variable */
208 /* Not just a spare terminator */
214 /* end of variable... */
218 /* Last variable needs all IFS chars */
225 setvar(*ap
, stackblock(), 0);
231 /* Remove trailing IFS chars */
232 for (; stackblock() <= --p
; *p
= 0) {
233 if (!strchr(ifs
, *p
))
235 if (strchr(" \t\n", *p
))
236 /* Always remove whitespace */
239 /* Don't remove non-whitespace unless it was naked */
242 setvar(*ap
, stackblock(), 0);
244 /* Set any remaining args to "" */
245 while (*++ap
!= NULL
)
246 setvar(*ap
, nullstr
, 0);
253 umaskcmd(int argc __unused
, char **argv __unused
)
258 int symbolic_mode
= 0;
260 while ((i
= nextopt("S")) != '\0') {
269 if ((ap
= *argptr
) == NULL
) {
271 char u
[4], g
[4], o
[4];
274 if ((mask
& S_IRUSR
) == 0)
276 if ((mask
& S_IWUSR
) == 0)
278 if ((mask
& S_IXUSR
) == 0)
283 if ((mask
& S_IRGRP
) == 0)
285 if ((mask
& S_IWGRP
) == 0)
287 if ((mask
& S_IXGRP
) == 0)
292 if ((mask
& S_IROTH
) == 0)
294 if ((mask
& S_IWOTH
) == 0)
296 if ((mask
& S_IXOTH
) == 0)
300 out1fmt("u=%s,g=%s,o=%s\n", u
, g
, o
);
302 out1fmt("%.4o\n", mask
);
308 if (*ap
>= '8' || *ap
< '0')
309 error("Illegal number: %s", *argptr
);
310 mask
= (mask
<< 3) + (*ap
- '0');
311 } while (*++ap
!= '\0');
316 if ((set
= setmode (ap
)) == NULL
)
317 error("Illegal number: %s", ap
);
319 mask
= getmode (set
, ~mask
& 0777);
331 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
332 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
333 * ash by J.T. Conklin.
342 int factor
; /* multiply by to get rlim_{cur,max} values */
346 static const struct limits limits
[] = {
348 { "cpu time", "seconds", RLIMIT_CPU
, 1, 't' },
351 { "file size", "512-blocks", RLIMIT_FSIZE
, 512, 'f' },
354 { "data seg size", "kbytes", RLIMIT_DATA
, 1024, 'd' },
357 { "stack size", "kbytes", RLIMIT_STACK
, 1024, 's' },
360 { "core file size", "512-blocks", RLIMIT_CORE
, 512, 'c' },
363 { "max memory size", "kbytes", RLIMIT_RSS
, 1024, 'm' },
365 #ifdef RLIMIT_MEMLOCK
366 { "locked memory", "kbytes", RLIMIT_MEMLOCK
, 1024, 'l' },
369 { "max user processes", NULL
, RLIMIT_NPROC
, 1, 'u' },
372 { "open files", NULL
, RLIMIT_NOFILE
, 1, 'n' },
375 { "virtual mem size", "kbytes", RLIMIT_VMEM
, 1024, 'v' },
378 { "swap limit", "kbytes", RLIMIT_SWAP
, 1024, 'w' },
381 { "sbsize", "bytes", RLIMIT_SBSIZE
, 1, 'b' },
383 #ifdef RLIMIT_POSIXLOCK
384 { "posixlocks", NULL
, RLIMIT_POSIXLOCK
, 1, 'k' },
386 { NULL
, NULL
, 0, 0, '\0' }
390 ulimitcmd(int argc __unused
, char **argv __unused
)
394 enum { SOFT
= 0x1, HARD
= 0x2 }
396 const struct limits
*l
;
402 while ((optc
= nextopt("HSatfdsmcnuvlbk")) != '\0')
417 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
420 error("internal error (%c)", what
);
422 set
= *argptr
? 1 : 0;
426 if (all
|| argptr
[1])
427 error("too many arguments");
428 if (strcmp(p
, "unlimited") == 0)
433 while ((c
= *p
++) >= '0' && c
<= '9')
435 val
= (val
* 10) + (long)(c
- '0');
445 for (l
= limits
; l
->name
; l
++) {
447 if (getrlimit(l
->cmd
, &limit
) < 0)
448 error("can't get limit: %s", strerror(errno
));
450 val
= limit
.rlim_cur
;
452 val
= limit
.rlim_max
;
455 snprintf(optbuf
, sizeof(optbuf
),
456 "(%s, -%c) ", l
->units
, l
->option
);
458 snprintf(optbuf
, sizeof(optbuf
),
459 "(-%c) ", l
->option
);
460 out1fmt("%-18s %18s ", l
->name
, optbuf
);
461 if (val
== RLIM_INFINITY
)
462 out1str("unlimited\n");
466 out1fmt("%jd\n", (intmax_t)val
);
472 if (getrlimit(l
->cmd
, &limit
) < 0)
473 error("can't get limit: %s", strerror(errno
));
476 limit
.rlim_cur
= val
;
478 limit
.rlim_max
= val
;
479 if (setrlimit(l
->cmd
, &limit
) < 0)
480 error("bad limit: %s", strerror(errno
));
483 val
= limit
.rlim_cur
;
485 val
= limit
.rlim_max
;
487 if (val
== RLIM_INFINITY
)
488 out1str("unlimited\n");
492 out1fmt("%jd\n", (intmax_t)val
);