miscbltin.c revision 104208
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
411556Srgrimes#endif /* not lint */
4299110Sobrien#include <sys/cdefs.h>
4399110Sobrien__FBSDID("$FreeBSD: head/bin/sh/miscbltin.c 104208 2002-09-30 13:29:32Z tjr $");
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
701556Srgrimes/*
7150394Stg * The read builtin.  The -r option causes backslashes to be treated like
7250394Stg * ordinary characters.
731556Srgrimes *
741556Srgrimes * This uses unbuffered input, which may be avoidable in some cases.
751556Srgrimes */
761556Srgrimes
7717987Speterint
7890111Simpreadcmd(int argc __unused, char **argv __unused)
7917987Speter{
801556Srgrimes	char **ap;
811556Srgrimes	int backslash;
821556Srgrimes	char c;
8350394Stg	int rflag;
841556Srgrimes	char *prompt;
851556Srgrimes	char *ifs;
861556Srgrimes	char *p;
871556Srgrimes	int startword;
881556Srgrimes	int status;
891556Srgrimes	int i;
9029983Smsmith	struct timeval tv;
9129983Smsmith	char *tvptr;
9229983Smsmith	fd_set ifds;
9329983Smsmith	struct termios told, tnew;
9429983Smsmith	int tsaved;
951556Srgrimes
9650394Stg	rflag = 0;
971556Srgrimes	prompt = NULL;
9829983Smsmith	tv.tv_sec = -1;
9929983Smsmith	tv.tv_usec = 0;
10050394Stg	while ((i = nextopt("erp:t:")) != '\0') {
10129983Smsmith		switch(i) {
10229983Smsmith		case 'p':
10359436Scracauer			prompt = shoptarg;
10429983Smsmith			break;
10529983Smsmith		case 'e':
10629983Smsmith			break;
10750394Stg		case 'r':
10850394Stg			rflag = 1;
10950394Stg			break;
11029983Smsmith		case 't':
11159436Scracauer			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
11259436Scracauer			if (tvptr == shoptarg)
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 (;;) {
17280381Ssheldonh		if (read(STDIN_FILENO, &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		}
18450394Stg		if (!rflag && 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 == '\\') {
19580381Ssheldonh			if (read(STDIN_FILENO, &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
22090111Simpumaskcmd(int argc __unused, char **argv)
22117987Speter{
22217987Speter	char *ap;
2231556Srgrimes	int mask;
2241556Srgrimes	int i;
22517987Speter	int symbolic_mode = 0;
2261556Srgrimes
22717987Speter	while ((i = nextopt("S")) != '\0') {
22817987Speter		symbolic_mode = 1;
2291556Srgrimes	}
23011571Sjoerg
23117987Speter	INTOFF;
23217987Speter	mask = umask(0);
23317987Speter	umask(mask);
23417987Speter	INTON;
23511571Sjoerg
23617987Speter	if ((ap = *argptr) == NULL) {
23717987Speter		if (symbolic_mode) {
23817987Speter			char u[4], g[4], o[4];
23911571Sjoerg
24017987Speter			i = 0;
24117987Speter			if ((mask & S_IRUSR) == 0)
24217987Speter				u[i++] = 'r';
24317987Speter			if ((mask & S_IWUSR) == 0)
24417987Speter				u[i++] = 'w';
24517987Speter			if ((mask & S_IXUSR) == 0)
24617987Speter				u[i++] = 'x';
24717987Speter			u[i] = '\0';
24811571Sjoerg
24917987Speter			i = 0;
25017987Speter			if ((mask & S_IRGRP) == 0)
25117987Speter				g[i++] = 'r';
25217987Speter			if ((mask & S_IWGRP) == 0)
25317987Speter				g[i++] = 'w';
25417987Speter			if ((mask & S_IXGRP) == 0)
25517987Speter				g[i++] = 'x';
25617987Speter			g[i] = '\0';
25711571Sjoerg
25817987Speter			i = 0;
25917987Speter			if ((mask & S_IROTH) == 0)
26017987Speter				o[i++] = 'r';
26117987Speter			if ((mask & S_IWOTH) == 0)
26217987Speter				o[i++] = 'w';
26317987Speter			if ((mask & S_IXOTH) == 0)
26417987Speter				o[i++] = 'x';
26517987Speter			o[i] = '\0';
26611571Sjoerg
26717987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
26817987Speter		} else {
26917987Speter			out1fmt("%.4o\n", mask);
27017987Speter		}
27117987Speter	} else {
27217987Speter		if (isdigit(*ap)) {
27317987Speter			mask = 0;
27417987Speter			do {
27517987Speter				if (*ap >= '8' || *ap < '0')
27617987Speter					error("Illegal number: %s", argv[1]);
27717987Speter				mask = (mask << 3) + (*ap - '0');
27817987Speter			} while (*++ap != '\0');
27917987Speter			umask(mask);
28017987Speter		} else {
28120425Ssteve			void *set;
28217987Speter			if ((set = setmode (ap)) == 0)
28341844Simp				error("Illegal number: %s", ap);
28411571Sjoerg
28517987Speter			mask = getmode (set, ~mask & 0777);
28617987Speter			umask(~mask & 0777);
28741844Simp			free(set);
28817987Speter		}
28917987Speter	}
29011571Sjoerg	return 0;
29111571Sjoerg}
29211571Sjoerg
29317987Speter/*
29417987Speter * ulimit builtin
29517987Speter *
29617987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
29717987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
29817987Speter * ash by J.T. Conklin.
29917987Speter *
30017987Speter * Public domain.
30117987Speter */
30211571Sjoerg
30317987Speterstruct limits {
30417987Speter	const char *name;
30518016Speter	const char *units;
30617987Speter	int	cmd;
30717987Speter	int	factor;	/* multiply by to get rlim_{cur,max} values */
30817987Speter	char	option;
30917987Speter};
31011571Sjoerg
31117987Speterstatic const struct limits limits[] = {
31217987Speter#ifdef RLIMIT_CPU
31318016Speter	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
31417987Speter#endif
31517987Speter#ifdef RLIMIT_FSIZE
31618016Speter	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
31717987Speter#endif
31817987Speter#ifdef RLIMIT_DATA
31918016Speter	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
32017987Speter#endif
32117987Speter#ifdef RLIMIT_STACK
32218016Speter	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
32317987Speter#endif
32417987Speter#ifdef  RLIMIT_CORE
32518016Speter	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
32617987Speter#endif
32717987Speter#ifdef RLIMIT_RSS
32818016Speter	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
32917987Speter#endif
33017987Speter#ifdef RLIMIT_MEMLOCK
33118016Speter	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
33217987Speter#endif
33317987Speter#ifdef RLIMIT_NPROC
33418016Speter	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
33517987Speter#endif
33617987Speter#ifdef RLIMIT_NOFILE
33718016Speter	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
33817987Speter#endif
33917987Speter#ifdef RLIMIT_VMEM
34018016Speter	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
34117987Speter#endif
34217987Speter#ifdef RLIMIT_SWAP
34318016Speter	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
34417987Speter#endif
34552072Sgreen#ifdef RLIMIT_SBSIZE
34652072Sgreen	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
34752072Sgreen#endif
34818016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
34917987Speter};
35017987Speter
35117987Speterint
35290111Simpulimitcmd(int argc __unused, 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';
36498834Sdillon	while ((optc = nextopt("HSatfdsmcnuvlb")) != '\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)
382104208Stjr		error("internal error (%c)", what);
38317987Speter
38417987Speter	set = *argptr ? 1 : 0;
38517987Speter	if (set) {
38617987Speter		char *p = *argptr;
38717987Speter
38817987Speter		if (all || argptr[1])
389104208Stjr			error("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)
402104208Stjr				error("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)
410104208Stjr				error("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)
435104208Stjr		error("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)
442104208Stjr			error("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