1 /* vi: set sw=4 ts=4: */
3 * Adapted from ash applet code
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
11 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
12 * was re-ported from NetBSD and debianized.
14 * Copyright (c) 2010 Denys Vlasenko
17 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
20 #include "shell_common.h"
22 const char defifsvar[] ALIGN1 = "IFS= \t\n";
25 int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
27 if (!s || !(isalpha(*s) || *s == '_'))
32 while (isalnum(*s) || *s == '_');
34 return *s == terminator;
39 /* Needs to be interruptible: shell mush handle traps and shell-special signals
40 * while inside read. To implement this, be sure to not loop on EINTR
41 * and return errno == EINTR reliably.
43 //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
44 //string. hush naturally has it, and ash has setvareq().
45 //Here we can simply store "VAR=" at buffer start and store read data directly
46 //after "=", then pass buffer to setvar() to consume.
48 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
59 unsigned end_ms; /* -t TIMEOUT */
61 int nchars; /* -n NUM */
64 struct termios tty, old_tty;
66 int bufpos; /* need to be able to hold -1 */
74 if (!is_well_formed_var_name(*pp, '\0')) {
75 /* Mimic bash message */
76 bb_error_msg("read: '%s': not a valid identifier", *pp);
77 return (const char *)(uintptr_t)1;
82 nchars = 0; /* if != 0, -n is in effect */
84 nchars = bb_strtou(opt_n, NULL, 10);
85 if (nchars < 0 || errno)
86 return "invalid count";
87 /* note: "-n 0": off (bash 3.2 does this too) */
91 end_ms = bb_strtou(opt_t, NULL, 10);
92 if (errno || end_ms > UINT_MAX / 2048)
93 return "invalid timeout";
95 #if 0 /* even bash has no -t N.NNN support */
96 ts.tv_sec = bb_strtou(opt_t, &p, 10);
98 /* EINVAL means number is ok, but not terminated by NUL */
99 if (*p == '.' && errno == EINVAL) {
103 ts.tv_usec = bb_strtou(p, &p2, 10);
105 return "invalid timeout";
107 /* normalize to usec */
109 return "invalid timeout";
113 } else if (ts.tv_sec < 0 || errno) {
114 return "invalid timeout";
116 if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
117 return "invalid timeout";
123 fd = bb_strtou(opt_u, NULL, 10);
125 return "invalid file descriptor";
128 if (opt_p && isatty(fd)) {
129 fputs(opt_p, stderr);
136 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
140 tty.c_lflag &= ~ICANON;
141 tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
143 if (read_flags & BUILTIN_READ_SILENT) {
144 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
146 /* This forces execution of "restoring" tcgetattr later */
147 read_flags |= BUILTIN_READ_SILENT;
148 /* if tcgetattr failed, tcsetattr will fail too.
149 * Ignoring, it's harmless. */
150 tcsetattr(fd, TCSANOW, &tty);
153 retval = (const char *)(uintptr_t)0;
156 if (end_ms) /* NB: end_ms stays nonzero: */
157 end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
162 struct pollfd pfd[1];
165 if ((bufpos & 0xff) == 0)
166 buffer = xrealloc(buffer, bufpos + 0x100);
170 timeout = end_ms - (unsigned)monotonic_ms();
171 if (timeout <= 0) { /* already late? */
172 retval = (const char *)(uintptr_t)1;
177 /* We must poll even if timeout is -1:
178 * we want to be interrupted if signal arrives,
179 * regardless of SA_RESTART-ness of that signal!
183 pfd[0].events = POLLIN;
184 if (poll(pfd, 1, timeout) != 1) {
185 /* timed out, or EINTR */
187 retval = (const char *)(uintptr_t)1;
190 if (read(fd, &buffer[bufpos], 1) != 1) {
192 retval = (const char *)(uintptr_t)1;
205 if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
212 /* $IFS splitting. NOT done if we run "read"
213 * without variable names (bash compat).
214 * Thus, "read" and "read REPLY" are not the same.
217 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
218 const char *is_ifs = strchr(ifs, c);
219 if (startword && is_ifs) {
222 /* it is a non-space ifs char */
224 if (startword == 1) /* first one? */
225 continue; /* yes, it is not next word yet */
228 if (argv[1] != NULL && is_ifs) {
229 buffer[bufpos] = '\0';
231 setvar(*argv, buffer);
233 /* can we skip one non-space ifs char? (2: yes) */
234 startword = isspace(c) ? 2 : 1;
243 /* Remove trailing space $IFS chars */
244 while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
246 buffer[bufpos + 1] = '\0';
247 /* Use the remainder as a value for the next variable */
248 setvar(*argv, buffer);
249 /* Set the rest to "" */
253 /* Note: no $IFS removal */
254 buffer[bufpos] = '\0';
255 setvar("REPLY", buffer);
260 if (read_flags & BUILTIN_READ_SILENT)
261 tcsetattr(fd, TCSANOW, &old_tty);
270 uint8_t cmd; /* RLIMIT_xxx fit into it */
271 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
276 static const struct limits limits_tbl[] = {
278 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
281 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
284 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
287 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
290 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
293 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
295 #ifdef RLIMIT_MEMLOCK
296 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
299 { RLIMIT_NPROC, 0, 'p', "processes" },
302 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
305 { RLIMIT_AS, 10, 'v', "address space (kb)" },
308 { RLIMIT_LOCKS, 0, 'w', "locks" },
311 { RLIMIT_NICE, 0, 'e', "scheduling priority" },
314 { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
323 /* "-": treat args as parameters of option with ASCII code 1 */
324 static const char ulimit_opt_string[] = "-HSa"
343 #ifdef RLIMIT_MEMLOCK
366 static void printlim(unsigned opts, const struct rlimit *limit,
367 const struct limits *l)
371 val = limit->rlim_max;
372 if (!(opts & OPT_hard))
373 val = limit->rlim_cur;
375 if (val == RLIM_INFINITY)
376 printf("unlimited\n");
378 val >>= l->factor_shift;
379 printf("%llu\n", (long long) val);
384 shell_builtin_ulimit(char **argv)
389 /* We can't use getopt32: need to handle commands like
390 * ulimit 123 -c2 -l 456
393 /* In case getopt was already called:
394 * reset the libc getopt() function, which keeps internal state.
398 #else /* BSD style */
402 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
411 const struct limits *l;
412 int opt_char = getopt(argc, argv, ulimit_opt_string);
416 if (opt_char == 'H') {
420 if (opt_char == 'S') {
425 if (opt_char == 'a') {
426 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
427 getrlimit(l->cmd, &limit);
428 printf("-%c: %-30s ", l->option, l->name);
429 printlim(opts, &limit, l);
436 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
437 if (opt_char == l->option) {
440 getrlimit(l->cmd, &limit);
443 if (!val_str && argv[optind] && argv[optind][0] != '-')
444 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
448 if (strcmp(val_str, "unlimited") == 0)
451 if (sizeof(val) == sizeof(int))
452 val = bb_strtou(val_str, NULL, 10);
453 else if (sizeof(val) == sizeof(long))
454 val = bb_strtoul(val_str, NULL, 10);
456 val = bb_strtoull(val_str, NULL, 10);
458 bb_error_msg("invalid number '%s'", val_str);
461 val <<= l->factor_shift;
463 //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
464 /* from man bash: "If neither -H nor -S
465 * is specified, both the soft and hard
468 opts = OPT_hard + OPT_soft;
470 limit.rlim_max = val;
472 limit.rlim_cur = val;
473 //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
474 if (setrlimit(l->cmd, &limit) < 0) {
475 bb_perror_msg("error setting limit");
479 printlim(opts, &limit, l);
483 } /* for (every possible opt) */
485 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
486 /* bad option. getopt already complained. */
490 } /* while (there are options) */