miscbltin.c revision 1.54 1 1.54 kre /* $NetBSD: miscbltin.c,v 1.54 2023/10/05 20:33:31 kre Exp $ */
2 1.13 cgd
3 1.1 cgd /*-
4 1.7 jtc * Copyright (c) 1991, 1993
5 1.7 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.32 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.19 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.13 cgd #if 0
38 1.14 christos static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
39 1.13 cgd #else
40 1.54 kre __RCSID("$NetBSD: miscbltin.c,v 1.54 2023/10/05 20:33:31 kre Exp $");
41 1.13 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.47 andvar * Miscellaneous builtins.
46 1.1 cgd */
47 1.1 cgd
48 1.23 christos #include <sys/types.h> /* quad_t */
49 1.23 christos #include <sys/param.h> /* BSD4_4 */
50 1.8 jtc #include <sys/stat.h>
51 1.14 christos #include <sys/time.h>
52 1.14 christos #include <sys/resource.h>
53 1.9 jtc #include <unistd.h>
54 1.27 christos #include <stdlib.h>
55 1.9 jtc #include <ctype.h>
56 1.28 christos #include <errno.h>
57 1.14 christos
58 1.1 cgd #include "shell.h"
59 1.1 cgd #include "options.h"
60 1.1 cgd #include "var.h"
61 1.1 cgd #include "output.h"
62 1.1 cgd #include "memalloc.h"
63 1.1 cgd #include "error.h"
64 1.39 christos #include "builtins.h"
65 1.1 cgd #include "mystring.h"
66 1.45 kre #include "redir.h" /* for user_fd_limit */
67 1.1 cgd
68 1.20 kleink #undef rflag
69 1.1 cgd
70 1.1 cgd
71 1.1 cgd
72 1.1 cgd /*
73 1.35 dsl * The read builtin.
74 1.47 andvar * Backslashes escape the next char unless -r is specified.
75 1.1 cgd *
76 1.1 cgd * This uses unbuffered input, which may be avoidable in some cases.
77 1.35 dsl *
78 1.35 dsl * Note that if IFS=' :' then read x y should work so that:
79 1.35 dsl * 'a b' x='a', y='b'
80 1.35 dsl * ' a b ' x='a', y='b'
81 1.35 dsl * ':b' x='', y='b'
82 1.35 dsl * ':' x='', y=''
83 1.35 dsl * '::' x='', y=''
84 1.35 dsl * ': :' x='', y=''
85 1.35 dsl * ':::' x='', y='::'
86 1.35 dsl * ':b c:' x='', y='b c:'
87 1.1 cgd */
88 1.1 cgd
89 1.12 cgd int
90 1.31 christos readcmd(int argc, char **argv)
91 1.12 cgd {
92 1.1 cgd char **ap;
93 1.1 cgd char c;
94 1.53 kre char end;
95 1.20 kleink int rflag;
96 1.1 cgd char *prompt;
97 1.35 dsl const char *ifs;
98 1.1 cgd char *p;
99 1.1 cgd int startword;
100 1.1 cgd int status;
101 1.1 cgd int i;
102 1.35 dsl int is_ifs;
103 1.35 dsl int saveall = 0;
104 1.51 kre ptrdiff_t wordlen = 0;
105 1.54 kre char *newifs = NULL;
106 1.54 kre struct stackmark mk;
107 1.1 cgd
108 1.53 kre end = '\n'; /* record delimiter */
109 1.20 kleink rflag = 0;
110 1.1 cgd prompt = NULL;
111 1.53 kre while ((i = nextopt("d:p:r")) != '\0') {
112 1.53 kre switch (i) {
113 1.53 kre case 'd':
114 1.53 kre end = *optionarg; /* even if '\0' */
115 1.53 kre break;
116 1.53 kre case 'p':
117 1.30 christos prompt = optionarg;
118 1.53 kre break;
119 1.53 kre case 'r':
120 1.20 kleink rflag = 1;
121 1.53 kre break;
122 1.53 kre }
123 1.1 cgd }
124 1.35 dsl
125 1.52 kre if (*(ap = argptr) == NULL)
126 1.52 kre error("variable name required\n"
127 1.52 kre "Usage: read [-r] [-p prompt] var...");
128 1.52 kre
129 1.1 cgd if (prompt && isatty(0)) {
130 1.1 cgd out2str(prompt);
131 1.1 cgd flushall();
132 1.1 cgd }
133 1.35 dsl
134 1.1 cgd if ((ifs = bltinlookup("IFS", 1)) == NULL)
135 1.35 dsl ifs = " \t\n";
136 1.35 dsl
137 1.54 kre setstackmark(&mk);
138 1.1 cgd status = 0;
139 1.35 dsl startword = 2;
140 1.1 cgd STARTSTACKSTR(p);
141 1.1 cgd for (;;) {
142 1.1 cgd if (read(0, &c, 1) != 1) {
143 1.1 cgd status = 1;
144 1.1 cgd break;
145 1.1 cgd }
146 1.53 kre if (c == '\\' && c != end && !rflag) {
147 1.35 dsl if (read(0, &c, 1) != 1) {
148 1.35 dsl status = 1;
149 1.35 dsl break;
150 1.35 dsl }
151 1.53 kre if (c != '\n') /* \ \n is always just removed */
152 1.51 kre goto wdch;
153 1.1 cgd continue;
154 1.1 cgd }
155 1.53 kre if (c == end)
156 1.1 cgd break;
157 1.53 kre if (c == '\0')
158 1.53 kre continue;
159 1.35 dsl if (strchr(ifs, c))
160 1.35 dsl is_ifs = strchr(" \t\n", c) ? 1 : 2;
161 1.35 dsl else
162 1.35 dsl is_ifs = 0;
163 1.35 dsl
164 1.35 dsl if (startword != 0) {
165 1.35 dsl if (is_ifs == 1) {
166 1.35 dsl /* Ignore leading IFS whitespace */
167 1.35 dsl if (saveall)
168 1.35 dsl STPUTC(c, p);
169 1.35 dsl continue;
170 1.35 dsl }
171 1.35 dsl if (is_ifs == 2 && startword == 1) {
172 1.35 dsl /* Only one non-whitespace IFS per word */
173 1.35 dsl startword = 2;
174 1.35 dsl if (saveall)
175 1.35 dsl STPUTC(c, p);
176 1.35 dsl continue;
177 1.35 dsl }
178 1.35 dsl }
179 1.35 dsl
180 1.35 dsl if (is_ifs == 0) {
181 1.51 kre wdch:;
182 1.53 kre if (c == '\0') /* always ignore attempts to input \0 */
183 1.53 kre continue;
184 1.35 dsl /* append this character to the current variable */
185 1.35 dsl startword = 0;
186 1.35 dsl if (saveall)
187 1.35 dsl /* Not just a spare terminator */
188 1.35 dsl saveall++;
189 1.35 dsl STPUTC(c, p);
190 1.51 kre wordlen = p - stackblock();
191 1.1 cgd continue;
192 1.1 cgd }
193 1.35 dsl
194 1.35 dsl /* end of variable... */
195 1.35 dsl startword = is_ifs;
196 1.35 dsl
197 1.35 dsl if (ap[1] == NULL) {
198 1.35 dsl /* Last variable needs all IFS chars */
199 1.35 dsl saveall++;
200 1.1 cgd STPUTC(c, p);
201 1.35 dsl continue;
202 1.1 cgd }
203 1.35 dsl
204 1.54 kre if (equal(*ap, "IFS")) {
205 1.54 kre /*
206 1.54 kre * we must not alter the value of IFS, as our
207 1.54 kre * local "ifs" var is (perhaps) pointing at it,
208 1.54 kre * at best we would be using data after free()
209 1.54 kre * the next time we reference ifs - but that mem
210 1.54 kre * may have been reused for something different.
211 1.54 kre *
212 1.54 kre * note that this might occur several times
213 1.54 kre */
214 1.54 kre STPUTC('\0', p);
215 1.54 kre newifs = grabstackstr(p);
216 1.54 kre } else {
217 1.54 kre STACKSTRNUL(p);
218 1.54 kre setvar(*ap, stackblock(), 0);
219 1.54 kre }
220 1.35 dsl ap++;
221 1.35 dsl STARTSTACKSTR(p);
222 1.51 kre wordlen = 0;
223 1.1 cgd }
224 1.1 cgd STACKSTRNUL(p);
225 1.35 dsl
226 1.35 dsl /* Remove trailing IFS chars */
227 1.51 kre for (; stackblock() + wordlen <= --p; *p = 0) {
228 1.35 dsl if (!strchr(ifs, *p))
229 1.35 dsl break;
230 1.35 dsl if (strchr(" \t\n", *p))
231 1.35 dsl /* Always remove whitespace */
232 1.35 dsl continue;
233 1.35 dsl if (saveall > 1)
234 1.35 dsl /* Don't remove non-whitespace unless it was naked */
235 1.35 dsl break;
236 1.35 dsl }
237 1.54 kre
238 1.54 kre /*
239 1.54 kre * If IFS was one of the variables named, we can finally set it now
240 1.54 kre * (no further references to ifs will be made)
241 1.54 kre */
242 1.54 kre if (newifs != NULL)
243 1.54 kre setvar("IFS", newifs, 0);
244 1.54 kre
245 1.54 kre /*
246 1.54 kre * Now we can assign to the final variable (which might
247 1.54 kre * also be IFS, hence the ordering here)
248 1.54 kre */
249 1.1 cgd setvar(*ap, stackblock(), 0);
250 1.35 dsl
251 1.35 dsl /* Set any remaining args to "" */
252 1.1 cgd while (*++ap != NULL)
253 1.1 cgd setvar(*ap, nullstr, 0);
254 1.54 kre
255 1.54 kre popstackmark(&mk);
256 1.1 cgd return status;
257 1.1 cgd }
258 1.1 cgd
259 1.1 cgd
260 1.1 cgd
261 1.12 cgd int
262 1.31 christos umaskcmd(int argc, char **argv)
263 1.12 cgd {
264 1.8 jtc char *ap;
265 1.49 kre mode_t mask;
266 1.1 cgd int i;
267 1.8 jtc int symbolic_mode = 0;
268 1.8 jtc
269 1.8 jtc while ((i = nextopt("S")) != '\0') {
270 1.8 jtc symbolic_mode = 1;
271 1.8 jtc }
272 1.1 cgd
273 1.8 jtc INTOFF;
274 1.8 jtc mask = umask(0);
275 1.8 jtc umask(mask);
276 1.8 jtc INTON;
277 1.8 jtc
278 1.8 jtc if ((ap = *argptr) == NULL) {
279 1.8 jtc if (symbolic_mode) {
280 1.8 jtc char u[4], g[4], o[4];
281 1.8 jtc
282 1.8 jtc i = 0;
283 1.8 jtc if ((mask & S_IRUSR) == 0)
284 1.8 jtc u[i++] = 'r';
285 1.8 jtc if ((mask & S_IWUSR) == 0)
286 1.8 jtc u[i++] = 'w';
287 1.8 jtc if ((mask & S_IXUSR) == 0)
288 1.8 jtc u[i++] = 'x';
289 1.8 jtc u[i] = '\0';
290 1.8 jtc
291 1.8 jtc i = 0;
292 1.8 jtc if ((mask & S_IRGRP) == 0)
293 1.8 jtc g[i++] = 'r';
294 1.8 jtc if ((mask & S_IWGRP) == 0)
295 1.8 jtc g[i++] = 'w';
296 1.8 jtc if ((mask & S_IXGRP) == 0)
297 1.8 jtc g[i++] = 'x';
298 1.8 jtc g[i] = '\0';
299 1.8 jtc
300 1.8 jtc i = 0;
301 1.8 jtc if ((mask & S_IROTH) == 0)
302 1.8 jtc o[i++] = 'r';
303 1.8 jtc if ((mask & S_IWOTH) == 0)
304 1.8 jtc o[i++] = 'w';
305 1.8 jtc if ((mask & S_IXOTH) == 0)
306 1.8 jtc o[i++] = 'x';
307 1.8 jtc o[i] = '\0';
308 1.8 jtc
309 1.8 jtc out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
310 1.8 jtc } else {
311 1.8 jtc out1fmt("%.4o\n", mask);
312 1.8 jtc }
313 1.1 cgd } else {
314 1.25 christos if (isdigit((unsigned char)*ap)) {
315 1.49 kre int range = 0;
316 1.49 kre
317 1.8 jtc mask = 0;
318 1.8 jtc do {
319 1.8 jtc if (*ap >= '8' || *ap < '0')
320 1.48 kre error("Not a valid octal number: '%s'",
321 1.49 kre *argptr);
322 1.8 jtc mask = (mask << 3) + (*ap - '0');
323 1.49 kre if (mask & ~07777)
324 1.49 kre range = 1;
325 1.8 jtc } while (*++ap != '\0');
326 1.49 kre if (range)
327 1.49 kre error("Mask constant '%s' out of range", *argptr);
328 1.8 jtc umask(mask);
329 1.8 jtc } else {
330 1.16 christos void *set;
331 1.8 jtc
332 1.26 itohy INTOFF;
333 1.26 itohy if ((set = setmode(ap)) != 0) {
334 1.26 itohy mask = getmode(set, ~mask & 0777);
335 1.26 itohy ckfree(set);
336 1.26 itohy }
337 1.26 itohy INTON;
338 1.26 itohy if (!set)
339 1.36 christos error("Cannot set mode `%s' (%s)", ap,
340 1.36 christos strerror(errno));
341 1.26 itohy
342 1.8 jtc umask(~mask & 0777);
343 1.14 christos }
344 1.14 christos }
345 1.46 kre flushout(out1);
346 1.46 kre if (io_err(out1)) {
347 1.46 kre out2str("umask: I/O error\n");
348 1.46 kre return 1;
349 1.46 kre }
350 1.14 christos return 0;
351 1.14 christos }
352 1.14 christos
353 1.14 christos /*
354 1.14 christos * ulimit builtin
355 1.14 christos *
356 1.14 christos * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
357 1.14 christos * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
358 1.14 christos * ash by J.T. Conklin.
359 1.14 christos *
360 1.14 christos * Public domain.
361 1.14 christos */
362 1.14 christos
363 1.14 christos struct limits {
364 1.14 christos const char *name;
365 1.40 christos const char *unit;
366 1.14 christos char option;
367 1.50 kre int8_t cmd; /* all RLIMIT_xxx are <= 127 */
368 1.50 kre unsigned short factor; /* multiply by to get rlim_{cur,max} values */
369 1.14 christos };
370 1.14 christos
371 1.50 kre #define OPTSTRING_BASE "HSa"
372 1.50 kre
373 1.14 christos static const struct limits limits[] = {
374 1.14 christos #ifdef RLIMIT_CPU
375 1.50 kre { "time", "seconds", 't', RLIMIT_CPU, 1 },
376 1.50 kre #define OPTSTRING_t OPTSTRING_BASE "t"
377 1.50 kre #else
378 1.50 kre #define OPTSTRING_t OPTSTRING_BASE
379 1.14 christos #endif
380 1.14 christos #ifdef RLIMIT_FSIZE
381 1.50 kre { "file", "blocks", 'f', RLIMIT_FSIZE, 512 },
382 1.50 kre #define OPTSTRING_f OPTSTRING_t "f"
383 1.50 kre #else
384 1.50 kre #define OPTSTRING_f OPTSTRING_t
385 1.14 christos #endif
386 1.14 christos #ifdef RLIMIT_DATA
387 1.50 kre { "data", "kbytes", 'd', RLIMIT_DATA, 1024 },
388 1.50 kre #define OPTSTRING_d OPTSTRING_f "d"
389 1.50 kre #else
390 1.50 kre #define OPTSTRING_d OPTSTRING_f
391 1.14 christos #endif
392 1.14 christos #ifdef RLIMIT_STACK
393 1.50 kre { "stack", "kbytes", 's', RLIMIT_STACK, 1024 },
394 1.50 kre #define OPTSTRING_s OPTSTRING_d "s"
395 1.50 kre #else
396 1.50 kre #define OPTSTRING_s OPTSTRING_d
397 1.14 christos #endif
398 1.44 gson #ifdef RLIMIT_CORE
399 1.50 kre { "coredump", "blocks", 'c', RLIMIT_CORE, 512 },
400 1.50 kre #define OPTSTRING_c OPTSTRING_s "c"
401 1.50 kre #else
402 1.50 kre #define OPTSTRING_c OPTSTRING_s
403 1.14 christos #endif
404 1.14 christos #ifdef RLIMIT_RSS
405 1.50 kre { "memory", "kbytes", 'm', RLIMIT_RSS, 1024 },
406 1.50 kre #define OPTSTRING_m OPTSTRING_c "m"
407 1.50 kre #else
408 1.50 kre #define OPTSTRING_m OPTSTRING_c
409 1.14 christos #endif
410 1.14 christos #ifdef RLIMIT_MEMLOCK
411 1.50 kre { "locked memory","kbytes", 'l', RLIMIT_MEMLOCK, 1024 },
412 1.50 kre #define OPTSTRING_l OPTSTRING_m "l"
413 1.50 kre #else
414 1.50 kre #define OPTSTRING_l OPTSTRING_m
415 1.14 christos #endif
416 1.41 christos #ifdef RLIMIT_NTHR
417 1.50 kre { "thread", "threads", 'r', RLIMIT_NTHR, 1 },
418 1.50 kre #define OPTSTRING_r OPTSTRING_l "r"
419 1.50 kre #else
420 1.50 kre #define OPTSTRING_r OPTSTRING_l
421 1.41 christos #endif
422 1.14 christos #ifdef RLIMIT_NPROC
423 1.50 kre { "process", "processes", 'p', RLIMIT_NPROC, 1 },
424 1.50 kre #define OPTSTRING_p OPTSTRING_r "p"
425 1.50 kre #else
426 1.50 kre #define OPTSTRING_p OPTSTRING_r
427 1.14 christos #endif
428 1.14 christos #ifdef RLIMIT_NOFILE
429 1.50 kre { "nofiles", "descriptors", 'n', RLIMIT_NOFILE, 1 },
430 1.50 kre #define OPTSTRING_n OPTSTRING_p "n"
431 1.50 kre #else
432 1.50 kre #define OPTSTRING_n OPTSTRING_p
433 1.14 christos #endif
434 1.14 christos #ifdef RLIMIT_VMEM
435 1.50 kre { "vmemory", "kbytes", 'v', RLIMIT_VMEM, 1024 },
436 1.50 kre #define OPTSTRING_v OPTSTRING_n "v"
437 1.50 kre #else
438 1.50 kre #define OPTSTRING_v OPTSTRING_n
439 1.14 christos #endif
440 1.14 christos #ifdef RLIMIT_SWAP
441 1.50 kre { "swap", "kbytes", 'w', RLIMIT_SWAP, 1024 },
442 1.50 kre #define OPTSTRING_w OPTSTRING_v "w"
443 1.50 kre #else
444 1.50 kre #define OPTSTRING_w OPTSTRING_v
445 1.14 christos #endif
446 1.33 christos #ifdef RLIMIT_SBSIZE
447 1.50 kre { "sbsize", "bytes", 'b', RLIMIT_SBSIZE, 1 },
448 1.50 kre #define OPTSTRING_b OPTSTRING_w "b"
449 1.50 kre #else
450 1.50 kre #define OPTSTRING_b OPTSTRING_w
451 1.33 christos #endif
452 1.50 kre { NULL, NULL, '\0', 0, 0 }
453 1.14 christos };
454 1.50 kre #define OPTSTRING OPTSTRING_b
455 1.14 christos
456 1.14 christos int
457 1.31 christos ulimitcmd(int argc, char **argv)
458 1.14 christos {
459 1.17 tls int c;
460 1.19 christos rlim_t val = 0;
461 1.14 christos enum { SOFT = 0x1, HARD = 0x2 }
462 1.45 kre how = 0, which;
463 1.14 christos const struct limits *l;
464 1.14 christos int set, all = 0;
465 1.14 christos int optc, what;
466 1.14 christos struct rlimit limit;
467 1.14 christos
468 1.14 christos what = 'f';
469 1.50 kre while ((optc = nextopt(OPTSTRING)) != '\0')
470 1.14 christos switch (optc) {
471 1.14 christos case 'H':
472 1.45 kre how |= HARD;
473 1.14 christos break;
474 1.14 christos case 'S':
475 1.45 kre how |= SOFT;
476 1.14 christos break;
477 1.14 christos case 'a':
478 1.14 christos all = 1;
479 1.14 christos break;
480 1.14 christos default:
481 1.14 christos what = optc;
482 1.14 christos }
483 1.14 christos
484 1.14 christos for (l = limits; l->name && l->option != what; l++)
485 1.14 christos ;
486 1.14 christos if (!l->name)
487 1.28 christos error("internal error (%c)", what);
488 1.14 christos
489 1.14 christos set = *argptr ? 1 : 0;
490 1.14 christos if (set) {
491 1.14 christos char *p = *argptr;
492 1.14 christos
493 1.14 christos if (all || argptr[1])
494 1.28 christos error("too many arguments");
495 1.45 kre if (how == 0)
496 1.45 kre how = HARD | SOFT;
497 1.45 kre
498 1.14 christos if (strcmp(p, "unlimited") == 0)
499 1.14 christos val = RLIM_INFINITY;
500 1.14 christos else {
501 1.15 jtc val = (rlim_t) 0;
502 1.14 christos
503 1.45 kre while ((c = *p++) >= '0' && c <= '9') {
504 1.45 kre if (val >= RLIM_INFINITY/10)
505 1.50 kre error("%s: value overflow", *argptr);
506 1.45 kre val = (val * 10);
507 1.45 kre if (val >= RLIM_INFINITY - (long)(c - '0'))
508 1.50 kre error("%s: value overflow", *argptr);
509 1.45 kre val += (long)(c - '0');
510 1.45 kre }
511 1.14 christos if (c)
512 1.50 kre error("%s: bad number", *argptr);
513 1.45 kre if (val > RLIM_INFINITY / l->factor)
514 1.50 kre error("%s: value overflow", *argptr);
515 1.14 christos val *= l->factor;
516 1.14 christos }
517 1.45 kre } else if (how == 0)
518 1.45 kre how = SOFT;
519 1.45 kre
520 1.14 christos if (all) {
521 1.14 christos for (l = limits; l->name; l++) {
522 1.14 christos getrlimit(l->cmd, &limit);
523 1.45 kre out1fmt("%-13s (-%c %-11s) ", l->name, l->option,
524 1.45 kre l->unit);
525 1.14 christos
526 1.45 kre which = how;
527 1.45 kre while (which != 0) {
528 1.45 kre if (which & SOFT) {
529 1.45 kre val = limit.rlim_cur;
530 1.45 kre which &= ~SOFT;
531 1.45 kre } else if (which & HARD) {
532 1.45 kre val = limit.rlim_max;
533 1.45 kre which &= ~HARD;
534 1.45 kre }
535 1.45 kre
536 1.45 kre if (val == RLIM_INFINITY)
537 1.45 kre out1fmt("unlimited");
538 1.45 kre else {
539 1.45 kre val /= l->factor;
540 1.18 christos #ifdef BSD4_4
541 1.45 kre out1fmt("%9lld", (long long) val);
542 1.18 christos #else
543 1.45 kre out1fmt("%9ld", (long) val);
544 1.18 christos #endif
545 1.45 kre }
546 1.45 kre out1fmt("%c", which ? '\t' : '\n');
547 1.14 christos }
548 1.14 christos }
549 1.46 kre goto done;
550 1.14 christos }
551 1.14 christos
552 1.43 christos if (getrlimit(l->cmd, &limit) == -1)
553 1.43 christos error("error getting limit (%s)", strerror(errno));
554 1.14 christos if (set) {
555 1.28 christos if (how & HARD)
556 1.28 christos limit.rlim_max = val;
557 1.14 christos if (how & SOFT)
558 1.14 christos limit.rlim_cur = val;
559 1.14 christos if (setrlimit(l->cmd, &limit) < 0)
560 1.28 christos error("error setting limit (%s)", strerror(errno));
561 1.45 kre if (l->cmd == RLIMIT_NOFILE)
562 1.45 kre user_fd_limit = sysconf(_SC_OPEN_MAX);
563 1.14 christos } else {
564 1.14 christos if (how & SOFT)
565 1.14 christos val = limit.rlim_cur;
566 1.14 christos else if (how & HARD)
567 1.14 christos val = limit.rlim_max;
568 1.14 christos
569 1.14 christos if (val == RLIM_INFINITY)
570 1.14 christos out1fmt("unlimited\n");
571 1.14 christos else
572 1.14 christos {
573 1.14 christos val /= l->factor;
574 1.18 christos #ifdef BSD4_4
575 1.29 lukem out1fmt("%lld\n", (long long) val);
576 1.18 christos #else
577 1.18 christos out1fmt("%ld\n", (long) val);
578 1.18 christos #endif
579 1.8 jtc }
580 1.1 cgd }
581 1.46 kre done:;
582 1.46 kre flushout(out1);
583 1.46 kre if (io_err(out1)) {
584 1.46 kre out2str("ulimit: I/O error (stdout)\n");
585 1.46 kre return 1;
586 1.46 kre }
587 1.1 cgd return 0;
588 1.1 cgd }
589