11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
411556Srgrimes/*
4246684Skris * Miscellaneous builtins.
431556Srgrimes */
441556Srgrimes
4517987Speter#include <sys/types.h>
4617987Speter#include <sys/stat.h>
4717987Speter#include <sys/time.h>
4817987Speter#include <sys/resource.h>
4917987Speter#include <unistd.h>
5018016Speter#include <errno.h>
51104282Smux#include <stdint.h>
5218018Speter#include <stdio.h>
5338536Scracauer#include <stdlib.h>
5417987Speter
551556Srgrimes#include "shell.h"
561556Srgrimes#include "options.h"
571556Srgrimes#include "var.h"
581556Srgrimes#include "output.h"
591556Srgrimes#include "memalloc.h"
601556Srgrimes#include "error.h"
611556Srgrimes#include "mystring.h"
62246167Sjilles#include "syntax.h"
63250214Sjilles#include "trap.h"
641556Srgrimes
651556Srgrimes#undef eflag
661556Srgrimes
67149018Sstefanfint readcmd(int, char **);
68149018Sstefanfint umaskcmd(int, char **);
69149018Sstefanfint ulimitcmd(int, char **);
70149018Sstefanf
711556Srgrimes/*
7250394Stg * The read builtin.  The -r option causes backslashes to be treated like
7350394Stg * ordinary characters.
741556Srgrimes *
751556Srgrimes * This uses unbuffered input, which may be avoidable in some cases.
76190295Sstefanf *
77190295Sstefanf * Note that if IFS=' :' then read x y should work so that:
78190295Sstefanf * 'a b'	x='a', y='b'
79190295Sstefanf * ' a b '	x='a', y='b'
80190295Sstefanf * ':b'		x='',  y='b'
81190295Sstefanf * ':'		x='',  y=''
82190295Sstefanf * '::'		x='',  y=''
83190295Sstefanf * ': :'	x='',  y=''
84190295Sstefanf * ':::'	x='',  y='::'
85190295Sstefanf * ':b c:'	x='',  y='b c:'
861556Srgrimes */
871556Srgrimes
8817987Speterint
8990111Simpreadcmd(int argc __unused, char **argv __unused)
9017987Speter{
911556Srgrimes	char **ap;
921556Srgrimes	int backslash;
931556Srgrimes	char c;
9450394Stg	int rflag;
951556Srgrimes	char *prompt;
96201053Sjilles	const char *ifs;
971556Srgrimes	char *p;
981556Srgrimes	int startword;
991556Srgrimes	int status;
1001556Srgrimes	int i;
101190295Sstefanf	int is_ifs;
102190295Sstefanf	int saveall = 0;
10329983Smsmith	struct timeval tv;
10429983Smsmith	char *tvptr;
10529983Smsmith	fd_set ifds;
106250214Sjilles	ssize_t nread;
107250214Sjilles	int sig;
1081556Srgrimes
10950394Stg	rflag = 0;
1101556Srgrimes	prompt = NULL;
11129983Smsmith	tv.tv_sec = -1;
11229983Smsmith	tv.tv_usec = 0;
11350394Stg	while ((i = nextopt("erp:t:")) != '\0') {
11429983Smsmith		switch(i) {
11529983Smsmith		case 'p':
11659436Scracauer			prompt = shoptarg;
11729983Smsmith			break;
11829983Smsmith		case 'e':
11929983Smsmith			break;
12050394Stg		case 'r':
12150394Stg			rflag = 1;
12250394Stg			break;
12329983Smsmith		case 't':
12459436Scracauer			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
12559436Scracauer			if (tvptr == shoptarg)
12629983Smsmith				error("timeout value");
12729983Smsmith			switch(*tvptr) {
12829983Smsmith			case 0:
12929983Smsmith			case 's':
13029983Smsmith				break;
13129983Smsmith			case 'h':
13229983Smsmith				tv.tv_sec *= 60;
13329983Smsmith				/* FALLTHROUGH */
13429983Smsmith			case 'm':
13529983Smsmith				tv.tv_sec *= 60;
13629983Smsmith				break;
13729983Smsmith			default:
13829983Smsmith				error("timeout unit");
13929983Smsmith			}
14029983Smsmith			break;
14129983Smsmith		}
1421556Srgrimes	}
1431556Srgrimes	if (prompt && isatty(0)) {
1441556Srgrimes		out2str(prompt);
1451556Srgrimes		flushall();
1461556Srgrimes	}
1471556Srgrimes	if (*(ap = argptr) == NULL)
1481556Srgrimes		error("arg count");
1491556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
150190298Sstefanf		ifs = " \t\n";
15129983Smsmith
15229983Smsmith	if (tv.tv_sec >= 0) {
15329983Smsmith		/*
15429983Smsmith		 * Wait for something to become available.
15529983Smsmith		 */
15629983Smsmith		FD_ZERO(&ifds);
15729983Smsmith		FD_SET(0, &ifds);
15829983Smsmith		status = select(1, &ifds, NULL, NULL, &tv);
15929983Smsmith		/*
16029983Smsmith		 * If there's nothing ready, return an error.
16129983Smsmith		 */
162250214Sjilles		if (status <= 0) {
163250214Sjilles			sig = pendingsig;
164250214Sjilles			return (128 + (sig != 0 ? sig : SIGALRM));
165250214Sjilles		}
16629983Smsmith	}
16729983Smsmith
1681556Srgrimes	status = 0;
169190295Sstefanf	startword = 2;
1701556Srgrimes	backslash = 0;
1711556Srgrimes	STARTSTACKSTR(p);
1721556Srgrimes	for (;;) {
173250214Sjilles		nread = read(STDIN_FILENO, &c, 1);
174250214Sjilles		if (nread == -1) {
175250214Sjilles			if (errno == EINTR) {
176250214Sjilles				sig = pendingsig;
177250214Sjilles				if (sig == 0)
178250214Sjilles					continue;
179250214Sjilles				status = 128 + sig;
180250214Sjilles				break;
181250214Sjilles			}
182250214Sjilles			warning("read error: %s", strerror(errno));
183250214Sjilles			status = 2;
184250214Sjilles			break;
185250214Sjilles		} else if (nread != 1) {
1861556Srgrimes			status = 1;
1871556Srgrimes			break;
1881556Srgrimes		}
1891556Srgrimes		if (c == '\0')
1901556Srgrimes			continue;
191215783Sjilles		CHECKSTRSPACE(1, p);
1921556Srgrimes		if (backslash) {
1931556Srgrimes			backslash = 0;
194212339Sjilles			startword = 0;
1951556Srgrimes			if (c != '\n')
196215783Sjilles				USTPUTC(c, p);
1971556Srgrimes			continue;
1981556Srgrimes		}
19950394Stg		if (!rflag && c == '\\') {
2001556Srgrimes			backslash++;
2011556Srgrimes			continue;
2021556Srgrimes		}
2031556Srgrimes		if (c == '\n')
2041556Srgrimes			break;
205190295Sstefanf		if (strchr(ifs, c))
206190295Sstefanf			is_ifs = strchr(" \t\n", c) ? 1 : 2;
207190295Sstefanf		else
208190295Sstefanf			is_ifs = 0;
209190295Sstefanf
210190295Sstefanf		if (startword != 0) {
211190295Sstefanf			if (is_ifs == 1) {
212190295Sstefanf				/* Ignore leading IFS whitespace */
213190295Sstefanf				if (saveall)
214215783Sjilles					USTPUTC(c, p);
215190295Sstefanf				continue;
216190295Sstefanf			}
217190295Sstefanf			if (is_ifs == 2 && startword == 1) {
218190295Sstefanf				/* Only one non-whitespace IFS per word */
219190295Sstefanf				startword = 2;
220190295Sstefanf				if (saveall)
221215783Sjilles					USTPUTC(c, p);
222190295Sstefanf				continue;
223190295Sstefanf			}
224190295Sstefanf		}
225190295Sstefanf
226190295Sstefanf		if (is_ifs == 0) {
227190295Sstefanf			/* append this character to the current variable */
228190295Sstefanf			startword = 0;
229190295Sstefanf			if (saveall)
230190295Sstefanf				/* Not just a spare terminator */
231190295Sstefanf				saveall++;
232215783Sjilles			USTPUTC(c, p);
2331556Srgrimes			continue;
2341556Srgrimes		}
235190295Sstefanf
236190295Sstefanf		/* end of variable... */
237190295Sstefanf		startword = is_ifs;
238190295Sstefanf
239190295Sstefanf		if (ap[1] == NULL) {
240190295Sstefanf			/* Last variable needs all IFS chars */
241190295Sstefanf			saveall++;
242215783Sjilles			USTPUTC(c, p);
243190295Sstefanf			continue;
2441556Srgrimes		}
245190295Sstefanf
246190295Sstefanf		STACKSTRNUL(p);
247190295Sstefanf		setvar(*ap, stackblock(), 0);
248190295Sstefanf		ap++;
249190295Sstefanf		STARTSTACKSTR(p);
2501556Srgrimes	}
2511556Srgrimes	STACKSTRNUL(p);
252190295Sstefanf
253190295Sstefanf	/* Remove trailing IFS chars */
254190295Sstefanf	for (; stackblock() <= --p; *p = 0) {
255190295Sstefanf		if (!strchr(ifs, *p))
256190295Sstefanf			break;
257190295Sstefanf		if (strchr(" \t\n", *p))
258190295Sstefanf			/* Always remove whitespace */
259190295Sstefanf			continue;
260190295Sstefanf		if (saveall > 1)
261190295Sstefanf			/* Don't remove non-whitespace unless it was naked */
262190295Sstefanf			break;
263190295Sstefanf	}
2641556Srgrimes	setvar(*ap, stackblock(), 0);
265190295Sstefanf
266190295Sstefanf	/* Set any remaining args to "" */
2671556Srgrimes	while (*++ap != NULL)
2681556Srgrimes		setvar(*ap, nullstr, 0);
2691556Srgrimes	return status;
2701556Srgrimes}
2711556Srgrimes
2721556Srgrimes
2731556Srgrimes
27417987Speterint
275201053Sjillesumaskcmd(int argc __unused, char **argv __unused)
27617987Speter{
27717987Speter	char *ap;
2781556Srgrimes	int mask;
2791556Srgrimes	int i;
28017987Speter	int symbolic_mode = 0;
2811556Srgrimes
28217987Speter	while ((i = nextopt("S")) != '\0') {
28317987Speter		symbolic_mode = 1;
2841556Srgrimes	}
28511571Sjoerg
28617987Speter	INTOFF;
28717987Speter	mask = umask(0);
28817987Speter	umask(mask);
28917987Speter	INTON;
29011571Sjoerg
29117987Speter	if ((ap = *argptr) == NULL) {
29217987Speter		if (symbolic_mode) {
29317987Speter			char u[4], g[4], o[4];
29411571Sjoerg
29517987Speter			i = 0;
29617987Speter			if ((mask & S_IRUSR) == 0)
29717987Speter				u[i++] = 'r';
29817987Speter			if ((mask & S_IWUSR) == 0)
29917987Speter				u[i++] = 'w';
30017987Speter			if ((mask & S_IXUSR) == 0)
30117987Speter				u[i++] = 'x';
30217987Speter			u[i] = '\0';
30311571Sjoerg
30417987Speter			i = 0;
30517987Speter			if ((mask & S_IRGRP) == 0)
30617987Speter				g[i++] = 'r';
30717987Speter			if ((mask & S_IWGRP) == 0)
30817987Speter				g[i++] = 'w';
30917987Speter			if ((mask & S_IXGRP) == 0)
31017987Speter				g[i++] = 'x';
31117987Speter			g[i] = '\0';
31211571Sjoerg
31317987Speter			i = 0;
31417987Speter			if ((mask & S_IROTH) == 0)
31517987Speter				o[i++] = 'r';
31617987Speter			if ((mask & S_IWOTH) == 0)
31717987Speter				o[i++] = 'w';
31817987Speter			if ((mask & S_IXOTH) == 0)
31917987Speter				o[i++] = 'x';
32017987Speter			o[i] = '\0';
32111571Sjoerg
32217987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
32317987Speter		} else {
32417987Speter			out1fmt("%.4o\n", mask);
32517987Speter		}
32617987Speter	} else {
327246167Sjilles		if (is_digit(*ap)) {
32817987Speter			mask = 0;
32917987Speter			do {
33017987Speter				if (*ap >= '8' || *ap < '0')
331149918Sstefanf					error("Illegal number: %s", *argptr);
33217987Speter				mask = (mask << 3) + (*ap - '0');
33317987Speter			} while (*++ap != '\0');
33417987Speter			umask(mask);
33517987Speter		} else {
33620425Ssteve			void *set;
337151795Sstefanf			INTOFF;
33817987Speter			if ((set = setmode (ap)) == 0)
33941844Simp				error("Illegal number: %s", ap);
34011571Sjoerg
34117987Speter			mask = getmode (set, ~mask & 0777);
34217987Speter			umask(~mask & 0777);
34341844Simp			free(set);
344151795Sstefanf			INTON;
34517987Speter		}
34617987Speter	}
34711571Sjoerg	return 0;
34811571Sjoerg}
34911571Sjoerg
35017987Speter/*
35117987Speter * ulimit builtin
35217987Speter *
35317987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
35417987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
35517987Speter * ash by J.T. Conklin.
35617987Speter *
35717987Speter * Public domain.
35817987Speter */
35911571Sjoerg
36017987Speterstruct limits {
36117987Speter	const char *name;
36218016Speter	const char *units;
36317987Speter	int	cmd;
36417987Speter	int	factor;	/* multiply by to get rlim_{cur,max} values */
36517987Speter	char	option;
36617987Speter};
36711571Sjoerg
36817987Speterstatic const struct limits limits[] = {
36917987Speter#ifdef RLIMIT_CPU
37018016Speter	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
37117987Speter#endif
37217987Speter#ifdef RLIMIT_FSIZE
37318016Speter	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
37417987Speter#endif
37517987Speter#ifdef RLIMIT_DATA
37618016Speter	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
37717987Speter#endif
37817987Speter#ifdef RLIMIT_STACK
37918016Speter	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
38017987Speter#endif
38117987Speter#ifdef  RLIMIT_CORE
38218016Speter	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
38317987Speter#endif
38417987Speter#ifdef RLIMIT_RSS
38518016Speter	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
38617987Speter#endif
38717987Speter#ifdef RLIMIT_MEMLOCK
38818016Speter	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
38917987Speter#endif
39017987Speter#ifdef RLIMIT_NPROC
39118016Speter	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
39217987Speter#endif
39317987Speter#ifdef RLIMIT_NOFILE
39418016Speter	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
39517987Speter#endif
39617987Speter#ifdef RLIMIT_VMEM
39718016Speter	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
39817987Speter#endif
39917987Speter#ifdef RLIMIT_SWAP
40018016Speter	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
40117987Speter#endif
40252072Sgreen#ifdef RLIMIT_SBSIZE
40352072Sgreen	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
40452072Sgreen#endif
405181905Sed#ifdef RLIMIT_NPTS
406181905Sed	{ "pseudo-terminals",	(char *)0,	RLIMIT_NPTS,	   1, 'p' },
407181905Sed#endif
40818016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
40917987Speter};
41017987Speter
41117987Speterint
41290111Simpulimitcmd(int argc __unused, char **argv __unused)
41317987Speter{
41425222Ssteve	int	c;
415104282Smux	rlim_t val = 0;
41617987Speter	enum { SOFT = 0x1, HARD = 0x2 }
41717987Speter			how = SOFT | HARD;
41817987Speter	const struct limits	*l;
41917987Speter	int		set, all = 0;
42017987Speter	int		optc, what;
42117987Speter	struct rlimit	limit;
42211571Sjoerg
42317987Speter	what = 'f';
424194767Skib	while ((optc = nextopt("HSatfdsmcnuvlbpw")) != '\0')
42517987Speter		switch (optc) {
42611571Sjoerg		case 'H':
42717987Speter			how = HARD;
42811571Sjoerg			break;
42911571Sjoerg		case 'S':
43017987Speter			how = SOFT;
43111571Sjoerg			break;
43211571Sjoerg		case 'a':
43317987Speter			all = 1;
43411571Sjoerg			break;
43517987Speter		default:
43617987Speter			what = optc;
43711571Sjoerg		}
43811571Sjoerg
43917987Speter	for (l = limits; l->name && l->option != what; l++)
44017987Speter		;
44117987Speter	if (!l->name)
442104208Stjr		error("internal error (%c)", what);
44317987Speter
44417987Speter	set = *argptr ? 1 : 0;
44517987Speter	if (set) {
44617987Speter		char *p = *argptr;
44717987Speter
44817987Speter		if (all || argptr[1])
449104208Stjr			error("too many arguments");
45017987Speter		if (strcmp(p, "unlimited") == 0)
45111571Sjoerg			val = RLIM_INFINITY;
45211571Sjoerg		else {
453104282Smux			val = 0;
45417987Speter
45517987Speter			while ((c = *p++) >= '0' && c <= '9')
45617987Speter			{
45717987Speter				val = (val * 10) + (long)(c - '0');
458104282Smux				if (val < 0)
45917987Speter					break;
46017987Speter			}
46117987Speter			if (c)
462104208Stjr				error("bad number");
46317987Speter			val *= l->factor;
46411571Sjoerg		}
46517987Speter	}
46617987Speter	if (all) {
467155301Sschweikh		for (l = limits; l->name; l++) {
46818016Speter			char optbuf[40];
46918016Speter			if (getrlimit(l->cmd, &limit) < 0)
470104208Stjr				error("can't get limit: %s", strerror(errno));
47117987Speter			if (how & SOFT)
47217987Speter				val = limit.rlim_cur;
47317987Speter			else if (how & HARD)
47417987Speter				val = limit.rlim_max;
47517987Speter
47618016Speter			if (l->units)
47718016Speter				snprintf(optbuf, sizeof(optbuf),
47818019Speter					"(%s, -%c) ", l->units, l->option);
47918016Speter			else
48018016Speter				snprintf(optbuf, sizeof(optbuf),
48118019Speter					"(-%c) ", l->option);
48218019Speter			out1fmt("%-18s %18s ", l->name, optbuf);
48317987Speter			if (val == RLIM_INFINITY)
484221975Sjilles				out1str("unlimited\n");
48517987Speter			else
48617987Speter			{
48717987Speter				val /= l->factor;
488104282Smux				out1fmt("%jd\n", (intmax_t)val);
48917987Speter			}
49011571Sjoerg		}
49117987Speter		return 0;
49211571Sjoerg	}
49317987Speter
49418016Speter	if (getrlimit(l->cmd, &limit) < 0)
495104208Stjr		error("can't get limit: %s", strerror(errno));
49617987Speter	if (set) {
49717987Speter		if (how & SOFT)
49817987Speter			limit.rlim_cur = val;
49917987Speter		if (how & HARD)
50017987Speter			limit.rlim_max = val;
50117987Speter		if (setrlimit(l->cmd, &limit) < 0)
502104208Stjr			error("bad limit: %s", strerror(errno));
50317987Speter	} else {
50417987Speter		if (how & SOFT)
50517987Speter			val = limit.rlim_cur;
50617987Speter		else if (how & HARD)
50717987Speter			val = limit.rlim_max;
50817987Speter
50917987Speter		if (val == RLIM_INFINITY)
510221975Sjilles			out1str("unlimited\n");
51117987Speter		else
51217987Speter		{
51317987Speter			val /= l->factor;
514104282Smux			out1fmt("%jd\n", (intmax_t)val);
51517987Speter		}
51617987Speter	}
51711571Sjoerg	return 0;
51811571Sjoerg}
519