xref: /dragonfly/bin/sh/miscbltin.c (revision e7d467f4)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)miscbltin.c	8.4 (Berkeley) 5/4/95
37  * $FreeBSD: head/bin/sh/miscbltin.c 246167 2013-01-31 22:10:57Z jilles $
38  */
39 
40 /*
41  * Miscellaneous builtins.
42  */
43 
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <unistd.h>
49 #include <errno.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 #include "shell.h"
55 #include "options.h"
56 #include "var.h"
57 #include "output.h"
58 #include "memalloc.h"
59 #include "error.h"
60 #include "mystring.h"
61 #include "syntax.h"
62 
63 int readcmd(int, char **);
64 int umaskcmd(int, char **);
65 int ulimitcmd(int, char **);
66 
67 #undef eflag
68 
69 /*
70  * The read builtin.  The -r option causes backslashes to be treated like
71  * ordinary characters.
72  *
73  * This uses unbuffered input, which may be avoidable in some cases.
74  *
75  * Note that if IFS=' :' then read x y should work so that:
76  * 'a b'	x='a', y='b'
77  * ' a b '	x='a', y='b'
78  * ':b'		x='',  y='b'
79  * ':'		x='',  y=''
80  * '::'		x='',  y=''
81  * ': :'	x='',  y=''
82  * ':::'	x='',  y='::'
83  * ':b c:'	x='',  y='b c:'
84  */
85 
86 int
87 readcmd(int argc __unused, char **argv __unused)
88 {
89 	char **ap;
90 	int backslash;
91 	char c;
92 	int rflag;
93 	const char *prompt;
94 	const char *ifs;
95 	char *p;
96 	int startword;
97 	int status;
98 	int i;
99 	int is_ifs;
100 	int saveall = 0;
101 	struct timeval tv;
102 	char *tvptr;
103 	fd_set ifds;
104 
105 	rflag = 0;
106 	prompt = NULL;
107 	tv.tv_sec = -1;
108 	tv.tv_usec = 0;
109 	while ((i = nextopt("erp:t:")) != '\0') {
110 		switch(i) {
111 		case 'p':
112 			prompt = shoptarg;
113 			break;
114 		case 'e':
115 			break;
116 		case 'r':
117 			rflag = 1;
118 			break;
119 		case 't':
120 			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
121 			if (tvptr == shoptarg)
122 				error("timeout value");
123 			switch(*tvptr) {
124 			case 0:
125 			case 's':
126 				break;
127 			case 'h':
128 				tv.tv_sec *= 60;
129 				/* FALLTHROUGH */
130 			case 'm':
131 				tv.tv_sec *= 60;
132 				break;
133 			default:
134 				error("timeout unit");
135 			}
136 			break;
137 		}
138 	}
139 	if (prompt && isatty(0)) {
140 		out2str(prompt);
141 		flushall();
142 	}
143 	if (*(ap = argptr) == NULL)
144 		error("arg count");
145 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
146 		ifs = " \t\n";
147 
148 	if (tv.tv_sec >= 0) {
149 		/*
150 		 * Wait for something to become available.
151 		 */
152 		FD_ZERO(&ifds);
153 		FD_SET(0, &ifds);
154 		status = select(1, &ifds, NULL, NULL, &tv);
155 		/*
156 		 * If there's nothing ready, return an error.
157 		 */
158 		if (status <= 0)
159 			return(1);
160 	}
161 
162 	status = 0;
163 	startword = 2;
164 	backslash = 0;
165 	STARTSTACKSTR(p);
166 	for (;;) {
167 		if (read(STDIN_FILENO, &c, 1) != 1) {
168 			status = 1;
169 			break;
170 		}
171 		if (c == '\0')
172 			continue;
173 		CHECKSTRSPACE(1, p);
174 		if (backslash) {
175 			backslash = 0;
176 			startword = 0;
177 			if (c != '\n')
178 				USTPUTC(c, p);
179 			continue;
180 		}
181 		if (!rflag && c == '\\') {
182 			backslash++;
183 			continue;
184 		}
185 		if (c == '\n')
186 			break;
187 		if (strchr(ifs, c))
188 			is_ifs = strchr(" \t\n", c) ? 1 : 2;
189 		else
190 			is_ifs = 0;
191 
192 		if (startword != 0) {
193 			if (is_ifs == 1) {
194 				/* Ignore leading IFS whitespace */
195 				if (saveall)
196 					USTPUTC(c, p);
197 				continue;
198 			}
199 			if (is_ifs == 2 && startword == 1) {
200 				/* Only one non-whitespace IFS per word */
201 				startword = 2;
202 				if (saveall)
203 					USTPUTC(c, p);
204 				continue;
205 			}
206 		}
207 
208 		if (is_ifs == 0) {
209 			/* append this character to the current variable */
210 			startword = 0;
211 			if (saveall)
212 				/* Not just a spare terminator */
213 				saveall++;
214 			USTPUTC(c, p);
215 			continue;
216 		}
217 
218 		/* end of variable... */
219 		startword = is_ifs;
220 
221 		if (ap[1] == NULL) {
222 			/* Last variable needs all IFS chars */
223 			saveall++;
224 			USTPUTC(c, p);
225 			continue;
226 		}
227 
228 		STACKSTRNUL(p);
229 		setvar(*ap, stackblock(), 0);
230 		ap++;
231 		STARTSTACKSTR(p);
232 	}
233 	STACKSTRNUL(p);
234 
235 	/* Remove trailing IFS chars */
236 	for (; stackblock() <= --p; *p = 0) {
237 		if (!strchr(ifs, *p))
238 			break;
239 		if (strchr(" \t\n", *p))
240 			/* Always remove whitespace */
241 			continue;
242 		if (saveall > 1)
243 			/* Don't remove non-whitespace unless it was naked */
244 			break;
245 	}
246 	setvar(*ap, stackblock(), 0);
247 
248 	/* Set any remaining args to "" */
249 	while (*++ap != NULL)
250 		setvar(*ap, nullstr, 0);
251 	return status;
252 }
253 
254 
255 
256 int
257 umaskcmd(int argc __unused, char **argv __unused)
258 {
259 	char *ap;
260 	int mask;
261 	int i;
262 	int symbolic_mode = 0;
263 
264 	while ((i = nextopt("S")) != '\0') {
265 		symbolic_mode = 1;
266 	}
267 
268 	INTOFF;
269 	mask = umask(0);
270 	umask(mask);
271 	INTON;
272 
273 	if ((ap = *argptr) == NULL) {
274 		if (symbolic_mode) {
275 			char u[4], g[4], o[4];
276 
277 			i = 0;
278 			if ((mask & S_IRUSR) == 0)
279 				u[i++] = 'r';
280 			if ((mask & S_IWUSR) == 0)
281 				u[i++] = 'w';
282 			if ((mask & S_IXUSR) == 0)
283 				u[i++] = 'x';
284 			u[i] = '\0';
285 
286 			i = 0;
287 			if ((mask & S_IRGRP) == 0)
288 				g[i++] = 'r';
289 			if ((mask & S_IWGRP) == 0)
290 				g[i++] = 'w';
291 			if ((mask & S_IXGRP) == 0)
292 				g[i++] = 'x';
293 			g[i] = '\0';
294 
295 			i = 0;
296 			if ((mask & S_IROTH) == 0)
297 				o[i++] = 'r';
298 			if ((mask & S_IWOTH) == 0)
299 				o[i++] = 'w';
300 			if ((mask & S_IXOTH) == 0)
301 				o[i++] = 'x';
302 			o[i] = '\0';
303 
304 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
305 		} else {
306 			out1fmt("%.4o\n", mask);
307 		}
308 	} else {
309 		if (is_digit(*ap)) {
310 			mask = 0;
311 			do {
312 				if (*ap >= '8' || *ap < '0')
313 					error("Illegal number: %s", *argptr);
314 				mask = (mask << 3) + (*ap - '0');
315 			} while (*++ap != '\0');
316 			umask(mask);
317 		} else {
318 			void *set;
319 			INTOFF;
320 			if ((set = setmode (ap)) == NULL)
321 				error("Illegal number: %s", ap);
322 
323 			mask = getmode (set, ~mask & 0777);
324 			umask(~mask & 0777);
325 			free(set);
326 			INTON;
327 		}
328 	}
329 	return 0;
330 }
331 
332 /*
333  * ulimit builtin
334  *
335  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
336  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
337  * ash by J.T. Conklin.
338  *
339  * Public domain.
340  */
341 
342 struct limits {
343 	const char *name;
344 	const char *units;
345 	int	cmd;
346 	int	factor;	/* multiply by to get rlim_{cur,max} values */
347 	char	option;
348 };
349 
350 static const struct limits limits[] = {
351 #ifdef RLIMIT_CPU
352 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
353 #endif
354 #ifdef RLIMIT_FSIZE
355 	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
356 #endif
357 #ifdef RLIMIT_DATA
358 	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
359 #endif
360 #ifdef RLIMIT_STACK
361 	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
362 #endif
363 #ifdef  RLIMIT_CORE
364 	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
365 #endif
366 #ifdef RLIMIT_RSS
367 	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
368 #endif
369 #ifdef RLIMIT_MEMLOCK
370 	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
371 #endif
372 #ifdef RLIMIT_NPROC
373 	{ "max user processes",	NULL,		RLIMIT_NPROC,      1, 'u' },
374 #endif
375 #ifdef RLIMIT_NOFILE
376 	{ "open files",		NULL,		RLIMIT_NOFILE,     1, 'n' },
377 #endif
378 #ifdef RLIMIT_VMEM
379 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
380 #endif
381 #ifdef RLIMIT_SWAP
382 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
383 #endif
384 #ifdef RLIMIT_SBSIZE
385 	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
386 #endif
387 #ifdef RLIMIT_POSIXLOCK
388 	{ "posixlocks",		NULL,		RLIMIT_POSIXLOCK,  1, 'k' },
389 #endif
390 	{ NULL,			NULL,		0,		   0, '\0' }
391 };
392 
393 int
394 ulimitcmd(int argc __unused, char **argv __unused)
395 {
396 	int	c;
397 	rlim_t val = 0;
398 	enum { SOFT = 0x1, HARD = 0x2 }
399 			how = SOFT | HARD;
400 	const struct limits	*l;
401 	int		set, all = 0;
402 	int		optc, what;
403 	struct rlimit	limit;
404 
405 	what = 'f';
406 	while ((optc = nextopt("HSatfdsmcnuvlbk")) != '\0')
407 		switch (optc) {
408 		case 'H':
409 			how = HARD;
410 			break;
411 		case 'S':
412 			how = SOFT;
413 			break;
414 		case 'a':
415 			all = 1;
416 			break;
417 		default:
418 			what = optc;
419 		}
420 
421 	for (l = limits; l->name && l->option != what; l++)
422 		;
423 	if (!l->name)
424 		error("internal error (%c)", what);
425 
426 	set = *argptr ? 1 : 0;
427 	if (set) {
428 		char *p = *argptr;
429 
430 		if (all || argptr[1])
431 			error("too many arguments");
432 		if (strcmp(p, "unlimited") == 0)
433 			val = RLIM_INFINITY;
434 		else {
435 			val = 0;
436 
437 			while ((c = *p++) >= '0' && c <= '9')
438 			{
439 				val = (val * 10) + (long)(c - '0');
440 				if (val < 0)
441 					break;
442 			}
443 			if (c)
444 				error("bad number");
445 			val *= l->factor;
446 		}
447 	}
448 	if (all) {
449 		for (l = limits; l->name; l++) {
450 			char optbuf[40];
451 			if (getrlimit(l->cmd, &limit) < 0)
452 				error("can't get limit: %s", strerror(errno));
453 			if (how & SOFT)
454 				val = limit.rlim_cur;
455 			else if (how & HARD)
456 				val = limit.rlim_max;
457 
458 			if (l->units)
459 				snprintf(optbuf, sizeof(optbuf),
460 					"(%s, -%c) ", l->units, l->option);
461 			else
462 				snprintf(optbuf, sizeof(optbuf),
463 					"(-%c) ", l->option);
464 			out1fmt("%-18s %18s ", l->name, optbuf);
465 			if (val == RLIM_INFINITY)
466 				out1str("unlimited\n");
467 			else
468 			{
469 				val /= l->factor;
470 				out1fmt("%jd\n", (intmax_t)val);
471 			}
472 		}
473 		return 0;
474 	}
475 
476 	if (getrlimit(l->cmd, &limit) < 0)
477 		error("can't get limit: %s", strerror(errno));
478 	if (set) {
479 		if (how & SOFT)
480 			limit.rlim_cur = val;
481 		if (how & HARD)
482 			limit.rlim_max = val;
483 		if (setrlimit(l->cmd, &limit) < 0)
484 			error("bad limit: %s", strerror(errno));
485 	} else {
486 		if (how & SOFT)
487 			val = limit.rlim_cur;
488 		else if (how & HARD)
489 			val = limit.rlim_max;
490 
491 		if (val == RLIM_INFINITY)
492 			out1str("unlimited\n");
493 		else
494 		{
495 			val /= l->factor;
496 			out1fmt("%jd\n", (intmax_t)val);
497 		}
498 	}
499 	return 0;
500 }
501