miscbltin.c revision 29983
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 * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
353044Sdg *
3629983Smsmith *	$Id: miscbltin.c,v 1.13 1997/05/19 00:18:43 steve Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4020425Sstevestatic char const sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes/*
441556Srgrimes * Miscelaneous builtins.
451556Srgrimes */
461556Srgrimes
4717987Speter#include <sys/types.h>
4817987Speter#include <sys/stat.h>
4917987Speter#include <sys/time.h>
5017987Speter#include <sys/resource.h>
5117987Speter#include <unistd.h>
5217987Speter#include <ctype.h>
5318016Speter#include <errno.h>
5418018Speter#include <stdio.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
671556Srgrimesextern char **argptr;		/* argument list for builtin command */
681556Srgrimes
691556Srgrimes
701556Srgrimes/*
711556Srgrimes * The read builtin.  The -e option causes backslashes to escape the
721556Srgrimes * following character.
731556Srgrimes *
741556Srgrimes * This uses unbuffered input, which may be avoidable in some cases.
751556Srgrimes */
761556Srgrimes
7717987Speterint
7817987Speterreadcmd(argc, argv)
7925905Ssteve	int argc __unused;
8025905Ssteve	char **argv __unused;
8117987Speter{
821556Srgrimes	char **ap;
831556Srgrimes	int backslash;
841556Srgrimes	char c;
851556Srgrimes	int eflag;
861556Srgrimes	char *prompt;
871556Srgrimes	char *ifs;
881556Srgrimes	char *p;
891556Srgrimes	int startword;
901556Srgrimes	int status;
911556Srgrimes	int i;
9229983Smsmith	struct timeval tv;
9329983Smsmith	char *tvptr;
9429983Smsmith	fd_set ifds;
9529983Smsmith	struct termios told, tnew;
9629983Smsmith	int tsaved;
971556Srgrimes
981556Srgrimes	eflag = 0;
991556Srgrimes	prompt = NULL;
10029983Smsmith	tv.tv_sec = -1;
10129983Smsmith	tv.tv_usec = 0;
10229983Smsmith	while ((i = nextopt("ep:t:")) != '\0') {
10329983Smsmith		switch(i) {
10429983Smsmith		case 'p':
1051556Srgrimes			prompt = optarg;
10629983Smsmith			break;
10729983Smsmith		case 'e':
1081556Srgrimes			eflag = 1;
10929983Smsmith			break;
11029983Smsmith		case 't':
11129983Smsmith			tv.tv_sec = strtol(optarg, &tvptr, 0);
11229983Smsmith			if (tvptr == optarg)
11329983Smsmith				error("timeout value");
11429983Smsmith			switch(*tvptr) {
11529983Smsmith			case 0:
11629983Smsmith			case 's':
11729983Smsmith				break;
11829983Smsmith			case 'h':
11929983Smsmith				tv.tv_sec *= 60;
12029983Smsmith				/* FALLTHROUGH */
12129983Smsmith			case 'm':
12229983Smsmith				tv.tv_sec *= 60;
12329983Smsmith				break;
12429983Smsmith			default:
12529983Smsmith				error("timeout unit");
12629983Smsmith			}
12729983Smsmith			break;
12829983Smsmith		}
1291556Srgrimes	}
1301556Srgrimes	if (prompt && isatty(0)) {
1311556Srgrimes		out2str(prompt);
1321556Srgrimes		flushall();
1331556Srgrimes	}
1341556Srgrimes	if (*(ap = argptr) == NULL)
1351556Srgrimes		error("arg count");
1361556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
1371556Srgrimes		ifs = nullstr;
13829983Smsmith
13929983Smsmith	if (tv.tv_sec >= 0) {
14029983Smsmith		/*
14129983Smsmith		 * See if we can disable input processing; this will
14229983Smsmith		 * not give the desired result if we are in a pipeline
14329983Smsmith		 * and someone upstream is still in line-by-line mode.
14429983Smsmith		 */
14529983Smsmith		tsaved = 0;
14629983Smsmith		if (tcgetattr(0, &told) == 0) {
14729983Smsmith			memcpy(&tnew, &told, sizeof(told));
14829983Smsmith			cfmakeraw(&tnew);
14929983Smsmith			tcsetattr(0, TCSANOW, &tnew);
15029983Smsmith			tsaved = 1;
15129983Smsmith		}
15229983Smsmith		/*
15329983Smsmith		 * Wait for something to become available.
15429983Smsmith		 */
15529983Smsmith		FD_ZERO(&ifds);
15629983Smsmith		FD_SET(0, &ifds);
15729983Smsmith		status = select(1, &ifds, NULL, NULL, &tv);
15829983Smsmith		if (tsaved)
15929983Smsmith			tcsetattr(0, TCSANOW, &told);
16029983Smsmith		/*
16129983Smsmith		 * If there's nothing ready, return an error.
16229983Smsmith		 */
16329983Smsmith		if (status <= 0)
16429983Smsmith			return(1);
16529983Smsmith	}
16629983Smsmith
1671556Srgrimes	status = 0;
1681556Srgrimes	startword = 1;
1691556Srgrimes	backslash = 0;
1701556Srgrimes	STARTSTACKSTR(p);
1711556Srgrimes	for (;;) {
1721556Srgrimes		if (read(0, &c, 1) != 1) {
1731556Srgrimes			status = 1;
1741556Srgrimes			break;
1751556Srgrimes		}
1761556Srgrimes		if (c == '\0')
1771556Srgrimes			continue;
1781556Srgrimes		if (backslash) {
1791556Srgrimes			backslash = 0;
1801556Srgrimes			if (c != '\n')
1811556Srgrimes				STPUTC(c, p);
1821556Srgrimes			continue;
1831556Srgrimes		}
1841556Srgrimes		if (eflag && c == '\\') {
1851556Srgrimes			backslash++;
1861556Srgrimes			continue;
1871556Srgrimes		}
1881556Srgrimes		if (c == '\n')
1891556Srgrimes			break;
1901556Srgrimes		if (startword && *ifs == ' ' && strchr(ifs, c)) {
1911556Srgrimes			continue;
1921556Srgrimes		}
1931556Srgrimes		startword = 0;
1941556Srgrimes		if (backslash && c == '\\') {
1951556Srgrimes			if (read(0, &c, 1) != 1) {
1961556Srgrimes				status = 1;
1971556Srgrimes				break;
1981556Srgrimes			}
1991556Srgrimes			STPUTC(c, p);
2001556Srgrimes		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
2011556Srgrimes			STACKSTRNUL(p);
2021556Srgrimes			setvar(*ap, stackblock(), 0);
2031556Srgrimes			ap++;
2041556Srgrimes			startword = 1;
2051556Srgrimes			STARTSTACKSTR(p);
2061556Srgrimes		} else {
2071556Srgrimes			STPUTC(c, p);
2081556Srgrimes		}
2091556Srgrimes	}
2101556Srgrimes	STACKSTRNUL(p);
2111556Srgrimes	setvar(*ap, stackblock(), 0);
2121556Srgrimes	while (*++ap != NULL)
2131556Srgrimes		setvar(*ap, nullstr, 0);
2141556Srgrimes	return status;
2151556Srgrimes}
2161556Srgrimes
2171556Srgrimes
2181556Srgrimes
21917987Speterint
22017987Speterumaskcmd(argc, argv)
22125905Ssteve	int argc __unused;
22220425Ssteve	char **argv;
22317987Speter{
22417987Speter	char *ap;
2251556Srgrimes	int mask;
2261556Srgrimes	int i;
22717987Speter	int symbolic_mode = 0;
2281556Srgrimes
22917987Speter	while ((i = nextopt("S")) != '\0') {
23017987Speter		symbolic_mode = 1;
2311556Srgrimes	}
23211571Sjoerg
23317987Speter	INTOFF;
23417987Speter	mask = umask(0);
23517987Speter	umask(mask);
23617987Speter	INTON;
23711571Sjoerg
23817987Speter	if ((ap = *argptr) == NULL) {
23917987Speter		if (symbolic_mode) {
24017987Speter			char u[4], g[4], o[4];
24111571Sjoerg
24217987Speter			i = 0;
24317987Speter			if ((mask & S_IRUSR) == 0)
24417987Speter				u[i++] = 'r';
24517987Speter			if ((mask & S_IWUSR) == 0)
24617987Speter				u[i++] = 'w';
24717987Speter			if ((mask & S_IXUSR) == 0)
24817987Speter				u[i++] = 'x';
24917987Speter			u[i] = '\0';
25011571Sjoerg
25117987Speter			i = 0;
25217987Speter			if ((mask & S_IRGRP) == 0)
25317987Speter				g[i++] = 'r';
25417987Speter			if ((mask & S_IWGRP) == 0)
25517987Speter				g[i++] = 'w';
25617987Speter			if ((mask & S_IXGRP) == 0)
25717987Speter				g[i++] = 'x';
25817987Speter			g[i] = '\0';
25911571Sjoerg
26017987Speter			i = 0;
26117987Speter			if ((mask & S_IROTH) == 0)
26217987Speter				o[i++] = 'r';
26317987Speter			if ((mask & S_IWOTH) == 0)
26417987Speter				o[i++] = 'w';
26517987Speter			if ((mask & S_IXOTH) == 0)
26617987Speter				o[i++] = 'x';
26717987Speter			o[i] = '\0';
26811571Sjoerg
26917987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
27017987Speter		} else {
27117987Speter			out1fmt("%.4o\n", mask);
27217987Speter		}
27317987Speter	} else {
27417987Speter		if (isdigit(*ap)) {
27517987Speter			mask = 0;
27617987Speter			do {
27717987Speter				if (*ap >= '8' || *ap < '0')
27817987Speter					error("Illegal number: %s", argv[1]);
27917987Speter				mask = (mask << 3) + (*ap - '0');
28017987Speter			} while (*++ap != '\0');
28117987Speter			umask(mask);
28217987Speter		} else {
28320425Ssteve			void *set;
28417987Speter			if ((set = setmode (ap)) == 0)
28517987Speter					error("Illegal number: %s", ap);
28611571Sjoerg
28717987Speter			mask = getmode (set, ~mask & 0777);
28817987Speter			umask(~mask & 0777);
28917987Speter		}
29017987Speter	}
29111571Sjoerg	return 0;
29211571Sjoerg}
29311571Sjoerg
29417987Speter/*
29517987Speter * ulimit builtin
29617987Speter *
29717987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
29817987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
29917987Speter * ash by J.T. Conklin.
30017987Speter *
30117987Speter * Public domain.
30217987Speter */
30311571Sjoerg
30417987Speterstruct limits {
30517987Speter	const char *name;
30618016Speter	const char *units;
30717987Speter	int	cmd;
30817987Speter	int	factor;	/* multiply by to get rlim_{cur,max} values */
30917987Speter	char	option;
31017987Speter};
31111571Sjoerg
31217987Speterstatic const struct limits limits[] = {
31317987Speter#ifdef RLIMIT_CPU
31418016Speter	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
31517987Speter#endif
31617987Speter#ifdef RLIMIT_FSIZE
31718016Speter	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
31817987Speter#endif
31917987Speter#ifdef RLIMIT_DATA
32018016Speter	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
32117987Speter#endif
32217987Speter#ifdef RLIMIT_STACK
32318016Speter	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
32417987Speter#endif
32517987Speter#ifdef  RLIMIT_CORE
32618016Speter	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
32717987Speter#endif
32817987Speter#ifdef RLIMIT_RSS
32918016Speter	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
33017987Speter#endif
33117987Speter#ifdef RLIMIT_MEMLOCK
33218016Speter	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
33317987Speter#endif
33417987Speter#ifdef RLIMIT_NPROC
33518016Speter	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
33617987Speter#endif
33717987Speter#ifdef RLIMIT_NOFILE
33818016Speter	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
33917987Speter#endif
34017987Speter#ifdef RLIMIT_VMEM
34118016Speter	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
34217987Speter#endif
34317987Speter#ifdef RLIMIT_SWAP
34418016Speter	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
34517987Speter#endif
34618016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
34717987Speter};
34817987Speter
34917987Speterint
35017987Speterulimitcmd(argc, argv)
35125905Ssteve	int argc __unused;
35225905Ssteve	char **argv __unused;
35317987Speter{
35425222Ssteve	int	c;
35518018Speter	quad_t val = 0;
35617987Speter	enum { SOFT = 0x1, HARD = 0x2 }
35717987Speter			how = SOFT | HARD;
35817987Speter	const struct limits	*l;
35917987Speter	int		set, all = 0;
36017987Speter	int		optc, what;
36117987Speter	struct rlimit	limit;
36211571Sjoerg
36317987Speter	what = 'f';
36418016Speter	while ((optc = nextopt("HSatfdsmcnul")) != '\0')
36517987Speter		switch (optc) {
36611571Sjoerg		case 'H':
36717987Speter			how = HARD;
36811571Sjoerg			break;
36911571Sjoerg		case 'S':
37017987Speter			how = SOFT;
37111571Sjoerg			break;
37211571Sjoerg		case 'a':
37317987Speter			all = 1;
37411571Sjoerg			break;
37517987Speter		default:
37617987Speter			what = optc;
37711571Sjoerg		}
37811571Sjoerg
37917987Speter	for (l = limits; l->name && l->option != what; l++)
38017987Speter		;
38117987Speter	if (!l->name)
38220425Ssteve		error("ulimit: internal error (%c)", what);
38317987Speter
38417987Speter	set = *argptr ? 1 : 0;
38517987Speter	if (set) {
38617987Speter		char *p = *argptr;
38717987Speter
38817987Speter		if (all || argptr[1])
38920425Ssteve			error("ulimit: too many arguments");
39017987Speter		if (strcmp(p, "unlimited") == 0)
39111571Sjoerg			val = RLIM_INFINITY;
39211571Sjoerg		else {
39317987Speter			val = (quad_t) 0;
39417987Speter
39517987Speter			while ((c = *p++) >= '0' && c <= '9')
39617987Speter			{
39717987Speter				val = (val * 10) + (long)(c - '0');
39817987Speter				if (val < (quad_t) 0)
39917987Speter					break;
40017987Speter			}
40117987Speter			if (c)
40220425Ssteve				error("ulimit: bad number");
40317987Speter			val *= l->factor;
40411571Sjoerg		}
40517987Speter	}
40617987Speter	if (all) {
40718016Speter		for (l = limits; l->name; l++) {
40818016Speter			char optbuf[40];
40918016Speter			if (getrlimit(l->cmd, &limit) < 0)
41020425Ssteve				error("ulimit: can't get limit: %s", strerror(errno));
41117987Speter			if (how & SOFT)
41217987Speter				val = limit.rlim_cur;
41317987Speter			else if (how & HARD)
41417987Speter				val = limit.rlim_max;
41517987Speter
41618016Speter			if (l->units)
41718016Speter				snprintf(optbuf, sizeof(optbuf),
41818019Speter					"(%s, -%c) ", l->units, l->option);
41918016Speter			else
42018016Speter				snprintf(optbuf, sizeof(optbuf),
42118019Speter					"(-%c) ", l->option);
42218019Speter			out1fmt("%-18s %18s ", l->name, optbuf);
42317987Speter			if (val == RLIM_INFINITY)
42417987Speter				out1fmt("unlimited\n");
42517987Speter			else
42617987Speter			{
42717987Speter				val /= l->factor;
42818018Speter				out1fmt("%qd\n", (quad_t) val);
42917987Speter			}
43011571Sjoerg		}
43117987Speter		return 0;
43211571Sjoerg	}
43317987Speter
43418016Speter	if (getrlimit(l->cmd, &limit) < 0)
43520425Ssteve		error("ulimit: can't get limit: %s", strerror(errno));
43617987Speter	if (set) {
43717987Speter		if (how & SOFT)
43817987Speter			limit.rlim_cur = val;
43917987Speter		if (how & HARD)
44017987Speter			limit.rlim_max = val;
44117987Speter		if (setrlimit(l->cmd, &limit) < 0)
44220425Ssteve			error("ulimit: bad limit: %s", strerror(errno));
44317987Speter	} else {
44417987Speter		if (how & SOFT)
44517987Speter			val = limit.rlim_cur;
44617987Speter		else if (how & HARD)
44717987Speter			val = limit.rlim_max;
44817987Speter
44917987Speter		if (val == RLIM_INFINITY)
45017987Speter			out1fmt("unlimited\n");
45117987Speter		else
45217987Speter		{
45317987Speter			val /= l->factor;
45418018Speter			out1fmt("%qd\n", (quad_t) val);
45517987Speter		}
45617987Speter	}
45711571Sjoerg	return 0;
45811571Sjoerg}
459