xref: /freebsd/bin/sh/miscbltin.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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  *	$Id: miscbltin.c,v 1.13 1997/05/19 00:18:43 steve Exp $
37  */
38 
39 #ifndef lint
40 static char const sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
41 #endif /* not lint */
42 
43 /*
44  * Miscelaneous builtins.
45  */
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <unistd.h>
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <termios.h>
56 
57 #include "shell.h"
58 #include "options.h"
59 #include "var.h"
60 #include "output.h"
61 #include "memalloc.h"
62 #include "error.h"
63 #include "mystring.h"
64 
65 #undef eflag
66 
67 extern char **argptr;		/* argument list for builtin command */
68 
69 
70 /*
71  * The read builtin.  The -e option causes backslashes to escape the
72  * following character.
73  *
74  * This uses unbuffered input, which may be avoidable in some cases.
75  */
76 
77 int
78 readcmd(argc, argv)
79 	int argc __unused;
80 	char **argv __unused;
81 {
82 	char **ap;
83 	int backslash;
84 	char c;
85 	int eflag;
86 	char *prompt;
87 	char *ifs;
88 	char *p;
89 	int startword;
90 	int status;
91 	int i;
92 	struct timeval tv;
93 	char *tvptr;
94 	fd_set ifds;
95 	struct termios told, tnew;
96 	int tsaved;
97 
98 	eflag = 0;
99 	prompt = NULL;
100 	tv.tv_sec = -1;
101 	tv.tv_usec = 0;
102 	while ((i = nextopt("ep:t:")) != '\0') {
103 		switch(i) {
104 		case 'p':
105 			prompt = optarg;
106 			break;
107 		case 'e':
108 			eflag = 1;
109 			break;
110 		case 't':
111 			tv.tv_sec = strtol(optarg, &tvptr, 0);
112 			if (tvptr == optarg)
113 				error("timeout value");
114 			switch(*tvptr) {
115 			case 0:
116 			case 's':
117 				break;
118 			case 'h':
119 				tv.tv_sec *= 60;
120 				/* FALLTHROUGH */
121 			case 'm':
122 				tv.tv_sec *= 60;
123 				break;
124 			default:
125 				error("timeout unit");
126 			}
127 			break;
128 		}
129 	}
130 	if (prompt && isatty(0)) {
131 		out2str(prompt);
132 		flushall();
133 	}
134 	if (*(ap = argptr) == NULL)
135 		error("arg count");
136 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
137 		ifs = nullstr;
138 
139 	if (tv.tv_sec >= 0) {
140 		/*
141 		 * See if we can disable input processing; this will
142 		 * not give the desired result if we are in a pipeline
143 		 * and someone upstream is still in line-by-line mode.
144 		 */
145 		tsaved = 0;
146 		if (tcgetattr(0, &told) == 0) {
147 			memcpy(&tnew, &told, sizeof(told));
148 			cfmakeraw(&tnew);
149 			tcsetattr(0, TCSANOW, &tnew);
150 			tsaved = 1;
151 		}
152 		/*
153 		 * Wait for something to become available.
154 		 */
155 		FD_ZERO(&ifds);
156 		FD_SET(0, &ifds);
157 		status = select(1, &ifds, NULL, NULL, &tv);
158 		if (tsaved)
159 			tcsetattr(0, TCSANOW, &told);
160 		/*
161 		 * If there's nothing ready, return an error.
162 		 */
163 		if (status <= 0)
164 			return(1);
165 	}
166 
167 	status = 0;
168 	startword = 1;
169 	backslash = 0;
170 	STARTSTACKSTR(p);
171 	for (;;) {
172 		if (read(0, &c, 1) != 1) {
173 			status = 1;
174 			break;
175 		}
176 		if (c == '\0')
177 			continue;
178 		if (backslash) {
179 			backslash = 0;
180 			if (c != '\n')
181 				STPUTC(c, p);
182 			continue;
183 		}
184 		if (eflag && c == '\\') {
185 			backslash++;
186 			continue;
187 		}
188 		if (c == '\n')
189 			break;
190 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
191 			continue;
192 		}
193 		startword = 0;
194 		if (backslash && c == '\\') {
195 			if (read(0, &c, 1) != 1) {
196 				status = 1;
197 				break;
198 			}
199 			STPUTC(c, p);
200 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
201 			STACKSTRNUL(p);
202 			setvar(*ap, stackblock(), 0);
203 			ap++;
204 			startword = 1;
205 			STARTSTACKSTR(p);
206 		} else {
207 			STPUTC(c, p);
208 		}
209 	}
210 	STACKSTRNUL(p);
211 	setvar(*ap, stackblock(), 0);
212 	while (*++ap != NULL)
213 		setvar(*ap, nullstr, 0);
214 	return status;
215 }
216 
217 
218 
219 int
220 umaskcmd(argc, argv)
221 	int argc __unused;
222 	char **argv;
223 {
224 	char *ap;
225 	int mask;
226 	int i;
227 	int symbolic_mode = 0;
228 
229 	while ((i = nextopt("S")) != '\0') {
230 		symbolic_mode = 1;
231 	}
232 
233 	INTOFF;
234 	mask = umask(0);
235 	umask(mask);
236 	INTON;
237 
238 	if ((ap = *argptr) == NULL) {
239 		if (symbolic_mode) {
240 			char u[4], g[4], o[4];
241 
242 			i = 0;
243 			if ((mask & S_IRUSR) == 0)
244 				u[i++] = 'r';
245 			if ((mask & S_IWUSR) == 0)
246 				u[i++] = 'w';
247 			if ((mask & S_IXUSR) == 0)
248 				u[i++] = 'x';
249 			u[i] = '\0';
250 
251 			i = 0;
252 			if ((mask & S_IRGRP) == 0)
253 				g[i++] = 'r';
254 			if ((mask & S_IWGRP) == 0)
255 				g[i++] = 'w';
256 			if ((mask & S_IXGRP) == 0)
257 				g[i++] = 'x';
258 			g[i] = '\0';
259 
260 			i = 0;
261 			if ((mask & S_IROTH) == 0)
262 				o[i++] = 'r';
263 			if ((mask & S_IWOTH) == 0)
264 				o[i++] = 'w';
265 			if ((mask & S_IXOTH) == 0)
266 				o[i++] = 'x';
267 			o[i] = '\0';
268 
269 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
270 		} else {
271 			out1fmt("%.4o\n", mask);
272 		}
273 	} else {
274 		if (isdigit(*ap)) {
275 			mask = 0;
276 			do {
277 				if (*ap >= '8' || *ap < '0')
278 					error("Illegal number: %s", argv[1]);
279 				mask = (mask << 3) + (*ap - '0');
280 			} while (*++ap != '\0');
281 			umask(mask);
282 		} else {
283 			void *set;
284 			if ((set = setmode (ap)) == 0)
285 					error("Illegal number: %s", ap);
286 
287 			mask = getmode (set, ~mask & 0777);
288 			umask(~mask & 0777);
289 		}
290 	}
291 	return 0;
292 }
293 
294 /*
295  * ulimit builtin
296  *
297  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
298  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
299  * ash by J.T. Conklin.
300  *
301  * Public domain.
302  */
303 
304 struct limits {
305 	const char *name;
306 	const char *units;
307 	int	cmd;
308 	int	factor;	/* multiply by to get rlim_{cur,max} values */
309 	char	option;
310 };
311 
312 static const struct limits limits[] = {
313 #ifdef RLIMIT_CPU
314 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
315 #endif
316 #ifdef RLIMIT_FSIZE
317 	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
318 #endif
319 #ifdef RLIMIT_DATA
320 	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
321 #endif
322 #ifdef RLIMIT_STACK
323 	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
324 #endif
325 #ifdef  RLIMIT_CORE
326 	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
327 #endif
328 #ifdef RLIMIT_RSS
329 	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
330 #endif
331 #ifdef RLIMIT_MEMLOCK
332 	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
333 #endif
334 #ifdef RLIMIT_NPROC
335 	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
336 #endif
337 #ifdef RLIMIT_NOFILE
338 	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
339 #endif
340 #ifdef RLIMIT_VMEM
341 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
342 #endif
343 #ifdef RLIMIT_SWAP
344 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
345 #endif
346 	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
347 };
348 
349 int
350 ulimitcmd(argc, argv)
351 	int argc __unused;
352 	char **argv __unused;
353 {
354 	int	c;
355 	quad_t val = 0;
356 	enum { SOFT = 0x1, HARD = 0x2 }
357 			how = SOFT | HARD;
358 	const struct limits	*l;
359 	int		set, all = 0;
360 	int		optc, what;
361 	struct rlimit	limit;
362 
363 	what = 'f';
364 	while ((optc = nextopt("HSatfdsmcnul")) != '\0')
365 		switch (optc) {
366 		case 'H':
367 			how = HARD;
368 			break;
369 		case 'S':
370 			how = SOFT;
371 			break;
372 		case 'a':
373 			all = 1;
374 			break;
375 		default:
376 			what = optc;
377 		}
378 
379 	for (l = limits; l->name && l->option != what; l++)
380 		;
381 	if (!l->name)
382 		error("ulimit: internal error (%c)", what);
383 
384 	set = *argptr ? 1 : 0;
385 	if (set) {
386 		char *p = *argptr;
387 
388 		if (all || argptr[1])
389 			error("ulimit: too many arguments");
390 		if (strcmp(p, "unlimited") == 0)
391 			val = RLIM_INFINITY;
392 		else {
393 			val = (quad_t) 0;
394 
395 			while ((c = *p++) >= '0' && c <= '9')
396 			{
397 				val = (val * 10) + (long)(c - '0');
398 				if (val < (quad_t) 0)
399 					break;
400 			}
401 			if (c)
402 				error("ulimit: bad number");
403 			val *= l->factor;
404 		}
405 	}
406 	if (all) {
407 		for (l = limits; l->name; l++) {
408 			char optbuf[40];
409 			if (getrlimit(l->cmd, &limit) < 0)
410 				error("ulimit: can't get limit: %s", strerror(errno));
411 			if (how & SOFT)
412 				val = limit.rlim_cur;
413 			else if (how & HARD)
414 				val = limit.rlim_max;
415 
416 			if (l->units)
417 				snprintf(optbuf, sizeof(optbuf),
418 					"(%s, -%c) ", l->units, l->option);
419 			else
420 				snprintf(optbuf, sizeof(optbuf),
421 					"(-%c) ", l->option);
422 			out1fmt("%-18s %18s ", l->name, optbuf);
423 			if (val == RLIM_INFINITY)
424 				out1fmt("unlimited\n");
425 			else
426 			{
427 				val /= l->factor;
428 				out1fmt("%qd\n", (quad_t) val);
429 			}
430 		}
431 		return 0;
432 	}
433 
434 	if (getrlimit(l->cmd, &limit) < 0)
435 		error("ulimit: can't get limit: %s", strerror(errno));
436 	if (set) {
437 		if (how & SOFT)
438 			limit.rlim_cur = val;
439 		if (how & HARD)
440 			limit.rlim_max = val;
441 		if (setrlimit(l->cmd, &limit) < 0)
442 			error("ulimit: bad limit: %s", strerror(errno));
443 	} else {
444 		if (how & SOFT)
445 			val = limit.rlim_cur;
446 		else if (how & HARD)
447 			val = limit.rlim_max;
448 
449 		if (val == RLIM_INFINITY)
450 			out1fmt("unlimited\n");
451 		else
452 		{
453 			val /= l->factor;
454 			out1fmt("%qd\n", (quad_t) val);
455 		}
456 	}
457 	return 0;
458 }
459