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