miscbltin.c revision 1.26 1 /* $NetBSD: miscbltin.c,v 1.26 1998/09/24 17:49:48 itohy 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.26 1998/09/24 17:49:48 itohy 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((unsigned char)*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
234 INTOFF;
235 if ((set = setmode(ap)) != 0) {
236 mask = getmode(set, ~mask & 0777);
237 ckfree(set);
238 }
239 INTON;
240 if (!set)
241 error("Illegal mode: %s", ap);
242
243 umask(~mask & 0777);
244 }
245 }
246 return 0;
247 }
248
249 /*
250 * ulimit builtin
251 *
252 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
253 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
254 * ash by J.T. Conklin.
255 *
256 * Public domain.
257 */
258
259 struct limits {
260 const char *name;
261 int cmd;
262 int factor; /* multiply by to get rlim_{cur,max} values */
263 char option;
264 };
265
266 static const struct limits limits[] = {
267 #ifdef RLIMIT_CPU
268 { "time(seconds)", RLIMIT_CPU, 1, 't' },
269 #endif
270 #ifdef RLIMIT_FSIZE
271 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
272 #endif
273 #ifdef RLIMIT_DATA
274 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
275 #endif
276 #ifdef RLIMIT_STACK
277 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
278 #endif
279 #ifdef RLIMIT_CORE
280 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
281 #endif
282 #ifdef RLIMIT_RSS
283 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
284 #endif
285 #ifdef RLIMIT_MEMLOCK
286 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
287 #endif
288 #ifdef RLIMIT_NPROC
289 { "process(processes)", RLIMIT_NPROC, 1, 'p' },
290 #endif
291 #ifdef RLIMIT_NOFILE
292 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
293 #endif
294 #ifdef RLIMIT_VMEM
295 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
296 #endif
297 #ifdef RLIMIT_SWAP
298 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
299 #endif
300 { (char *) 0, 0, 0, '\0' }
301 };
302
303 int
304 ulimitcmd(argc, argv)
305 int argc;
306 char **argv;
307 {
308 int c;
309 rlim_t val = 0;
310 enum { SOFT = 0x1, HARD = 0x2 }
311 how = SOFT | HARD;
312 const struct limits *l;
313 int set, all = 0;
314 int optc, what;
315 struct rlimit limit;
316
317 what = 'f';
318 while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
319 switch (optc) {
320 case 'H':
321 how = HARD;
322 break;
323 case 'S':
324 how = SOFT;
325 break;
326 case 'a':
327 all = 1;
328 break;
329 default:
330 what = optc;
331 }
332
333 for (l = limits; l->name && l->option != what; l++)
334 ;
335 if (!l->name)
336 error("ulimit: internal error (%c)\n", what);
337
338 set = *argptr ? 1 : 0;
339 if (set) {
340 char *p = *argptr;
341
342 if (all || argptr[1])
343 error("ulimit: too many arguments\n");
344 if (strcmp(p, "unlimited") == 0)
345 val = RLIM_INFINITY;
346 else {
347 val = (rlim_t) 0;
348
349 while ((c = *p++) >= '0' && c <= '9')
350 {
351 val = (val * 10) + (long)(c - '0');
352 if (val < (rlim_t) 0)
353 break;
354 }
355 if (c)
356 error("ulimit: bad number\n");
357 val *= l->factor;
358 }
359 }
360 if (all) {
361 for (l = limits; l->name; l++) {
362 getrlimit(l->cmd, &limit);
363 if (how & SOFT)
364 val = limit.rlim_cur;
365 else if (how & HARD)
366 val = limit.rlim_max;
367
368 out1fmt("%-20s ", l->name);
369 if (val == RLIM_INFINITY)
370 out1fmt("unlimited\n");
371 else
372 {
373 val /= l->factor;
374 #ifdef BSD4_4
375 out1fmt("%qd\n", (long long) val);
376 #else
377 out1fmt("%ld\n", (long) val);
378 #endif
379 }
380 }
381 return 0;
382 }
383
384 getrlimit(l->cmd, &limit);
385 if (set) {
386 if (how & SOFT)
387 limit.rlim_cur = val;
388 if (how & HARD)
389 limit.rlim_max = val;
390 if (setrlimit(l->cmd, &limit) < 0)
391 error("ulimit: bad limit\n");
392 } else {
393 if (how & SOFT)
394 val = limit.rlim_cur;
395 else if (how & HARD)
396 val = limit.rlim_max;
397
398 if (val == RLIM_INFINITY)
399 out1fmt("unlimited\n");
400 else
401 {
402 val /= l->factor;
403 #ifdef BSD4_4
404 out1fmt("%qd\n", (long long) val);
405 #else
406 out1fmt("%ld\n", (long) val);
407 #endif
408 }
409 }
410 return 0;
411 }
412