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
35 static char sccsid
[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/miscbltin.c,v 1.30 2004/04/06 20:06:51 markm Exp $");
44 * Miscellaneous builtins.
47 #include <sys/types.h>
50 #include <sys/select.h>
72 * The read builtin. The -r option causes backslashes to be treated like
73 * ordinary characters.
75 * This uses unbuffered input, which may be avoidable in some cases.
79 readcmd(int argc __unused
, char **argv __unused
)
95 struct termios told
, tnew
;
103 while ((i
= nextopt("erp:t:")) != '\0') {
114 tv
.tv_sec
= strtol(shoptarg
, &tvptr
, 0);
115 if (tvptr
== shoptarg
)
116 error("timeout value");
128 error("timeout unit");
133 if (prompt
&& isatty(0)) {
137 if (*(ap
= argptr
) == NULL
)
139 if ((ifs
= bltinlookup("IFS", 1)) == NULL
)
142 if (tv
.tv_sec
>= 0) {
147 * See if we can disable input processing; this will
148 * not give the desired result if we are in a pipeline
149 * and someone upstream is still in line-by-line mode.
152 if (tcgetattr(0, &told
) == 0) {
153 memcpy(&tnew
, &told
, sizeof(told
));
155 tcsetattr(0, TCSANOW
, &tnew
);
159 * Wait for something to become available.
163 status
= select(1, &ifds
, NULL
, NULL
, &tv
);
165 tcsetattr(0, TCSANOW
, &told
);
167 * If there's nothing ready, return an error.
179 if (read(STDIN_FILENO
, &c
, 1) != 1) {
191 if (!rflag
&& c
== '\\') {
197 if (startword
&& *ifs
== ' ' && strchr(ifs
, c
)) {
201 if (backslash
&& c
== '\\') {
202 if (read(STDIN_FILENO
, &c
, 1) != 1) {
207 } else if (ap
[1] != NULL
&& strchr(ifs
, c
) != NULL
) {
209 setvar(*ap
, stackblock(), 0);
218 setvar(*ap
, stackblock(), 0);
219 while (*++ap
!= NULL
)
220 setvar(*ap
, nullstr
, 0);
227 umaskcmd(int argc __unused
, char **argv
)
232 int symbolic_mode
= 0;
234 while ((i
= nextopt("S")) != '\0') {
243 if ((ap
= *argptr
) == NULL
) {
245 char u
[4], g
[4], o
[4];
248 if ((mask
& S_IRUSR
) == 0)
250 if ((mask
& S_IWUSR
) == 0)
252 if ((mask
& S_IXUSR
) == 0)
257 if ((mask
& S_IRGRP
) == 0)
259 if ((mask
& S_IWGRP
) == 0)
261 if ((mask
& S_IXGRP
) == 0)
266 if ((mask
& S_IROTH
) == 0)
268 if ((mask
& S_IWOTH
) == 0)
270 if ((mask
& S_IXOTH
) == 0)
274 out1fmt("u=%s,g=%s,o=%s\n", u
, g
, o
);
276 out1fmt("%.4o\n", mask
);
282 if (*ap
>= '8' || *ap
< '0')
283 error("Illegal number: %s", argv
[1]);
284 mask
= (mask
<< 3) + (*ap
- '0');
285 } while (*++ap
!= '\0');
289 if ((set
= setmode (ap
)) == 0)
290 error("Illegal number: %s", ap
);
292 mask
= getmode (set
, ~mask
& 0777);
303 unsigned long rlim_cur
; /* current (soft) limit */
304 unsigned long rlim_max
; /* maximum value for rlim_cur */
306 #define RLIM_INFINITY (((unsigned long)1 << 31) - 1)
308 int getrlimit (int, struct rlimit
*);
309 int setrlimit (int, const struct rlimit
*);
311 int getrlimit(resource
, rlp
)
318 int setrlimit(resource
, rlp
)
320 const struct rlimit
*rlp
;
330 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
331 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
332 * ash by J.T. Conklin.
341 int factor
; /* multiply by to get rlim_{cur,max} values */
345 static const struct limits limits
[] = {
347 { "cpu time", "seconds", RLIMIT_CPU
, 1, 't' },
350 { "file size", "512-blocks", RLIMIT_FSIZE
, 512, 'f' },
353 { "data seg size", "kbytes", RLIMIT_DATA
, 1024, 'd' },
356 { "stack size", "kbytes", RLIMIT_STACK
, 1024, 's' },
359 { "core file size", "512-blocks", RLIMIT_CORE
, 512, 'c' },
362 { "max memory size", "kbytes", RLIMIT_RSS
, 1024, 'm' },
364 #ifdef RLIMIT_MEMLOCK
365 { "locked memory", "kbytes", RLIMIT_MEMLOCK
, 1024, 'l' },
368 { "max user processes", (char *)0, RLIMIT_NPROC
, 1, 'u' },
371 { "open files", (char *)0, RLIMIT_NOFILE
, 1, 'n' },
374 { "virtual mem size", "kbytes", RLIMIT_VMEM
, 1024, 'v' },
377 { "swap limit", "kbytes", RLIMIT_SWAP
, 1024, 'w' },
380 { "sbsize", "bytes", RLIMIT_SBSIZE
, 1, 'b' },
382 { (char *) 0, (char *)0, 0, 0, '\0' }
386 ulimitcmd(int argc __unused
, char **argv __unused
)
390 enum { SOFT
= 0x1, HARD
= 0x2 }
392 const struct limits
*l
;
398 while ((optc
= nextopt("HSatfdsmcnuvlb")) != '\0')
413 for (l
= limits
; l
->name
&& l
->option
!= what
; l
++)
416 error("internal error (%c)", what
);
418 set
= *argptr
? 1 : 0;
422 if (all
|| argptr
[1])
423 error("too many arguments");
424 if (strcmp(p
, "unlimited") == 0)
429 while ((c
= *p
++) >= '0' && c
<= '9')
431 val
= (val
* 10) + (long)(c
- '0');
441 for (l
= limits
; l
->name
; l
++) {
443 if (getrlimit(l
->cmd
, &limit
) < 0)
444 error("can't get limit: %s", strerror(errno
));
446 val
= limit
.rlim_cur
;
448 val
= limit
.rlim_max
;
451 snprintf(optbuf
, sizeof(optbuf
),
452 "(%s, -%c) ", l
->units
, l
->option
);
454 snprintf(optbuf
, sizeof(optbuf
),
455 "(-%c) ", l
->option
);
456 out1fmt("%-18s %18s ", l
->name
, optbuf
);
457 if (val
== RLIM_INFINITY
)
458 out1fmt("unlimited\n");
462 out1fmt("%jd\n", (intmax_t)val
);
468 if (getrlimit(l
->cmd
, &limit
) < 0)
469 error("can't get limit: %s", strerror(errno
));
472 limit
.rlim_cur
= val
;
474 limit
.rlim_max
= val
;
475 if (setrlimit(l
->cmd
, &limit
) < 0)
476 error("bad limit: %s", strerror(errno
));
479 val
= limit
.rlim_cur
;
481 val
= limit
.rlim_max
;
483 if (val
== RLIM_INFINITY
)
484 out1fmt("unlimited\n");
488 out1fmt("%jd\n", (intmax_t)val
);
495 * $PchId: miscbltin.c,v 1.7 2006/05/23 11:59:08 philip Exp $