miscbltin.c revision 1.24 1 /* $NetBSD: miscbltin.c,v 1.24 1998/02/04 20:10:17 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
43 #else
44 __RCSID("$NetBSD: miscbltin.c,v 1.24 1998/02/04 20:10:17 thorpej Exp $");
45 #endif
46 #endif /* not lint */
47
48 /*
49 * Miscelaneous builtins.
50 */
51
52 #include <sys/types.h> /* quad_t */
53 #include <sys/param.h> /* BSD4_4 */
54 #include <sys/stat.h>
55 #include <sys/time.h>
56 #include <sys/resource.h>
57 #include <unistd.h>
58 #include <ctype.h>
59
60 #include "shell.h"
61 #include "options.h"
62 #include "var.h"
63 #include "output.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "miscbltin.h"
67 #include "mystring.h"
68
69 #undef rflag
70
71 extern char **argptr; /* argument list for builtin command */
72
73
74 /*
75 * The read builtin. The -e option causes backslashes to escape the
76 * following character.
77 *
78 * This uses unbuffered input, which may be avoidable in some cases.
79 */
80
81 int
82 readcmd(argc, argv)
83 int argc;
84 char **argv;
85 {
86 char **ap;
87 int backslash;
88 char c;
89 int rflag;
90 char *prompt;
91 char *ifs;
92 char *p;
93 int startword;
94 int status;
95 int i;
96
97 rflag = 0;
98 prompt = NULL;
99 while ((i = nextopt("p:r")) != '\0') {
100 if (i == 'p')
101 prompt = optarg;
102 else
103 rflag = 1;
104 }
105 if (prompt && isatty(0)) {
106 out2str(prompt);
107 flushall();
108 }
109 if (*(ap = argptr) == NULL)
110 error("arg count");
111 if ((ifs = bltinlookup("IFS", 1)) == NULL)
112 ifs = nullstr;
113 status = 0;
114 startword = 1;
115 backslash = 0;
116 STARTSTACKSTR(p);
117 for (;;) {
118 if (read(0, &c, 1) != 1) {
119 status = 1;
120 break;
121 }
122 if (c == '\0')
123 continue;
124 if (backslash) {
125 backslash = 0;
126 if (c != '\n')
127 STPUTC(c, p);
128 continue;
129 }
130 if (!rflag && c == '\\') {
131 backslash++;
132 continue;
133 }
134 if (c == '\n')
135 break;
136 if (startword && *ifs == ' ' && strchr(ifs, c)) {
137 continue;
138 }
139 startword = 0;
140 if (backslash && c == '\\') {
141 if (read(0, &c, 1) != 1) {
142 status = 1;
143 break;
144 }
145 STPUTC(c, p);
146 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
147 STACKSTRNUL(p);
148 setvar(*ap, stackblock(), 0);
149 ap++;
150 startword = 1;
151 STARTSTACKSTR(p);
152 } else {
153 STPUTC(c, p);
154 }
155 }
156 STACKSTRNUL(p);
157 /* Remove trailing blanks */
158 while (stackblock() <= --p && strchr(ifs, *p) != NULL)
159 *p = '\0';
160 setvar(*ap, stackblock(), 0);
161 while (*++ap != NULL)
162 setvar(*ap, nullstr, 0);
163 return status;
164 }
165
166
167
168 int
169 umaskcmd(argc, argv)
170 int argc;
171 char **argv;
172 {
173 char *ap;
174 int mask;
175 int i;
176 int symbolic_mode = 0;
177
178 while ((i = nextopt("S")) != '\0') {
179 symbolic_mode = 1;
180 }
181
182 INTOFF;
183 mask = umask(0);
184 umask(mask);
185 INTON;
186
187 if ((ap = *argptr) == NULL) {
188 if (symbolic_mode) {
189 char u[4], g[4], o[4];
190
191 i = 0;
192 if ((mask & S_IRUSR) == 0)
193 u[i++] = 'r';
194 if ((mask & S_IWUSR) == 0)
195 u[i++] = 'w';
196 if ((mask & S_IXUSR) == 0)
197 u[i++] = 'x';
198 u[i] = '\0';
199
200 i = 0;
201 if ((mask & S_IRGRP) == 0)
202 g[i++] = 'r';
203 if ((mask & S_IWGRP) == 0)
204 g[i++] = 'w';
205 if ((mask & S_IXGRP) == 0)
206 g[i++] = 'x';
207 g[i] = '\0';
208
209 i = 0;
210 if ((mask & S_IROTH) == 0)
211 o[i++] = 'r';
212 if ((mask & S_IWOTH) == 0)
213 o[i++] = 'w';
214 if ((mask & S_IXOTH) == 0)
215 o[i++] = 'x';
216 o[i] = '\0';
217
218 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
219 } else {
220 out1fmt("%.4o\n", mask);
221 }
222 } else {
223 if (isdigit(*ap)) {
224 mask = 0;
225 do {
226 if (*ap >= '8' || *ap < '0')
227 error("Illegal number: %s", argv[1]);
228 mask = (mask << 3) + (*ap - '0');
229 } while (*++ap != '\0');
230 umask(mask);
231 } else {
232 void *set;
233 if ((set = setmode (ap)) == 0)
234 error("Illegal number: %s", ap);
235
236 mask = getmode (set, ~mask & 0777);
237 umask(~mask & 0777);
238 }
239 }
240 return 0;
241 }
242
243 /*
244 * ulimit builtin
245 *
246 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
247 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
248 * ash by J.T. Conklin.
249 *
250 * Public domain.
251 */
252
253 struct limits {
254 const char *name;
255 int cmd;
256 int factor; /* multiply by to get rlim_{cur,max} values */
257 char option;
258 };
259
260 static const struct limits limits[] = {
261 #ifdef RLIMIT_CPU
262 { "time(seconds)", RLIMIT_CPU, 1, 't' },
263 #endif
264 #ifdef RLIMIT_FSIZE
265 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
266 #endif
267 #ifdef RLIMIT_DATA
268 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
269 #endif
270 #ifdef RLIMIT_STACK
271 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
272 #endif
273 #ifdef RLIMIT_CORE
274 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
275 #endif
276 #ifdef RLIMIT_RSS
277 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
278 #endif
279 #ifdef RLIMIT_MEMLOCK
280 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
281 #endif
282 #ifdef RLIMIT_NPROC
283 { "process(processes)", RLIMIT_NPROC, 1, 'p' },
284 #endif
285 #ifdef RLIMIT_NOFILE
286 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
287 #endif
288 #ifdef RLIMIT_VMEM
289 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
290 #endif
291 #ifdef RLIMIT_SWAP
292 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
293 #endif
294 { (char *) 0, 0, 0, '\0' }
295 };
296
297 int
298 ulimitcmd(argc, argv)
299 int argc;
300 char **argv;
301 {
302 int c;
303 rlim_t val = 0;
304 enum { SOFT = 0x1, HARD = 0x2 }
305 how = SOFT | HARD;
306 const struct limits *l;
307 int set, all = 0;
308 int optc, what;
309 struct rlimit limit;
310
311 what = 'f';
312 while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
313 switch (optc) {
314 case 'H':
315 how = HARD;
316 break;
317 case 'S':
318 how = SOFT;
319 break;
320 case 'a':
321 all = 1;
322 break;
323 default:
324 what = optc;
325 }
326
327 for (l = limits; l->name && l->option != what; l++)
328 ;
329 if (!l->name)
330 error("ulimit: internal error (%c)\n", what);
331
332 set = *argptr ? 1 : 0;
333 if (set) {
334 char *p = *argptr;
335
336 if (all || argptr[1])
337 error("ulimit: too many arguments\n");
338 if (strcmp(p, "unlimited") == 0)
339 val = RLIM_INFINITY;
340 else {
341 val = (rlim_t) 0;
342
343 while ((c = *p++) >= '0' && c <= '9')
344 {
345 val = (val * 10) + (long)(c - '0');
346 if (val < (rlim_t) 0)
347 break;
348 }
349 if (c)
350 error("ulimit: bad number\n");
351 val *= l->factor;
352 }
353 }
354 if (all) {
355 for (l = limits; l->name; l++) {
356 getrlimit(l->cmd, &limit);
357 if (how & SOFT)
358 val = limit.rlim_cur;
359 else if (how & HARD)
360 val = limit.rlim_max;
361
362 out1fmt("%-20s ", l->name);
363 if (val == RLIM_INFINITY)
364 out1fmt("unlimited\n");
365 else
366 {
367 val /= l->factor;
368 #ifdef BSD4_4
369 out1fmt("%qd\n", (long long) val);
370 #else
371 out1fmt("%ld\n", (long) val);
372 #endif
373 }
374 }
375 return 0;
376 }
377
378 getrlimit(l->cmd, &limit);
379 if (set) {
380 if (how & SOFT)
381 limit.rlim_cur = val;
382 if (how & HARD)
383 limit.rlim_max = val;
384 if (setrlimit(l->cmd, &limit) < 0)
385 error("ulimit: bad limit\n");
386 } else {
387 if (how & SOFT)
388 val = limit.rlim_cur;
389 else if (how & HARD)
390 val = limit.rlim_max;
391
392 if (val == RLIM_INFINITY)
393 out1fmt("unlimited\n");
394 else
395 {
396 val /= l->factor;
397 #ifdef BSD4_4
398 out1fmt("%qd\n", (long long) val);
399 #else
400 out1fmt("%ld\n", (long) val);
401 #endif
402 }
403 }
404 return 0;
405 }
406