blob: 0a07296f30b253377a3a4a3abcf12fad31e542b3 [file] [log] [blame]
Denys Vlasenko73067272010-01-12 22:11:24 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Adapted from ash applet code
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
12 * was re-ported from NetBSD and debianized.
13 *
14 * Copyright (c) 2010 Denys Vlasenko
15 * Split from ash.c
16 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020017 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko73067272010-01-12 22:11:24 +010018 */
19#include "libbb.h"
20#include "shell_common.h"
21
Denys Vlasenko73067272010-01-12 22:11:24 +010022const char defifsvar[] ALIGN1 = "IFS= \t\n";
Denys Vlasenkoe627ac92016-09-30 14:36:59 +020023const char defoptindvar[] ALIGN1 = "OPTIND=1";
Denys Vlasenko25d9b912010-01-13 18:22:35 +010024
25
26int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
27{
28 if (!s || !(isalpha(*s) || *s == '_'))
29 return 0;
30
31 do
32 s++;
33 while (isalnum(*s) || *s == '_');
34
35 return *s == terminator;
36}
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020037
38/* read builtin */
39
Denys Vlasenko25ce3ee2013-07-14 01:23:06 +020040/* Needs to be interruptible: shell must handle traps and shell-special signals
Denys Vlasenko80542ba2011-05-08 21:23:43 +020041 * while inside read. To implement this, be sure to not loop on EINTR
42 * and return errno == EINTR reliably.
43 */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020044//TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
45//string. hush naturally has it, and ash has setvareq().
46//Here we can simply store "VAR=" at buffer start and store read data directly
47//after "=", then pass buffer to setvar() to consume.
48const char* FAST_FUNC
49shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
50 char **argv,
51 const char *ifs,
52 int read_flags,
53 const char *opt_n,
54 const char *opt_p,
55 const char *opt_t,
Johannes Schindelin3bef5d82017-08-08 16:46:39 +020056 const char *opt_u,
57 const char *opt_d
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020058)
59{
Denys Vlasenkoeae12682017-07-20 16:09:31 +020060 struct pollfd pfd[1];
61#define fd (pfd[0].fd) /* -u FD */
Denys Vlasenko80542ba2011-05-08 21:23:43 +020062 unsigned err;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020063 unsigned end_ms; /* -t TIMEOUT */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020064 int nchars; /* -n NUM */
65 char **pp;
66 char *buffer;
Denys Vlasenkocde46f72017-08-09 14:04:07 +020067 char delim;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020068 struct termios tty, old_tty;
69 const char *retval;
70 int bufpos; /* need to be able to hold -1 */
71 int startword;
72 smallint backslash;
73
Denys Vlasenko80542ba2011-05-08 21:23:43 +020074 errno = err = 0;
75
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020076 pp = argv;
77 while (*pp) {
78 if (!is_well_formed_var_name(*pp, '\0')) {
79 /* Mimic bash message */
80 bb_error_msg("read: '%s': not a valid identifier", *pp);
81 return (const char *)(uintptr_t)1;
82 }
83 pp++;
84 }
85
86 nchars = 0; /* if != 0, -n is in effect */
87 if (opt_n) {
88 nchars = bb_strtou(opt_n, NULL, 10);
89 if (nchars < 0 || errno)
90 return "invalid count";
91 /* note: "-n 0": off (bash 3.2 does this too) */
92 }
Denys Vlasenkoeae12682017-07-20 16:09:31 +020093
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020094 end_ms = 0;
Denys Vlasenkoeae12682017-07-20 16:09:31 +020095 if (opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020096 end_ms = bb_strtou(opt_t, NULL, 10);
Denys Vlasenkoeae12682017-07-20 16:09:31 +020097 if (errno)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020098 return "invalid timeout";
Denys Vlasenkoeae12682017-07-20 16:09:31 +020099 if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
100 end_ms = UINT_MAX / 2048;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200101 end_ms *= 1000;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200102 }
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200103 if (opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
104 /* bash 4.3 (maybe earlier) supports -t N.NNNNNN */
105 char *p;
106 /* Eat up to three fractional digits */
107 int frac_digits = 3 + 1;
108
109 end_ms = bb_strtou(opt_t, &p, 10);
110 if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
111 end_ms = UINT_MAX / 2048;
112
113 if (errno) {
114 /* EINVAL = number is ok, but not NUL terminated */
115 if (errno != EINVAL || *p != '.')
116 return "invalid timeout";
117 /* Do not check the rest: bash allows "0.123456xyz" */
118 while (*++p && --frac_digits) {
119 end_ms *= 10;
120 end_ms += (*p - '0');
121 if ((unsigned char)(*p - '0') > 9)
122 return "invalid timeout";
123 }
124 }
125 while (--frac_digits > 0) {
126 end_ms *= 10;
127 }
128 }
129
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200130 fd = STDIN_FILENO;
131 if (opt_u) {
132 fd = bb_strtou(opt_u, NULL, 10);
133 if (fd < 0 || errno)
134 return "invalid file descriptor";
135 }
136
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200137 if (opt_t && end_ms == 0) {
138 /* "If timeout is 0, read returns immediately, without trying
139 * to read any data. The exit status is 0 if input is available
140 * on the specified file descriptor, non-zero otherwise."
141 * bash seems to ignore -p PROMPT for this use case.
142 */
143 int r;
144 pfd[0].events = POLLIN;
145 r = poll(pfd, 1, /*timeout:*/ 0);
146 /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */
147 return (const char *)(uintptr_t)(r <= 0);
148 }
149
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200150 if (opt_p && isatty(fd)) {
151 fputs(opt_p, stderr);
152 fflush_all();
153 }
154
155 if (ifs == NULL)
156 ifs = defifs;
157
158 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
159 tcgetattr(fd, &tty);
160 old_tty = tty;
161 if (nchars) {
162 tty.c_lflag &= ~ICANON;
Denys Vlasenko7ce209b2012-01-15 22:58:06 +0100163 // Setting it to more than 1 breaks poll():
164 // it blocks even if there's data. !??
165 //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100166 /* reads will block only if < 1 char is available */
Denys Vlasenko7ce209b2012-01-15 22:58:06 +0100167 tty.c_cc[VMIN] = 1;
168 /* no timeout (reads block forever) */
169 tty.c_cc[VTIME] = 0;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200170 }
171 if (read_flags & BUILTIN_READ_SILENT) {
172 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
173 }
174 /* This forces execution of "restoring" tcgetattr later */
175 read_flags |= BUILTIN_READ_SILENT;
176 /* if tcgetattr failed, tcsetattr will fail too.
177 * Ignoring, it's harmless. */
178 tcsetattr(fd, TCSANOW, &tty);
179 }
180
181 retval = (const char *)(uintptr_t)0;
182 startword = 1;
183 backslash = 0;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200184 if (opt_t)
185 end_ms += (unsigned)monotonic_ms();
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200186 buffer = NULL;
187 bufpos = 0;
Denys Vlasenkocde46f72017-08-09 14:04:07 +0200188 delim = opt_d ? *opt_d : '\n';
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200189 do {
190 char c;
Denys Vlasenko10c01312011-05-11 11:49:21 +0200191 int timeout;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200192
Denys Vlasenko10c01312011-05-11 11:49:21 +0200193 if ((bufpos & 0xff) == 0)
Denys Vlasenko9e71e3c2012-09-06 13:28:10 +0200194 buffer = xrealloc(buffer, bufpos + 0x101);
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200195
Denys Vlasenko10c01312011-05-11 11:49:21 +0200196 timeout = -1;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200197 if (opt_t) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200198 timeout = end_ms - (unsigned)monotonic_ms();
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200199 /* ^^^^^^^^^^^^^ all values are unsigned,
200 * wrapping math is used here, good even if
201 * 32-bit unix time wrapped (year 2038+).
202 */
Denys Vlasenko10c01312011-05-11 11:49:21 +0200203 if (timeout <= 0) { /* already late? */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200204 retval = (const char *)(uintptr_t)1;
205 goto ret;
206 }
207 }
208
Denys Vlasenko10c01312011-05-11 11:49:21 +0200209 /* We must poll even if timeout is -1:
210 * we want to be interrupted if signal arrives,
211 * regardless of SA_RESTART-ness of that signal!
212 */
213 errno = 0;
Denys Vlasenko10c01312011-05-11 11:49:21 +0200214 pfd[0].events = POLLIN;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200215 if (poll(pfd, 1, timeout) <= 0) {
Denys Vlasenko10c01312011-05-11 11:49:21 +0200216 /* timed out, or EINTR */
217 err = errno;
218 retval = (const char *)(uintptr_t)1;
219 goto ret;
220 }
221 if (read(fd, &buffer[bufpos], 1) != 1) {
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200222 err = errno;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200223 retval = (const char *)(uintptr_t)1;
224 break;
225 }
Denys Vlasenko10c01312011-05-11 11:49:21 +0200226
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200227 c = buffer[bufpos];
228 if (c == '\0')
229 continue;
Denys Vlasenkof5470412017-05-22 19:34:45 +0200230 if (!(read_flags & BUILTIN_READ_RAW)) {
231 if (backslash) {
232 backslash = 0;
233 if (c != '\n')
234 goto put;
235 continue;
236 }
237 if (c == '\\') {
238 backslash = 1;
239 continue;
240 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200241 }
Denys Vlasenkocde46f72017-08-09 14:04:07 +0200242 if (c == delim) /* '\n' or -d CHAR */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200243 break;
244
245 /* $IFS splitting. NOT done if we run "read"
246 * without variable names (bash compat).
247 * Thus, "read" and "read REPLY" are not the same.
248 */
Johannes Schindelin3bef5d82017-08-08 16:46:39 +0200249 if (!opt_d && argv[0]) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200250/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
251 const char *is_ifs = strchr(ifs, c);
252 if (startword && is_ifs) {
253 if (isspace(c))
254 continue;
255 /* it is a non-space ifs char */
256 startword--;
257 if (startword == 1) /* first one? */
258 continue; /* yes, it is not next word yet */
259 }
260 startword = 0;
261 if (argv[1] != NULL && is_ifs) {
262 buffer[bufpos] = '\0';
263 bufpos = 0;
264 setvar(*argv, buffer);
265 argv++;
266 /* can we skip one non-space ifs char? (2: yes) */
267 startword = isspace(c) ? 2 : 1;
268 continue;
269 }
270 }
271 put:
272 bufpos++;
273 } while (--nchars);
274
275 if (argv[0]) {
276 /* Remove trailing space $IFS chars */
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200277 while (--bufpos >= 0
278 && isspace(buffer[bufpos])
279 && strchr(ifs, buffer[bufpos]) != NULL
280 ) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200281 continue;
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200282 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200283 buffer[bufpos + 1] = '\0';
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200284
285 /* Last variable takes the entire remainder with delimiters
286 * (sans trailing whitespace $IFS),
287 * but ***only "if there are fewer vars than fields"(c)***!
288 * The "X:Y:" case below: there are two fields,
289 * and therefore last delimiter (:) is eaten:
290 * IFS=": "
291 * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
292 * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
293 * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
294 * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
295 */
296 if (bufpos >= 0
297 && strchr(ifs, buffer[bufpos]) != NULL
298 ) {
299 /* There _is_ a non-whitespace IFS char */
300 /* Skip whitespace IFS char before it */
301 while (--bufpos >= 0
302 && isspace(buffer[bufpos])
303 && strchr(ifs, buffer[bufpos]) != NULL
304 ) {
305 continue;
306 }
307 /* Are there $IFS chars? */
308 if (strcspn(buffer, ifs) >= ++bufpos) {
309 /* No: last var takes one field, not more */
310 /* So, drop trailing IFS delims */
311 buffer[bufpos] = '\0';
312 }
313 }
314
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200315 /* Use the remainder as a value for the next variable */
316 setvar(*argv, buffer);
317 /* Set the rest to "" */
318 while (*++argv)
319 setvar(*argv, "");
320 } else {
321 /* Note: no $IFS removal */
322 buffer[bufpos] = '\0';
323 setvar("REPLY", buffer);
324 }
325
326 ret:
327 free(buffer);
328 if (read_flags & BUILTIN_READ_SILENT)
329 tcsetattr(fd, TCSANOW, &old_tty);
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200330
331 errno = err;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200332 return retval;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200333#undef fd
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200334}
335
336/* ulimit builtin */
337
338struct limits {
339 uint8_t cmd; /* RLIMIT_xxx fit into it */
340 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
341 char option;
342 const char *name;
343};
344
345static const struct limits limits_tbl[] = {
346#ifdef RLIMIT_FSIZE
347 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
348#endif
349#ifdef RLIMIT_CPU
350 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
351#endif
352#ifdef RLIMIT_DATA
353 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
354#endif
355#ifdef RLIMIT_STACK
356 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
357#endif
358#ifdef RLIMIT_CORE
359 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
360#endif
361#ifdef RLIMIT_RSS
362 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
363#endif
364#ifdef RLIMIT_MEMLOCK
365 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
366#endif
367#ifdef RLIMIT_NPROC
368 { RLIMIT_NPROC, 0, 'p', "processes" },
369#endif
370#ifdef RLIMIT_NOFILE
371 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
372#endif
373#ifdef RLIMIT_AS
374 { RLIMIT_AS, 10, 'v', "address space (kb)" },
375#endif
376#ifdef RLIMIT_LOCKS
377 { RLIMIT_LOCKS, 0, 'w', "locks" },
378#endif
Denys Vlasenkoe32d05b2011-04-04 02:12:14 +0200379#ifdef RLIMIT_NICE
380 { RLIMIT_NICE, 0, 'e', "scheduling priority" },
381#endif
382#ifdef RLIMIT_RTPRIO
383 { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
384#endif
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200385};
386
387enum {
388 OPT_hard = (1 << 0),
389 OPT_soft = (1 << 1),
390};
391
392/* "-": treat args as parameters of option with ASCII code 1 */
Denys Vlasenko3e134eb2016-04-22 18:09:21 +0200393static const char ulimit_opt_string[] ALIGN1 = "-HSa"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200394#ifdef RLIMIT_FSIZE
395 "f::"
396#endif
397#ifdef RLIMIT_CPU
398 "t::"
399#endif
400#ifdef RLIMIT_DATA
401 "d::"
402#endif
403#ifdef RLIMIT_STACK
404 "s::"
405#endif
406#ifdef RLIMIT_CORE
407 "c::"
408#endif
409#ifdef RLIMIT_RSS
410 "m::"
411#endif
412#ifdef RLIMIT_MEMLOCK
413 "l::"
414#endif
415#ifdef RLIMIT_NPROC
416 "p::"
417#endif
418#ifdef RLIMIT_NOFILE
419 "n::"
420#endif
421#ifdef RLIMIT_AS
422 "v::"
423#endif
424#ifdef RLIMIT_LOCKS
425 "w::"
426#endif
Denys Vlasenkoe32d05b2011-04-04 02:12:14 +0200427#ifdef RLIMIT_NICE
428 "e::"
429#endif
430#ifdef RLIMIT_RTPRIO
431 "r::"
432#endif
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200433 ;
434
435static void printlim(unsigned opts, const struct rlimit *limit,
436 const struct limits *l)
437{
438 rlim_t val;
439
440 val = limit->rlim_max;
441 if (!(opts & OPT_hard))
442 val = limit->rlim_cur;
443
444 if (val == RLIM_INFINITY)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200445 puts("unlimited");
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200446 else {
447 val >>= l->factor_shift;
448 printf("%llu\n", (long long) val);
449 }
450}
451
452int FAST_FUNC
453shell_builtin_ulimit(char **argv)
454{
455 unsigned opts;
456 unsigned argc;
457
458 /* We can't use getopt32: need to handle commands like
459 * ulimit 123 -c2 -l 456
460 */
461
Denys Vlasenko60161812017-08-29 14:32:17 +0200462 /* In case getopt() was already called:
463 * reset libc getopt() internal state.
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200464 */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +0300465 GETOPT_RESET();
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200466
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200467 argc = string_array_len(argv);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200468
469 opts = 0;
470 while (1) {
471 struct rlimit limit;
472 const struct limits *l;
473 int opt_char = getopt(argc, argv, ulimit_opt_string);
474
475 if (opt_char == -1)
476 break;
477 if (opt_char == 'H') {
478 opts |= OPT_hard;
479 continue;
480 }
481 if (opt_char == 'S') {
482 opts |= OPT_soft;
483 continue;
484 }
485
486 if (opt_char == 'a') {
487 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
488 getrlimit(l->cmd, &limit);
489 printf("-%c: %-30s ", l->option, l->name);
490 printlim(opts, &limit, l);
491 }
492 continue;
493 }
494
495 if (opt_char == 1)
496 opt_char = 'f';
497 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
498 if (opt_char == l->option) {
499 char *val_str;
500
501 getrlimit(l->cmd, &limit);
502
503 val_str = optarg;
504 if (!val_str && argv[optind] && argv[optind][0] != '-')
505 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
506 if (val_str) {
507 rlim_t val;
508
509 if (strcmp(val_str, "unlimited") == 0)
510 val = RLIM_INFINITY;
511 else {
512 if (sizeof(val) == sizeof(int))
513 val = bb_strtou(val_str, NULL, 10);
514 else if (sizeof(val) == sizeof(long))
515 val = bb_strtoul(val_str, NULL, 10);
516 else
517 val = bb_strtoull(val_str, NULL, 10);
518 if (errno) {
Denys Vlasenkob32a5432010-08-29 13:29:02 +0200519 bb_error_msg("invalid number '%s'", val_str);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200520 return EXIT_FAILURE;
521 }
522 val <<= l->factor_shift;
523 }
524//bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
Alexander Shishkin17e0e432010-07-27 08:40:55 +0200525 /* from man bash: "If neither -H nor -S
526 * is specified, both the soft and hard
527 * limits are set. */
528 if (!opts)
529 opts = OPT_hard + OPT_soft;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200530 if (opts & OPT_hard)
531 limit.rlim_max = val;
Alexander Shishkin17e0e432010-07-27 08:40:55 +0200532 if (opts & OPT_soft)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200533 limit.rlim_cur = val;
534//bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
535 if (setrlimit(l->cmd, &limit) < 0) {
536 bb_perror_msg("error setting limit");
537 return EXIT_FAILURE;
538 }
539 } else {
540 printlim(opts, &limit, l);
541 }
542 break;
543 }
544 } /* for (every possible opt) */
545
546 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
547 /* bad option. getopt already complained. */
548 break;
549 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200550 } /* while (there are options) */
551
552 return 0;
553}