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>
5017987Speter#include <ctype.h>
5118016Speter#include <errno.h>
52104282Smux#include <stdint.h>
5318018Speter#include <stdio.h>
5438536Scracauer#include <stdlib.h>
5529983Smsmith#include <termios.h>
5617987Speter
571556Srgrimes#include "shell.h"
581556Srgrimes#include "options.h"
591556Srgrimes#include "var.h"
601556Srgrimes#include "output.h"
611556Srgrimes#include "memalloc.h"
621556Srgrimes#include "error.h"
631556Srgrimes#include "mystring.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;
1061556Srgrimes
10750394Stg	rflag = 0;
1081556Srgrimes	prompt = NULL;
10929983Smsmith	tv.tv_sec = -1;
11029983Smsmith	tv.tv_usec = 0;
11150394Stg	while ((i = nextopt("erp:t:")) != '\0') {
11229983Smsmith		switch(i) {
11329983Smsmith		case 'p':
11459436Scracauer			prompt = shoptarg;
11529983Smsmith			break;
11629983Smsmith		case 'e':
11729983Smsmith			break;
11850394Stg		case 'r':
11950394Stg			rflag = 1;
12050394Stg			break;
12129983Smsmith		case 't':
12259436Scracauer			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
12359436Scracauer			if (tvptr == shoptarg)
12429983Smsmith				error("timeout value");
12529983Smsmith			switch(*tvptr) {
12629983Smsmith			case 0:
12729983Smsmith			case 's':
12829983Smsmith				break;
12929983Smsmith			case 'h':
13029983Smsmith				tv.tv_sec *= 60;
13129983Smsmith				/* FALLTHROUGH */
13229983Smsmith			case 'm':
13329983Smsmith				tv.tv_sec *= 60;
13429983Smsmith				break;
13529983Smsmith			default:
13629983Smsmith				error("timeout unit");
13729983Smsmith			}
13829983Smsmith			break;
13929983Smsmith		}
1401556Srgrimes	}
1411556Srgrimes	if (prompt && isatty(0)) {
1421556Srgrimes		out2str(prompt);
1431556Srgrimes		flushall();
1441556Srgrimes	}
1451556Srgrimes	if (*(ap = argptr) == NULL)
1461556Srgrimes		error("arg count");
1471556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
148190298Sstefanf		ifs = " \t\n";
14929983Smsmith
15029983Smsmith	if (tv.tv_sec >= 0) {
15129983Smsmith		/*
15229983Smsmith		 * Wait for something to become available.
15329983Smsmith		 */
15429983Smsmith		FD_ZERO(&ifds);
15529983Smsmith		FD_SET(0, &ifds);
15629983Smsmith		status = select(1, &ifds, NULL, NULL, &tv);
15729983Smsmith		/*
15829983Smsmith		 * If there's nothing ready, return an error.
15929983Smsmith		 */
16029983Smsmith		if (status <= 0)
16129983Smsmith			return(1);
16229983Smsmith	}
16329983Smsmith
1641556Srgrimes	status = 0;
165190295Sstefanf	startword = 2;
1661556Srgrimes	backslash = 0;
1671556Srgrimes	STARTSTACKSTR(p);
1681556Srgrimes	for (;;) {
16980381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1) {
1701556Srgrimes			status = 1;
1711556Srgrimes			break;
1721556Srgrimes		}
1731556Srgrimes		if (c == '\0')
1741556Srgrimes			continue;
175215783Sjilles		CHECKSTRSPACE(1, p);
1761556Srgrimes		if (backslash) {
1771556Srgrimes			backslash = 0;
178212339Sjilles			startword = 0;
1791556Srgrimes			if (c != '\n')
180215783Sjilles				USTPUTC(c, p);
1811556Srgrimes			continue;
1821556Srgrimes		}
18350394Stg		if (!rflag && c == '\\') {
1841556Srgrimes			backslash++;
1851556Srgrimes			continue;
1861556Srgrimes		}
1871556Srgrimes		if (c == '\n')
1881556Srgrimes			break;
189190295Sstefanf		if (strchr(ifs, c))
190190295Sstefanf			is_ifs = strchr(" \t\n", c) ? 1 : 2;
191190295Sstefanf		else
192190295Sstefanf			is_ifs = 0;
193190295Sstefanf
194190295Sstefanf		if (startword != 0) {
195190295Sstefanf			if (is_ifs == 1) {
196190295Sstefanf				/* Ignore leading IFS whitespace */
197190295Sstefanf				if (saveall)
198215783Sjilles					USTPUTC(c, p);
199190295Sstefanf				continue;
200190295Sstefanf			}
201190295Sstefanf			if (is_ifs == 2 && startword == 1) {
202190295Sstefanf				/* Only one non-whitespace IFS per word */
203190295Sstefanf				startword = 2;
204190295Sstefanf				if (saveall)
205215783Sjilles					USTPUTC(c, p);
206190295Sstefanf				continue;
207190295Sstefanf			}
208190295Sstefanf		}
209190295Sstefanf
210190295Sstefanf		if (is_ifs == 0) {
211190295Sstefanf			/* append this character to the current variable */
212190295Sstefanf			startword = 0;
213190295Sstefanf			if (saveall)
214190295Sstefanf				/* Not just a spare terminator */
215190295Sstefanf				saveall++;
216215783Sjilles			USTPUTC(c, p);
2171556Srgrimes			continue;
2181556Srgrimes		}
219190295Sstefanf
220190295Sstefanf		/* end of variable... */
221190295Sstefanf		startword = is_ifs;
222190295Sstefanf
223190295Sstefanf		if (ap[1] == NULL) {
224190295Sstefanf			/* Last variable needs all IFS chars */
225190295Sstefanf			saveall++;
226215783Sjilles			USTPUTC(c, p);
227190295Sstefanf			continue;
2281556Srgrimes		}
229190295Sstefanf
230190295Sstefanf		STACKSTRNUL(p);
231190295Sstefanf		setvar(*ap, stackblock(), 0);
232190295Sstefanf		ap++;
233190295Sstefanf		STARTSTACKSTR(p);
2341556Srgrimes	}
2351556Srgrimes	STACKSTRNUL(p);
236190295Sstefanf
237190295Sstefanf	/* Remove trailing IFS chars */
238190295Sstefanf	for (; stackblock() <= --p; *p = 0) {
239190295Sstefanf		if (!strchr(ifs, *p))
240190295Sstefanf			break;
241190295Sstefanf		if (strchr(" \t\n", *p))
242190295Sstefanf			/* Always remove whitespace */
243190295Sstefanf			continue;
244190295Sstefanf		if (saveall > 1)
245190295Sstefanf			/* Don't remove non-whitespace unless it was naked */
246190295Sstefanf			break;
247190295Sstefanf	}
2481556Srgrimes	setvar(*ap, stackblock(), 0);
249190295Sstefanf
250190295Sstefanf	/* Set any remaining args to "" */
2511556Srgrimes	while (*++ap != NULL)
2521556Srgrimes		setvar(*ap, nullstr, 0);
2531556Srgrimes	return status;
2541556Srgrimes}
2551556Srgrimes
2561556Srgrimes
2571556Srgrimes
25817987Speterint
259201053Sjillesumaskcmd(int argc __unused, char **argv __unused)
26017987Speter{
26117987Speter	char *ap;
2621556Srgrimes	int mask;
2631556Srgrimes	int i;
26417987Speter	int symbolic_mode = 0;
2651556Srgrimes
26617987Speter	while ((i = nextopt("S")) != '\0') {
26717987Speter		symbolic_mode = 1;
2681556Srgrimes	}
26911571Sjoerg
27017987Speter	INTOFF;
27117987Speter	mask = umask(0);
27217987Speter	umask(mask);
27317987Speter	INTON;
27411571Sjoerg
27517987Speter	if ((ap = *argptr) == NULL) {
27617987Speter		if (symbolic_mode) {
27717987Speter			char u[4], g[4], o[4];
27811571Sjoerg
27917987Speter			i = 0;
28017987Speter			if ((mask & S_IRUSR) == 0)
28117987Speter				u[i++] = 'r';
28217987Speter			if ((mask & S_IWUSR) == 0)
28317987Speter				u[i++] = 'w';
28417987Speter			if ((mask & S_IXUSR) == 0)
28517987Speter				u[i++] = 'x';
28617987Speter			u[i] = '\0';
28711571Sjoerg
28817987Speter			i = 0;
28917987Speter			if ((mask & S_IRGRP) == 0)
29017987Speter				g[i++] = 'r';
29117987Speter			if ((mask & S_IWGRP) == 0)
29217987Speter				g[i++] = 'w';
29317987Speter			if ((mask & S_IXGRP) == 0)
29417987Speter				g[i++] = 'x';
29517987Speter			g[i] = '\0';
29611571Sjoerg
29717987Speter			i = 0;
29817987Speter			if ((mask & S_IROTH) == 0)
29917987Speter				o[i++] = 'r';
30017987Speter			if ((mask & S_IWOTH) == 0)
30117987Speter				o[i++] = 'w';
30217987Speter			if ((mask & S_IXOTH) == 0)
30317987Speter				o[i++] = 'x';
30417987Speter			o[i] = '\0';
30511571Sjoerg
30617987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
30717987Speter		} else {
30817987Speter			out1fmt("%.4o\n", mask);
30917987Speter		}
31017987Speter	} else {
31117987Speter		if (isdigit(*ap)) {
31217987Speter			mask = 0;
31317987Speter			do {
31417987Speter				if (*ap >= '8' || *ap < '0')
315149918Sstefanf					error("Illegal number: %s", *argptr);
31617987Speter				mask = (mask << 3) + (*ap - '0');
31717987Speter			} while (*++ap != '\0');
31817987Speter			umask(mask);
31917987Speter		} else {
32020425Ssteve			void *set;
321151795Sstefanf			INTOFF;
32217987Speter			if ((set = setmode (ap)) == 0)
32341844Simp				error("Illegal number: %s", ap);
32411571Sjoerg
32517987Speter			mask = getmode (set, ~mask & 0777);
32617987Speter			umask(~mask & 0777);
32741844Simp			free(set);
328151795Sstefanf			INTON;
32917987Speter		}
33017987Speter	}
33111571Sjoerg	return 0;
33211571Sjoerg}
33311571Sjoerg
33417987Speter/*
33517987Speter * ulimit builtin
33617987Speter *
33717987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
33817987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
33917987Speter * ash by J.T. Conklin.
34017987Speter *
34117987Speter * Public domain.
34217987Speter */
34311571Sjoerg
34417987Speterstruct limits {
34517987Speter	const char *name;
34618016Speter	const char *units;
34717987Speter	int	cmd;
34817987Speter	int	factor;	/* multiply by to get rlim_{cur,max} values */
34917987Speter	char	option;
35017987Speter};
35111571Sjoerg
35217987Speterstatic const struct limits limits[] = {
35317987Speter#ifdef RLIMIT_CPU
35418016Speter	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
35517987Speter#endif
35617987Speter#ifdef RLIMIT_FSIZE
35718016Speter	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
35817987Speter#endif
35917987Speter#ifdef RLIMIT_DATA
36018016Speter	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
36117987Speter#endif
36217987Speter#ifdef RLIMIT_STACK
36318016Speter	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
36417987Speter#endif
36517987Speter#ifdef  RLIMIT_CORE
36618016Speter	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
36717987Speter#endif
36817987Speter#ifdef RLIMIT_RSS
36918016Speter	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
37017987Speter#endif
37117987Speter#ifdef RLIMIT_MEMLOCK
37218016Speter	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
37317987Speter#endif
37417987Speter#ifdef RLIMIT_NPROC
37518016Speter	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
37617987Speter#endif
37717987Speter#ifdef RLIMIT_NOFILE
37818016Speter	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
37917987Speter#endif
38017987Speter#ifdef RLIMIT_VMEM
38118016Speter	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
38217987Speter#endif
38317987Speter#ifdef RLIMIT_SWAP
38418016Speter	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
38517987Speter#endif
38652072Sgreen#ifdef RLIMIT_SBSIZE
38752072Sgreen	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
38852072Sgreen#endif
389181905Sed#ifdef RLIMIT_NPTS
390181905Sed	{ "pseudo-terminals",	(char *)0,	RLIMIT_NPTS,	   1, 'p' },
391181905Sed#endif
39218016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
39317987Speter};
39417987Speter
39517987Speterint
39690111Simpulimitcmd(int argc __unused, char **argv __unused)
39717987Speter{
39825222Ssteve	int	c;
399104282Smux	rlim_t val = 0;
40017987Speter	enum { SOFT = 0x1, HARD = 0x2 }
40117987Speter			how = SOFT | HARD;
40217987Speter	const struct limits	*l;
40317987Speter	int		set, all = 0;
40417987Speter	int		optc, what;
40517987Speter	struct rlimit	limit;
40611571Sjoerg
40717987Speter	what = 'f';
408194767Skib	while ((optc = nextopt("HSatfdsmcnuvlbpw")) != '\0')
40917987Speter		switch (optc) {
41011571Sjoerg		case 'H':
41117987Speter			how = HARD;
41211571Sjoerg			break;
41311571Sjoerg		case 'S':
41417987Speter			how = SOFT;
41511571Sjoerg			break;
41611571Sjoerg		case 'a':
41717987Speter			all = 1;
41811571Sjoerg			break;
41917987Speter		default:
42017987Speter			what = optc;
42111571Sjoerg		}
42211571Sjoerg
42317987Speter	for (l = limits; l->name && l->option != what; l++)
42417987Speter		;
42517987Speter	if (!l->name)
426104208Stjr		error("internal error (%c)", what);
42717987Speter
42817987Speter	set = *argptr ? 1 : 0;
42917987Speter	if (set) {
43017987Speter		char *p = *argptr;
43117987Speter
43217987Speter		if (all || argptr[1])
433104208Stjr			error("too many arguments");
43417987Speter		if (strcmp(p, "unlimited") == 0)
43511571Sjoerg			val = RLIM_INFINITY;
43611571Sjoerg		else {
437104282Smux			val = 0;
43817987Speter
43917987Speter			while ((c = *p++) >= '0' && c <= '9')
44017987Speter			{
44117987Speter				val = (val * 10) + (long)(c - '0');
442104282Smux				if (val < 0)
44317987Speter					break;
44417987Speter			}
44517987Speter			if (c)
446104208Stjr				error("bad number");
44717987Speter			val *= l->factor;
44811571Sjoerg		}
44917987Speter	}
45017987Speter	if (all) {
451155301Sschweikh		for (l = limits; l->name; l++) {
45218016Speter			char optbuf[40];
45318016Speter			if (getrlimit(l->cmd, &limit) < 0)
454104208Stjr				error("can't get limit: %s", strerror(errno));
45517987Speter			if (how & SOFT)
45617987Speter				val = limit.rlim_cur;
45717987Speter			else if (how & HARD)
45817987Speter				val = limit.rlim_max;
45917987Speter
46018016Speter			if (l->units)
46118016Speter				snprintf(optbuf, sizeof(optbuf),
46218019Speter					"(%s, -%c) ", l->units, l->option);
46318016Speter			else
46418016Speter				snprintf(optbuf, sizeof(optbuf),
46518019Speter					"(-%c) ", l->option);
46618019Speter			out1fmt("%-18s %18s ", l->name, optbuf);
46717987Speter			if (val == RLIM_INFINITY)
468221975Sjilles				out1str("unlimited\n");
46917987Speter			else
47017987Speter			{
47117987Speter				val /= l->factor;
472104282Smux				out1fmt("%jd\n", (intmax_t)val);
47317987Speter			}
47411571Sjoerg		}
47517987Speter		return 0;
47611571Sjoerg	}
47717987Speter
47818016Speter	if (getrlimit(l->cmd, &limit) < 0)
479104208Stjr		error("can't get limit: %s", strerror(errno));
48017987Speter	if (set) {
48117987Speter		if (how & SOFT)
48217987Speter			limit.rlim_cur = val;
48317987Speter		if (how & HARD)
48417987Speter			limit.rlim_max = val;
48517987Speter		if (setrlimit(l->cmd, &limit) < 0)
486104208Stjr			error("bad limit: %s", strerror(errno));
48717987Speter	} else {
48817987Speter		if (how & SOFT)
48917987Speter			val = limit.rlim_cur;
49017987Speter		else if (how & HARD)
49117987Speter			val = limit.rlim_max;
49217987Speter
49317987Speter		if (val == RLIM_INFINITY)
494221975Sjilles			out1str("unlimited\n");
49517987Speter		else
49617987Speter		{
49717987Speter			val /= l->factor;
498104282Smux			out1fmt("%jd\n", (intmax_t)val);
49917987Speter		}
50017987Speter	}
50111571Sjoerg	return 0;
50211571Sjoerg}
503