miscbltin.c revision 46684
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.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
3836150Scharnier#if 0
3936150Scharnierstatic char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
4036150Scharnier#endif
4136150Scharnierstatic const char rcsid[] =
4246684Skris	"$Id: miscbltin.c,v 1.18 1998/12/16 04:45:35 imp Exp $";
431556Srgrimes#endif /* not lint */
441556Srgrimes
451556Srgrimes/*
4646684Skris * Miscellaneous builtins.
471556Srgrimes */
481556Srgrimes
4917987Speter#include <sys/types.h>
5017987Speter#include <sys/stat.h>
5117987Speter#include <sys/time.h>
5217987Speter#include <sys/resource.h>
5317987Speter#include <unistd.h>
5417987Speter#include <ctype.h>
5518016Speter#include <errno.h>
5618018Speter#include <stdio.h>
5738536Scracauer#include <stdlib.h>
5829983Smsmith#include <termios.h>
5917987Speter
601556Srgrimes#include "shell.h"
611556Srgrimes#include "options.h"
621556Srgrimes#include "var.h"
631556Srgrimes#include "output.h"
641556Srgrimes#include "memalloc.h"
651556Srgrimes#include "error.h"
661556Srgrimes#include "mystring.h"
671556Srgrimes
681556Srgrimes#undef eflag
691556Srgrimes
701556Srgrimesextern char **argptr;		/* argument list for builtin command */
711556Srgrimes
721556Srgrimes
731556Srgrimes/*
741556Srgrimes * The read builtin.  The -e option causes backslashes to escape the
751556Srgrimes * following character.
761556Srgrimes *
771556Srgrimes * This uses unbuffered input, which may be avoidable in some cases.
781556Srgrimes */
791556Srgrimes
8017987Speterint
8117987Speterreadcmd(argc, argv)
8225905Ssteve	int argc __unused;
8325905Ssteve	char **argv __unused;
8417987Speter{
851556Srgrimes	char **ap;
861556Srgrimes	int backslash;
871556Srgrimes	char c;
881556Srgrimes	int eflag;
891556Srgrimes	char *prompt;
901556Srgrimes	char *ifs;
911556Srgrimes	char *p;
921556Srgrimes	int startword;
931556Srgrimes	int status;
941556Srgrimes	int i;
9529983Smsmith	struct timeval tv;
9629983Smsmith	char *tvptr;
9729983Smsmith	fd_set ifds;
9829983Smsmith	struct termios told, tnew;
9929983Smsmith	int tsaved;
1001556Srgrimes
1011556Srgrimes	eflag = 0;
1021556Srgrimes	prompt = NULL;
10329983Smsmith	tv.tv_sec = -1;
10429983Smsmith	tv.tv_usec = 0;
10529983Smsmith	while ((i = nextopt("ep:t:")) != '\0') {
10629983Smsmith		switch(i) {
10729983Smsmith		case 'p':
1081556Srgrimes			prompt = optarg;
10929983Smsmith			break;
11029983Smsmith		case 'e':
1111556Srgrimes			eflag = 1;
11229983Smsmith			break;
11329983Smsmith		case 't':
11429983Smsmith			tv.tv_sec = strtol(optarg, &tvptr, 0);
11529983Smsmith			if (tvptr == optarg)
11629983Smsmith				error("timeout value");
11729983Smsmith			switch(*tvptr) {
11829983Smsmith			case 0:
11929983Smsmith			case 's':
12029983Smsmith				break;
12129983Smsmith			case 'h':
12229983Smsmith				tv.tv_sec *= 60;
12329983Smsmith				/* FALLTHROUGH */
12429983Smsmith			case 'm':
12529983Smsmith				tv.tv_sec *= 60;
12629983Smsmith				break;
12729983Smsmith			default:
12829983Smsmith				error("timeout unit");
12929983Smsmith			}
13029983Smsmith			break;
13129983Smsmith		}
1321556Srgrimes	}
1331556Srgrimes	if (prompt && isatty(0)) {
1341556Srgrimes		out2str(prompt);
1351556Srgrimes		flushall();
1361556Srgrimes	}
1371556Srgrimes	if (*(ap = argptr) == NULL)
1381556Srgrimes		error("arg count");
1391556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
1401556Srgrimes		ifs = nullstr;
14129983Smsmith
14229983Smsmith	if (tv.tv_sec >= 0) {
14329983Smsmith		/*
14429983Smsmith		 * See if we can disable input processing; this will
14529983Smsmith		 * not give the desired result if we are in a pipeline
14629983Smsmith		 * and someone upstream is still in line-by-line mode.
14729983Smsmith		 */
14829983Smsmith		tsaved = 0;
14929983Smsmith		if (tcgetattr(0, &told) == 0) {
15029983Smsmith			memcpy(&tnew, &told, sizeof(told));
15129983Smsmith			cfmakeraw(&tnew);
15229983Smsmith			tcsetattr(0, TCSANOW, &tnew);
15329983Smsmith			tsaved = 1;
15429983Smsmith		}
15529983Smsmith		/*
15629983Smsmith		 * Wait for something to become available.
15729983Smsmith		 */
15829983Smsmith		FD_ZERO(&ifds);
15929983Smsmith		FD_SET(0, &ifds);
16029983Smsmith		status = select(1, &ifds, NULL, NULL, &tv);
16129983Smsmith		if (tsaved)
16229983Smsmith			tcsetattr(0, TCSANOW, &told);
16329983Smsmith		/*
16429983Smsmith		 * If there's nothing ready, return an error.
16529983Smsmith		 */
16629983Smsmith		if (status <= 0)
16729983Smsmith			return(1);
16829983Smsmith	}
16929983Smsmith
1701556Srgrimes	status = 0;
1711556Srgrimes	startword = 1;
1721556Srgrimes	backslash = 0;
1731556Srgrimes	STARTSTACKSTR(p);
1741556Srgrimes	for (;;) {
1751556Srgrimes		if (read(0, &c, 1) != 1) {
1761556Srgrimes			status = 1;
1771556Srgrimes			break;
1781556Srgrimes		}
1791556Srgrimes		if (c == '\0')
1801556Srgrimes			continue;
1811556Srgrimes		if (backslash) {
1821556Srgrimes			backslash = 0;
1831556Srgrimes			if (c != '\n')
1841556Srgrimes				STPUTC(c, p);
1851556Srgrimes			continue;
1861556Srgrimes		}
1871556Srgrimes		if (eflag && c == '\\') {
1881556Srgrimes			backslash++;
1891556Srgrimes			continue;
1901556Srgrimes		}
1911556Srgrimes		if (c == '\n')
1921556Srgrimes			break;
1931556Srgrimes		if (startword && *ifs == ' ' && strchr(ifs, c)) {
1941556Srgrimes			continue;
1951556Srgrimes		}
1961556Srgrimes		startword = 0;
1971556Srgrimes		if (backslash && c == '\\') {
1981556Srgrimes			if (read(0, &c, 1) != 1) {
1991556Srgrimes				status = 1;
2001556Srgrimes				break;
2011556Srgrimes			}
2021556Srgrimes			STPUTC(c, p);
2031556Srgrimes		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
2041556Srgrimes			STACKSTRNUL(p);
2051556Srgrimes			setvar(*ap, stackblock(), 0);
2061556Srgrimes			ap++;
2071556Srgrimes			startword = 1;
2081556Srgrimes			STARTSTACKSTR(p);
2091556Srgrimes		} else {
2101556Srgrimes			STPUTC(c, p);
2111556Srgrimes		}
2121556Srgrimes	}
2131556Srgrimes	STACKSTRNUL(p);
2141556Srgrimes	setvar(*ap, stackblock(), 0);
2151556Srgrimes	while (*++ap != NULL)
2161556Srgrimes		setvar(*ap, nullstr, 0);
2171556Srgrimes	return status;
2181556Srgrimes}
2191556Srgrimes
2201556Srgrimes
2211556Srgrimes
22217987Speterint
22317987Speterumaskcmd(argc, argv)
22425905Ssteve	int argc __unused;
22520425Ssteve	char **argv;
22617987Speter{
22717987Speter	char *ap;
2281556Srgrimes	int mask;
2291556Srgrimes	int i;
23017987Speter	int symbolic_mode = 0;
2311556Srgrimes
23217987Speter	while ((i = nextopt("S")) != '\0') {
23317987Speter		symbolic_mode = 1;
2341556Srgrimes	}
23511571Sjoerg
23617987Speter	INTOFF;
23717987Speter	mask = umask(0);
23817987Speter	umask(mask);
23917987Speter	INTON;
24011571Sjoerg
24117987Speter	if ((ap = *argptr) == NULL) {
24217987Speter		if (symbolic_mode) {
24317987Speter			char u[4], g[4], o[4];
24411571Sjoerg
24517987Speter			i = 0;
24617987Speter			if ((mask & S_IRUSR) == 0)
24717987Speter				u[i++] = 'r';
24817987Speter			if ((mask & S_IWUSR) == 0)
24917987Speter				u[i++] = 'w';
25017987Speter			if ((mask & S_IXUSR) == 0)
25117987Speter				u[i++] = 'x';
25217987Speter			u[i] = '\0';
25311571Sjoerg
25417987Speter			i = 0;
25517987Speter			if ((mask & S_IRGRP) == 0)
25617987Speter				g[i++] = 'r';
25717987Speter			if ((mask & S_IWGRP) == 0)
25817987Speter				g[i++] = 'w';
25917987Speter			if ((mask & S_IXGRP) == 0)
26017987Speter				g[i++] = 'x';
26117987Speter			g[i] = '\0';
26211571Sjoerg
26317987Speter			i = 0;
26417987Speter			if ((mask & S_IROTH) == 0)
26517987Speter				o[i++] = 'r';
26617987Speter			if ((mask & S_IWOTH) == 0)
26717987Speter				o[i++] = 'w';
26817987Speter			if ((mask & S_IXOTH) == 0)
26917987Speter				o[i++] = 'x';
27017987Speter			o[i] = '\0';
27111571Sjoerg
27217987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
27317987Speter		} else {
27417987Speter			out1fmt("%.4o\n", mask);
27517987Speter		}
27617987Speter	} else {
27717987Speter		if (isdigit(*ap)) {
27817987Speter			mask = 0;
27917987Speter			do {
28017987Speter				if (*ap >= '8' || *ap < '0')
28117987Speter					error("Illegal number: %s", argv[1]);
28217987Speter				mask = (mask << 3) + (*ap - '0');
28317987Speter			} while (*++ap != '\0');
28417987Speter			umask(mask);
28517987Speter		} else {
28620425Ssteve			void *set;
28717987Speter			if ((set = setmode (ap)) == 0)
28841844Simp				error("Illegal number: %s", ap);
28911571Sjoerg
29017987Speter			mask = getmode (set, ~mask & 0777);
29117987Speter			umask(~mask & 0777);
29241844Simp			free(set);
29317987Speter		}
29417987Speter	}
29511571Sjoerg	return 0;
29611571Sjoerg}
29711571Sjoerg
29817987Speter/*
29917987Speter * ulimit builtin
30017987Speter *
30117987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
30217987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
30317987Speter * ash by J.T. Conklin.
30417987Speter *
30517987Speter * Public domain.
30617987Speter */
30711571Sjoerg
30817987Speterstruct limits {
30917987Speter	const char *name;
31018016Speter	const char *units;
31117987Speter	int	cmd;
31217987Speter	int	factor;	/* multiply by to get rlim_{cur,max} values */
31317987Speter	char	option;
31417987Speter};
31511571Sjoerg
31617987Speterstatic const struct limits limits[] = {
31717987Speter#ifdef RLIMIT_CPU
31818016Speter	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
31917987Speter#endif
32017987Speter#ifdef RLIMIT_FSIZE
32118016Speter	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
32217987Speter#endif
32317987Speter#ifdef RLIMIT_DATA
32418016Speter	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
32517987Speter#endif
32617987Speter#ifdef RLIMIT_STACK
32718016Speter	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
32817987Speter#endif
32917987Speter#ifdef  RLIMIT_CORE
33018016Speter	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
33117987Speter#endif
33217987Speter#ifdef RLIMIT_RSS
33318016Speter	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
33417987Speter#endif
33517987Speter#ifdef RLIMIT_MEMLOCK
33618016Speter	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
33717987Speter#endif
33817987Speter#ifdef RLIMIT_NPROC
33918016Speter	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
34017987Speter#endif
34117987Speter#ifdef RLIMIT_NOFILE
34218016Speter	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
34317987Speter#endif
34417987Speter#ifdef RLIMIT_VMEM
34518016Speter	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
34617987Speter#endif
34717987Speter#ifdef RLIMIT_SWAP
34818016Speter	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
34917987Speter#endif
35018016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
35117987Speter};
35217987Speter
35317987Speterint
35417987Speterulimitcmd(argc, argv)
35525905Ssteve	int argc __unused;
35625905Ssteve	char **argv __unused;
35717987Speter{
35825222Ssteve	int	c;
35918018Speter	quad_t val = 0;
36017987Speter	enum { SOFT = 0x1, HARD = 0x2 }
36117987Speter			how = SOFT | HARD;
36217987Speter	const struct limits	*l;
36317987Speter	int		set, all = 0;
36417987Speter	int		optc, what;
36517987Speter	struct rlimit	limit;
36611571Sjoerg
36717987Speter	what = 'f';
36818016Speter	while ((optc = nextopt("HSatfdsmcnul")) != '\0')
36917987Speter		switch (optc) {
37011571Sjoerg		case 'H':
37117987Speter			how = HARD;
37211571Sjoerg			break;
37311571Sjoerg		case 'S':
37417987Speter			how = SOFT;
37511571Sjoerg			break;
37611571Sjoerg		case 'a':
37717987Speter			all = 1;
37811571Sjoerg			break;
37917987Speter		default:
38017987Speter			what = optc;
38111571Sjoerg		}
38211571Sjoerg
38317987Speter	for (l = limits; l->name && l->option != what; l++)
38417987Speter		;
38517987Speter	if (!l->name)
38620425Ssteve		error("ulimit: internal error (%c)", what);
38717987Speter
38817987Speter	set = *argptr ? 1 : 0;
38917987Speter	if (set) {
39017987Speter		char *p = *argptr;
39117987Speter
39217987Speter		if (all || argptr[1])
39320425Ssteve			error("ulimit: too many arguments");
39417987Speter		if (strcmp(p, "unlimited") == 0)
39511571Sjoerg			val = RLIM_INFINITY;
39611571Sjoerg		else {
39717987Speter			val = (quad_t) 0;
39817987Speter
39917987Speter			while ((c = *p++) >= '0' && c <= '9')
40017987Speter			{
40117987Speter				val = (val * 10) + (long)(c - '0');
40217987Speter				if (val < (quad_t) 0)
40317987Speter					break;
40417987Speter			}
40517987Speter			if (c)
40620425Ssteve				error("ulimit: bad number");
40717987Speter			val *= l->factor;
40811571Sjoerg		}
40917987Speter	}
41017987Speter	if (all) {
41118016Speter		for (l = limits; l->name; l++) {
41218016Speter			char optbuf[40];
41318016Speter			if (getrlimit(l->cmd, &limit) < 0)
41420425Ssteve				error("ulimit: can't get limit: %s", strerror(errno));
41517987Speter			if (how & SOFT)
41617987Speter				val = limit.rlim_cur;
41717987Speter			else if (how & HARD)
41817987Speter				val = limit.rlim_max;
41917987Speter
42018016Speter			if (l->units)
42118016Speter				snprintf(optbuf, sizeof(optbuf),
42218019Speter					"(%s, -%c) ", l->units, l->option);
42318016Speter			else
42418016Speter				snprintf(optbuf, sizeof(optbuf),
42518019Speter					"(-%c) ", l->option);
42618019Speter			out1fmt("%-18s %18s ", l->name, optbuf);
42717987Speter			if (val == RLIM_INFINITY)
42817987Speter				out1fmt("unlimited\n");
42917987Speter			else
43017987Speter			{
43117987Speter				val /= l->factor;
43218018Speter				out1fmt("%qd\n", (quad_t) val);
43317987Speter			}
43411571Sjoerg		}
43517987Speter		return 0;
43611571Sjoerg	}
43717987Speter
43818016Speter	if (getrlimit(l->cmd, &limit) < 0)
43920425Ssteve		error("ulimit: can't get limit: %s", strerror(errno));
44017987Speter	if (set) {
44117987Speter		if (how & SOFT)
44217987Speter			limit.rlim_cur = val;
44317987Speter		if (how & HARD)
44417987Speter			limit.rlim_max = val;
44517987Speter		if (setrlimit(l->cmd, &limit) < 0)
44620425Ssteve			error("ulimit: bad limit: %s", strerror(errno));
44717987Speter	} else {
44817987Speter		if (how & SOFT)
44917987Speter			val = limit.rlim_cur;
45017987Speter		else if (how & HARD)
45117987Speter			val = limit.rlim_max;
45217987Speter
45317987Speter		if (val == RLIM_INFINITY)
45417987Speter			out1fmt("unlimited\n");
45517987Speter		else
45617987Speter		{
45717987Speter			val /= l->factor;
45818018Speter			out1fmt("%qd\n", (quad_t) val);
45917987Speter		}
46017987Speter	}
46111571Sjoerg	return 0;
46211571Sjoerg}
463