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 //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
40 //string. hush naturally has it, and ash has setvareq().
41 //Here we can simply store "VAR=" at buffer start and store read data directly
42 //after "=", then pass buffer to setvar() to consume.
44 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
54 unsigned end_ms; /* -t TIMEOUT */
56 int nchars; /* -n NUM */
59 struct termios tty, old_tty;
61 int bufpos; /* need to be able to hold -1 */
67 if (!is_well_formed_var_name(*pp, '\0')) {
68 /* Mimic bash message */
69 bb_error_msg("read: '%s': not a valid identifier", *pp);
70 return (const char *)(uintptr_t)1;
75 nchars = 0; /* if != 0, -n is in effect */
77 nchars = bb_strtou(opt_n, NULL, 10);
78 if (nchars < 0 || errno)
79 return "invalid count";
80 /* note: "-n 0": off (bash 3.2 does this too) */
84 end_ms = bb_strtou(opt_t, NULL, 10);
85 if (errno || end_ms > UINT_MAX / 2048)
86 return "invalid timeout";
88 #if 0 /* even bash has no -t N.NNN support */
89 ts.tv_sec = bb_strtou(opt_t, &p, 10);
91 /* EINVAL means number is ok, but not terminated by NUL */
92 if (*p == '.' && errno == EINVAL) {
96 ts.tv_usec = bb_strtou(p, &p2, 10);
98 return "invalid timeout";
100 /* normalize to usec */
102 return "invalid timeout";
106 } else if (ts.tv_sec < 0 || errno) {
107 return "invalid timeout";
109 if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
110 return "invalid timeout";
116 fd = bb_strtou(opt_u, NULL, 10);
118 return "invalid file descriptor";
121 if (opt_p && isatty(fd)) {
122 fputs(opt_p, stderr);
129 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
133 tty.c_lflag &= ~ICANON;
134 tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
136 if (read_flags & BUILTIN_READ_SILENT) {
137 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
139 /* This forces execution of "restoring" tcgetattr later */
140 read_flags |= BUILTIN_READ_SILENT;
141 /* if tcgetattr failed, tcsetattr will fail too.
142 * Ignoring, it's harmless. */
143 tcsetattr(fd, TCSANOW, &tty);
146 retval = (const char *)(uintptr_t)0;
149 if (end_ms) /* NB: end_ms stays nonzero: */
150 end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
158 struct pollfd pfd[1];
161 pfd[0].events = POLLIN;
162 timeout = end_ms - (unsigned)monotonic_ms();
163 if (timeout <= 0 /* already late? */
164 || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
166 retval = (const char *)(uintptr_t)1;
171 if ((bufpos & 0xff) == 0)
172 buffer = xrealloc(buffer, bufpos + 0x100);
173 if (nonblock_immune_read(fd, &buffer[bufpos], 1) != 1) {
174 retval = (const char *)(uintptr_t)1;
186 if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
193 /* $IFS splitting. NOT done if we run "read"
194 * without variable names (bash compat).
195 * Thus, "read" and "read REPLY" are not the same.
198 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
199 const char *is_ifs = strchr(ifs, c);
200 if (startword && is_ifs) {
203 /* it is a non-space ifs char */
205 if (startword == 1) /* first one? */
206 continue; /* yes, it is not next word yet */
209 if (argv[1] != NULL && is_ifs) {
210 buffer[bufpos] = '\0';
212 setvar(*argv, buffer);
214 /* can we skip one non-space ifs char? (2: yes) */
215 startword = isspace(c) ? 2 : 1;
224 /* Remove trailing space $IFS chars */
225 while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
227 buffer[bufpos + 1] = '\0';
228 /* Use the remainder as a value for the next variable */
229 setvar(*argv, buffer);
230 /* Set the rest to "" */
234 /* Note: no $IFS removal */
235 buffer[bufpos] = '\0';
236 setvar("REPLY", buffer);
241 if (read_flags & BUILTIN_READ_SILENT)
242 tcsetattr(fd, TCSANOW, &old_tty);
249 uint8_t cmd; /* RLIMIT_xxx fit into it */
250 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
255 static const struct limits limits_tbl[] = {
257 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
260 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
263 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
266 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
269 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
272 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
274 #ifdef RLIMIT_MEMLOCK
275 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
278 { RLIMIT_NPROC, 0, 'p', "processes" },
281 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
284 { RLIMIT_AS, 10, 'v', "address space (kb)" },
287 { RLIMIT_LOCKS, 0, 'w', "locks" },
290 { RLIMIT_NICE, 0, 'e', "scheduling priority" },
293 { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
302 /* "-": treat args as parameters of option with ASCII code 1 */
303 static const char ulimit_opt_string[] = "-HSa"
322 #ifdef RLIMIT_MEMLOCK
345 static void printlim(unsigned opts, const struct rlimit *limit,
346 const struct limits *l)
350 val = limit->rlim_max;
351 if (!(opts & OPT_hard))
352 val = limit->rlim_cur;
354 if (val == RLIM_INFINITY)
355 printf("unlimited\n");
357 val >>= l->factor_shift;
358 printf("%llu\n", (long long) val);
363 shell_builtin_ulimit(char **argv)
368 /* We can't use getopt32: need to handle commands like
369 * ulimit 123 -c2 -l 456
372 /* In case getopt was already called:
373 * reset the libc getopt() function, which keeps internal state.
377 #else /* BSD style */
381 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
390 const struct limits *l;
391 int opt_char = getopt(argc, argv, ulimit_opt_string);
395 if (opt_char == 'H') {
399 if (opt_char == 'S') {
404 if (opt_char == 'a') {
405 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
406 getrlimit(l->cmd, &limit);
407 printf("-%c: %-30s ", l->option, l->name);
408 printlim(opts, &limit, l);
415 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
416 if (opt_char == l->option) {
419 getrlimit(l->cmd, &limit);
422 if (!val_str && argv[optind] && argv[optind][0] != '-')
423 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
427 if (strcmp(val_str, "unlimited") == 0)
430 if (sizeof(val) == sizeof(int))
431 val = bb_strtou(val_str, NULL, 10);
432 else if (sizeof(val) == sizeof(long))
433 val = bb_strtoul(val_str, NULL, 10);
435 val = bb_strtoull(val_str, NULL, 10);
437 bb_error_msg("invalid number '%s'", val_str);
440 val <<= l->factor_shift;
442 //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
443 /* from man bash: "If neither -H nor -S
444 * is specified, both the soft and hard
447 opts = OPT_hard + OPT_soft;
449 limit.rlim_max = val;
451 limit.rlim_cur = val;
452 //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
453 if (setrlimit(l->cmd, &limit) < 0) {
454 bb_perror_msg("error setting limit");
458 printlim(opts, &limit, l);
462 } /* for (every possible opt) */
464 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
465 /* bad option. getopt already complained. */
469 } /* while (there are options) */