1 /* $NetBSD: miscbltin.c,v 1.37 2008/12/28 19:53:37 christos Exp $ */
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: miscbltin.c,v 1.37 2008/12/28 19:53:37 christos Exp $");
45 * Miscelaneous builtins.
48 #include <sys/types.h> /* quad_t */
49 #include <sys/param.h> /* BSD4_4 */
52 #include <sys/resource.h>
64 #include "miscbltin.h"
73 * Backslahes escape the next char unless -r is specified.
75 * This uses unbuffered input, which may be avoidable in some cases.
77 * Note that if IFS=' :' then read x y should work so that:
79 * ' a b ' x='a', y='b'
85 * ':b c:' x='', y='b c:'
89 readcmd(int argc
, char **argv
)
105 while ((i
= nextopt("p:r")) != '\0') {
112 if (prompt
&& isatty(0)) {
117 if (*(ap
= argptr
) == NULL
)
120 if ((ifs
= bltinlookup("IFS", 1)) == NULL
)
127 if (read(0, &c
, 1) != 1) {
133 if (c
== '\\' && !rflag
) {
134 if (read(0, &c
, 1) != 1) {
145 is_ifs
= strchr(" \t\n", c
) ? 1 : 2;
149 if (startword
!= 0) {
151 /* Ignore leading IFS whitespace */
156 if (is_ifs
== 2 && startword
== 1) {
157 /* Only one non-whitespace IFS per word */
166 /* append this character to the current variable */
169 /* Not just a spare terminator */
175 /* end of variable... */
179 /* Last variable needs all IFS chars */
186 setvar(*ap
, stackblock(), 0);
192 /* Remove trailing IFS chars */
193 for (; stackblock() <= --p
; *p
= 0) {
194 if (!strchr(ifs
, *p
))
196 if (strchr(" \t\n", *p
))
197 /* Always remove whitespace */
200 /* Don't remove non-whitespace unless it was naked */
203 setvar(*ap
, stackblock(), 0);
205 /* Set any remaining args to "" */
206 while (*++ap
!= NULL
)
207 setvar(*ap
, nullstr
, 0);
214 umaskcmd(int argc
, char **argv
)
219 int symbolic_mode
= 0;
221 while ((i
= nextopt("S")) != '\0') {
230 if ((ap
= *argptr
) == NULL
) {
232 char u
[4], g
[4], o
[4];
235 if ((mask
& S_IRUSR
) == 0)
237 if ((mask
& S_IWUSR
) == 0)
239 if ((mask
& S_IXUSR
) == 0)
244 if ((mask
& S_IRGRP
) == 0)
246 if ((mask
& S_IWGRP
) == 0)
248 if ((mask
& S_IXGRP
) == 0)
253 if ((mask
& S_IROTH
) == 0)
255 if ((mask
& S_IWOTH
) == 0)
257 if ((mask
& S_IXOTH
) == 0)
261 out1fmt("u=%s,g=%s,o=%s\n", u
, g
, o
);
263 out1fmt("%.4o\n", mask
);
266 if (isdigit((unsigned char)*ap
)) {
269 if (*ap
>= '8' || *ap
< '0')
270 error("Illegal number: %s", argv
[1]);
271 mask
= (mask
<< 3) + (*ap
- '0');
272 } while (*++ap
!= '\0');
278 if ((set
= setmode(ap
)) != 0) {
279 mask
= getmode(set
, ~mask
& 0777);
284 error("Cannot set mode `%s' (%s)", ap
,
296 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
297 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
298 * ash by J.T. Conklin.
306 int factor
; /* multiply by to get rlim_{cur,max} values */
310 static const struct limits limits
[] = {
312 { "time(seconds)", RLIMIT_CPU
, 1, 't' },
315 { "file(blocks)", RLIMIT_FSIZE
, 512, 'f' },
318 { "data(kbytes)", RLIMIT_DATA
, 1024, 'd' },
321 { "stack(kbytes)", RLIMIT_STACK
, 1024, 's' },
324 { "coredump(blocks)", RLIMIT_CORE
, 512, 'c' },
327 { "memory(kbytes)", RLIMIT_RSS
, 1024, 'm' },
329 #ifdef RLIMIT_MEMLOCK
330 { "locked memory(kbytes)", RLIMIT_MEMLOCK
, 1024, 'l' },
333 { "process(processes)", RLIMIT_NPROC
, 1, 'p' },
336 { "nofiles(descriptors)", RLIMIT_NOFILE
, 1, 'n' },
339 { "vmemory(kbytes)", RLIMIT_VMEM
, 1024, 'v' },
342 { "swap(kbytes)", RLIMIT_SWAP
, 1024, 'w' },
345 { "sbsize(bytes)", RLIMIT_SBSIZE
, 1, 'b' },
347 { (char *) 0, 0, 0, '\0' }
351 ulimitcmd(int argc
, char **argv
)
355 enum { SOFT
= 0x1, HARD
= 0x2 }
357 const struct limits
*l
;
363 while ((optc
= nextopt("HSabtfdsmcnplv")) != '\0')
378 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
381 error("internal error (%c)", what
);
383 set
= *argptr
? 1 : 0;
387 if (all
|| argptr
[1])
388 error("too many arguments");
389 if (strcmp(p
, "unlimited") == 0)
394 while ((c
= *p
++) >= '0' && c
<= '9')
395 val
= (val
* 10) + (long)(c
- '0');
402 for (l
= limits
; l
->name
; l
++) {
403 getrlimit(l
->cmd
, &limit
);
405 val
= limit
.rlim_cur
;
407 val
= limit
.rlim_max
;
409 out1fmt("%-20s ", l
->name
);
410 if (val
== RLIM_INFINITY
)
411 out1fmt("unlimited\n");
416 out1fmt("%lld\n", (long long) val
);
418 out1fmt("%ld\n", (long) val
);
425 getrlimit(l
->cmd
, &limit
);
428 limit
.rlim_max
= val
;
430 limit
.rlim_cur
= val
;
431 if (setrlimit(l
->cmd
, &limit
) < 0)
432 error("error setting limit (%s)", strerror(errno
));
435 val
= limit
.rlim_cur
;
437 val
= limit
.rlim_max
;
439 if (val
== RLIM_INFINITY
)
440 out1fmt("unlimited\n");
445 out1fmt("%lld\n", (long long) val
);
447 out1fmt("%ld\n", (long) val
);