1 /* $NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl 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
37 static char sccsid
[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
39 __RCSID("$NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl Exp $");
44 * Miscelaneous builtins.
47 #include <sys/types.h>
58 #include "miscbltin.h"
60 #include "shinstance.h"
64 void *bsd_setmode(shinstance
*psh
, const char *p
);
65 mode_t
bsd_getmode(const void *bbox
, mode_t omode
);
70 * Backslahes escape the next char unless -r is specified.
72 * This uses unbuffered input, which may be avoidable in some cases.
74 * Note that if IFS=' :' then read x y should work so that:
76 * ' a b ' x='a', y='b'
82 * ':b c:' x='', y='b c:'
86 readcmd(shinstance
*psh
, int argc
, char **argv
)
102 while ((i
= nextopt(psh
, "p:r")) != '\0') {
104 prompt
= psh
->optionarg
;
109 if (prompt
&& isatty(0)) {
110 out2str(psh
, prompt
);
111 output_flushall(psh
);
114 if (*(ap
= psh
->argptr
) == NULL
)
115 error(psh
, "arg count");
117 if ((ifs
= bltinlookup(psh
, "IFS", 1)) == NULL
)
122 STARTSTACKSTR(psh
, p
);
124 if (shfile_read(&psh
->fdtab
, 0, &c
, 1) != 1) {
130 if (c
== '\\' && !rflag
) {
131 if (shfile_read(&psh
->fdtab
, 0, &c
, 1) != 1) {
142 is_ifs
= strchr(" \t\n", c
) ? 1 : 2;
146 if (startword
!= 0) {
148 /* Ignore leading IFS whitespace */
153 if (is_ifs
== 2 && startword
== 1) {
154 /* Only one non-whitespace IFS per word */
163 /* append this character to the current variable */
166 /* Not just a spare terminator */
172 /* end of variable... */
176 /* Last variable needs all IFS chars */
183 setvar(psh
, *ap
, stackblock(psh
), 0);
185 STARTSTACKSTR(psh
, p
);
189 /* Remove trailing IFS chars */
190 for (; stackblock(psh
) <= --p
; *p
= 0) {
191 if (!strchr(ifs
, *p
))
193 if (strchr(" \t\n", *p
))
194 /* Always remove whitespace */
197 /* Don't remove non-whitespace unless it was naked */
200 setvar(psh
, *ap
, stackblock(psh
), 0);
202 /* Set any remaining args to "" */
203 while (*++ap
!= NULL
)
204 setvar(psh
, *ap
, nullstr
, 0);
211 umaskcmd(shinstance
*psh
, int argc
, char **argv
)
216 int symbolic_mode
= 0;
218 while ((i
= nextopt(psh
, "S")) != '\0') {
227 if ((ap
= *psh
->argptr
) == NULL
) {
229 char u
[4], g
[4], o
[4];
232 if ((mask
& S_IRUSR
) == 0)
234 if ((mask
& S_IWUSR
) == 0)
236 if ((mask
& S_IXUSR
) == 0)
241 if ((mask
& S_IRGRP
) == 0)
243 if ((mask
& S_IWGRP
) == 0)
245 if ((mask
& S_IXGRP
) == 0)
250 if ((mask
& S_IROTH
) == 0)
252 if ((mask
& S_IWOTH
) == 0)
254 if ((mask
& S_IXOTH
) == 0)
258 out1fmt(psh
, "u=%s,g=%s,o=%s\n", u
, g
, o
);
260 out1fmt(psh
, "%.4o\n", mask
);
263 if (isdigit((unsigned char)*ap
)) {
266 if (*ap
>= '8' || *ap
< '0')
267 error(psh
, "Illegal number: %s", argv
[1]);
268 mask
= (mask
<< 3) + (*ap
- '0');
269 } while (*++ap
!= '\0');
275 if ((set
= bsd_setmode(psh
, ap
)) != 0) {
276 mask
= bsd_getmode(set
, ~mask
& 0777);
281 error(psh
, "Illegal mode: %s", ap
);
292 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
293 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
294 * ash by J.T. Conklin.
302 int factor
; /* multiply by to get rlim_{cur,max} values */
306 static const struct limits limits
[] = {
308 { "time(seconds)", RLIMIT_CPU
, 1, 't' },
311 { "file(blocks)", RLIMIT_FSIZE
, 512, 'f' },
314 { "data(kbytes)", RLIMIT_DATA
, 1024, 'd' },
317 { "stack(kbytes)", RLIMIT_STACK
, 1024, 's' },
320 { "coredump(blocks)", RLIMIT_CORE
, 512, 'c' },
323 { "memory(kbytes)", RLIMIT_RSS
, 1024, 'm' },
325 #ifdef RLIMIT_MEMLOCK
326 { "locked memory(kbytes)", RLIMIT_MEMLOCK
, 1024, 'l' },
329 { "process(processes)", RLIMIT_NPROC
, 1, 'p' },
332 { "nofiles(descriptors)", RLIMIT_NOFILE
, 1, 'n' },
335 { "vmemory(kbytes)", RLIMIT_VMEM
, 1024, 'v' },
338 { "swap(kbytes)", RLIMIT_SWAP
, 1024, 'w' },
341 { "sbsize(bytes)", RLIMIT_SBSIZE
, 1, 'b' },
343 { (char *) 0, 0, 0, '\0' }
347 ulimitcmd(shinstance
*psh
, int argc
, char **argv
)
351 enum { SOFT
= 0x1, HARD
= 0x2 }
353 const struct limits
*l
;
359 while ((optc
= nextopt(psh
, "HSabtfdsmcnpl")) != '\0')
374 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
377 error(psh
, "internal error (%c)", what
);
379 set
= *psh
->argptr
? 1 : 0;
381 char *p
= *psh
->argptr
;
383 if (all
|| psh
->argptr
[1])
384 error(psh
, "too many arguments");
385 if (strcmp(p
, "unlimited") == 0)
390 while ((c
= *p
++) >= '0' && c
<= '9')
392 val
= (val
* 10) + (long)(c
- '0');
393 if (val
< (shrlim_t
) 0)
397 error(psh
, "bad number");
402 for (l
= limits
; l
->name
; l
++) {
403 sh_getrlimit(psh
, l
->cmd
, &limit
);
405 val
= limit
.rlim_cur
;
407 val
= limit
.rlim_max
;
409 out1fmt(psh
, "%-20s ", l
->name
);
410 if (val
== RLIM_INFINITY
)
411 out1fmt(psh
, "unlimited\n");
415 out1fmt(psh
, "%lld\n", (long long) val
);
421 sh_getrlimit(psh
, l
->cmd
, &limit
);
424 limit
.rlim_max
= val
;
426 limit
.rlim_cur
= val
;
427 if (sh_setrlimit(psh
, l
->cmd
, &limit
) < 0)
428 error(psh
, "error setting limit (%s)", strerror(errno
));
431 val
= limit
.rlim_cur
;
433 val
= limit
.rlim_max
;
435 if (val
== RLIM_INFINITY
)
436 out1fmt(psh
, "unlimited\n");
440 out1fmt(psh
, "%lld\n", (long long) val
);