miscbltin.c revision 104282
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 104282 2002-10-01 11:44:38Z mux $");
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>
56104282Smux#include <stdint.h>
5718018Speter#include <stdio.h>
5838536Scracauer#include <stdlib.h>
5929983Smsmith#include <termios.h>
6017987Speter
611556Srgrimes#include "shell.h"
621556Srgrimes#include "options.h"
631556Srgrimes#include "var.h"
641556Srgrimes#include "output.h"
651556Srgrimes#include "memalloc.h"
661556Srgrimes#include "error.h"
671556Srgrimes#include "mystring.h"
681556Srgrimes
691556Srgrimes#undef eflag
701556Srgrimes
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.
761556Srgrimes */
771556Srgrimes
7817987Speterint
7990111Simpreadcmd(int argc __unused, char **argv __unused)
8017987Speter{
811556Srgrimes	char **ap;
821556Srgrimes	int backslash;
831556Srgrimes	char c;
8450394Stg	int rflag;
851556Srgrimes	char *prompt;
861556Srgrimes	char *ifs;
871556Srgrimes	char *p;
881556Srgrimes	int startword;
891556Srgrimes	int status;
901556Srgrimes	int i;
9129983Smsmith	struct timeval tv;
9229983Smsmith	char *tvptr;
9329983Smsmith	fd_set ifds;
9429983Smsmith	struct termios told, tnew;
9529983Smsmith	int tsaved;
961556Srgrimes
9750394Stg	rflag = 0;
981556Srgrimes	prompt = NULL;
9929983Smsmith	tv.tv_sec = -1;
10029983Smsmith	tv.tv_usec = 0;
10150394Stg	while ((i = nextopt("erp:t:")) != '\0') {
10229983Smsmith		switch(i) {
10329983Smsmith		case 'p':
10459436Scracauer			prompt = shoptarg;
10529983Smsmith			break;
10629983Smsmith		case 'e':
10729983Smsmith			break;
10850394Stg		case 'r':
10950394Stg			rflag = 1;
11050394Stg			break;
11129983Smsmith		case 't':
11259436Scracauer			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
11359436Scracauer			if (tvptr == shoptarg)
11429983Smsmith				error("timeout value");
11529983Smsmith			switch(*tvptr) {
11629983Smsmith			case 0:
11729983Smsmith			case 's':
11829983Smsmith				break;
11929983Smsmith			case 'h':
12029983Smsmith				tv.tv_sec *= 60;
12129983Smsmith				/* FALLTHROUGH */
12229983Smsmith			case 'm':
12329983Smsmith				tv.tv_sec *= 60;
12429983Smsmith				break;
12529983Smsmith			default:
12629983Smsmith				error("timeout unit");
12729983Smsmith			}
12829983Smsmith			break;
12929983Smsmith		}
1301556Srgrimes	}
1311556Srgrimes	if (prompt && isatty(0)) {
1321556Srgrimes		out2str(prompt);
1331556Srgrimes		flushall();
1341556Srgrimes	}
1351556Srgrimes	if (*(ap = argptr) == NULL)
1361556Srgrimes		error("arg count");
1371556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
1381556Srgrimes		ifs = nullstr;
13929983Smsmith
14029983Smsmith	if (tv.tv_sec >= 0) {
14129983Smsmith		/*
14229983Smsmith		 * See if we can disable input processing; this will
14329983Smsmith		 * not give the desired result if we are in a pipeline
14429983Smsmith		 * and someone upstream is still in line-by-line mode.
14529983Smsmith		 */
14629983Smsmith		tsaved = 0;
14729983Smsmith		if (tcgetattr(0, &told) == 0) {
14829983Smsmith			memcpy(&tnew, &told, sizeof(told));
14929983Smsmith			cfmakeraw(&tnew);
15029983Smsmith			tcsetattr(0, TCSANOW, &tnew);
15129983Smsmith			tsaved = 1;
15229983Smsmith		}
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		if (tsaved)
16029983Smsmith			tcsetattr(0, TCSANOW, &told);
16129983Smsmith		/*
16229983Smsmith		 * If there's nothing ready, return an error.
16329983Smsmith		 */
16429983Smsmith		if (status <= 0)
16529983Smsmith			return(1);
16629983Smsmith	}
16729983Smsmith
1681556Srgrimes	status = 0;
1691556Srgrimes	startword = 1;
1701556Srgrimes	backslash = 0;
1711556Srgrimes	STARTSTACKSTR(p);
1721556Srgrimes	for (;;) {
17380381Ssheldonh		if (read(STDIN_FILENO, &c, 1) != 1) {
1741556Srgrimes			status = 1;
1751556Srgrimes			break;
1761556Srgrimes		}
1771556Srgrimes		if (c == '\0')
1781556Srgrimes			continue;
1791556Srgrimes		if (backslash) {
1801556Srgrimes			backslash = 0;
1811556Srgrimes			if (c != '\n')
1821556Srgrimes				STPUTC(c, p);
1831556Srgrimes			continue;
1841556Srgrimes		}
18550394Stg		if (!rflag && c == '\\') {
1861556Srgrimes			backslash++;
1871556Srgrimes			continue;
1881556Srgrimes		}
1891556Srgrimes		if (c == '\n')
1901556Srgrimes			break;
1911556Srgrimes		if (startword && *ifs == ' ' && strchr(ifs, c)) {
1921556Srgrimes			continue;
1931556Srgrimes		}
1941556Srgrimes		startword = 0;
1951556Srgrimes		if (backslash && c == '\\') {
19680381Ssheldonh			if (read(STDIN_FILENO, &c, 1) != 1) {
1971556Srgrimes				status = 1;
1981556Srgrimes				break;
1991556Srgrimes			}
2001556Srgrimes			STPUTC(c, p);
2011556Srgrimes		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
2021556Srgrimes			STACKSTRNUL(p);
2031556Srgrimes			setvar(*ap, stackblock(), 0);
2041556Srgrimes			ap++;
2051556Srgrimes			startword = 1;
2061556Srgrimes			STARTSTACKSTR(p);
2071556Srgrimes		} else {
2081556Srgrimes			STPUTC(c, p);
2091556Srgrimes		}
2101556Srgrimes	}
2111556Srgrimes	STACKSTRNUL(p);
2121556Srgrimes	setvar(*ap, stackblock(), 0);
2131556Srgrimes	while (*++ap != NULL)
2141556Srgrimes		setvar(*ap, nullstr, 0);
2151556Srgrimes	return status;
2161556Srgrimes}
2171556Srgrimes
2181556Srgrimes
2191556Srgrimes
22017987Speterint
22190111Simpumaskcmd(int argc __unused, char **argv)
22217987Speter{
22317987Speter	char *ap;
2241556Srgrimes	int mask;
2251556Srgrimes	int i;
22617987Speter	int symbolic_mode = 0;
2271556Srgrimes
22817987Speter	while ((i = nextopt("S")) != '\0') {
22917987Speter		symbolic_mode = 1;
2301556Srgrimes	}
23111571Sjoerg
23217987Speter	INTOFF;
23317987Speter	mask = umask(0);
23417987Speter	umask(mask);
23517987Speter	INTON;
23611571Sjoerg
23717987Speter	if ((ap = *argptr) == NULL) {
23817987Speter		if (symbolic_mode) {
23917987Speter			char u[4], g[4], o[4];
24011571Sjoerg
24117987Speter			i = 0;
24217987Speter			if ((mask & S_IRUSR) == 0)
24317987Speter				u[i++] = 'r';
24417987Speter			if ((mask & S_IWUSR) == 0)
24517987Speter				u[i++] = 'w';
24617987Speter			if ((mask & S_IXUSR) == 0)
24717987Speter				u[i++] = 'x';
24817987Speter			u[i] = '\0';
24911571Sjoerg
25017987Speter			i = 0;
25117987Speter			if ((mask & S_IRGRP) == 0)
25217987Speter				g[i++] = 'r';
25317987Speter			if ((mask & S_IWGRP) == 0)
25417987Speter				g[i++] = 'w';
25517987Speter			if ((mask & S_IXGRP) == 0)
25617987Speter				g[i++] = 'x';
25717987Speter			g[i] = '\0';
25811571Sjoerg
25917987Speter			i = 0;
26017987Speter			if ((mask & S_IROTH) == 0)
26117987Speter				o[i++] = 'r';
26217987Speter			if ((mask & S_IWOTH) == 0)
26317987Speter				o[i++] = 'w';
26417987Speter			if ((mask & S_IXOTH) == 0)
26517987Speter				o[i++] = 'x';
26617987Speter			o[i] = '\0';
26711571Sjoerg
26817987Speter			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
26917987Speter		} else {
27017987Speter			out1fmt("%.4o\n", mask);
27117987Speter		}
27217987Speter	} else {
27317987Speter		if (isdigit(*ap)) {
27417987Speter			mask = 0;
27517987Speter			do {
27617987Speter				if (*ap >= '8' || *ap < '0')
27717987Speter					error("Illegal number: %s", argv[1]);
27817987Speter				mask = (mask << 3) + (*ap - '0');
27917987Speter			} while (*++ap != '\0');
28017987Speter			umask(mask);
28117987Speter		} else {
28220425Ssteve			void *set;
28317987Speter			if ((set = setmode (ap)) == 0)
28441844Simp				error("Illegal number: %s", ap);
28511571Sjoerg
28617987Speter			mask = getmode (set, ~mask & 0777);
28717987Speter			umask(~mask & 0777);
28841844Simp			free(set);
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
34652072Sgreen#ifdef RLIMIT_SBSIZE
34752072Sgreen	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
34852072Sgreen#endif
34918016Speter	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
35017987Speter};
35117987Speter
35217987Speterint
35390111Simpulimitcmd(int argc __unused, char **argv __unused)
35417987Speter{
35525222Ssteve	int	c;
356104282Smux	rlim_t val = 0;
35717987Speter	enum { SOFT = 0x1, HARD = 0x2 }
35817987Speter			how = SOFT | HARD;
35917987Speter	const struct limits	*l;
36017987Speter	int		set, all = 0;
36117987Speter	int		optc, what;
36217987Speter	struct rlimit	limit;
36311571Sjoerg
36417987Speter	what = 'f';
36598834Sdillon	while ((optc = nextopt("HSatfdsmcnuvlb")) != '\0')
36617987Speter		switch (optc) {
36711571Sjoerg		case 'H':
36817987Speter			how = HARD;
36911571Sjoerg			break;
37011571Sjoerg		case 'S':
37117987Speter			how = SOFT;
37211571Sjoerg			break;
37311571Sjoerg		case 'a':
37417987Speter			all = 1;
37511571Sjoerg			break;
37617987Speter		default:
37717987Speter			what = optc;
37811571Sjoerg		}
37911571Sjoerg
38017987Speter	for (l = limits; l->name && l->option != what; l++)
38117987Speter		;
38217987Speter	if (!l->name)
383104208Stjr		error("internal error (%c)", what);
38417987Speter
38517987Speter	set = *argptr ? 1 : 0;
38617987Speter	if (set) {
38717987Speter		char *p = *argptr;
38817987Speter
38917987Speter		if (all || argptr[1])
390104208Stjr			error("too many arguments");
39117987Speter		if (strcmp(p, "unlimited") == 0)
39211571Sjoerg			val = RLIM_INFINITY;
39311571Sjoerg		else {
394104282Smux			val = 0;
39517987Speter
39617987Speter			while ((c = *p++) >= '0' && c <= '9')
39717987Speter			{
39817987Speter				val = (val * 10) + (long)(c - '0');
399104282Smux				if (val < 0)
40017987Speter					break;
40117987Speter			}
40217987Speter			if (c)
403104208Stjr				error("bad number");
40417987Speter			val *= l->factor;
40511571Sjoerg		}
40617987Speter	}
40717987Speter	if (all) {
40818016Speter		for (l = limits; l->name; l++) {
40918016Speter			char optbuf[40];
41018016Speter			if (getrlimit(l->cmd, &limit) < 0)
411104208Stjr				error("can't get limit: %s", strerror(errno));
41217987Speter			if (how & SOFT)
41317987Speter				val = limit.rlim_cur;
41417987Speter			else if (how & HARD)
41517987Speter				val = limit.rlim_max;
41617987Speter
41718016Speter			if (l->units)
41818016Speter				snprintf(optbuf, sizeof(optbuf),
41918019Speter					"(%s, -%c) ", l->units, l->option);
42018016Speter			else
42118016Speter				snprintf(optbuf, sizeof(optbuf),
42218019Speter					"(-%c) ", l->option);
42318019Speter			out1fmt("%-18s %18s ", l->name, optbuf);
42417987Speter			if (val == RLIM_INFINITY)
42517987Speter				out1fmt("unlimited\n");
42617987Speter			else
42717987Speter			{
42817987Speter				val /= l->factor;
429104282Smux				out1fmt("%jd\n", (intmax_t)val);
43017987Speter			}
43111571Sjoerg		}
43217987Speter		return 0;
43311571Sjoerg	}
43417987Speter
43518016Speter	if (getrlimit(l->cmd, &limit) < 0)
436104208Stjr		error("can't get limit: %s", strerror(errno));
43717987Speter	if (set) {
43817987Speter		if (how & SOFT)
43917987Speter			limit.rlim_cur = val;
44017987Speter		if (how & HARD)
44117987Speter			limit.rlim_max = val;
44217987Speter		if (setrlimit(l->cmd, &limit) < 0)
443104208Stjr			error("bad limit: %s", strerror(errno));
44417987Speter	} else {
44517987Speter		if (how & SOFT)
44617987Speter			val = limit.rlim_cur;
44717987Speter		else if (how & HARD)
44817987Speter			val = limit.rlim_max;
44917987Speter
45017987Speter		if (val == RLIM_INFINITY)
45117987Speter			out1fmt("unlimited\n");
45217987Speter		else
45317987Speter		{
45417987Speter			val /= l->factor;
455104282Smux			out1fmt("%jd\n", (intmax_t)val);
45617987Speter		}
45717987Speter	}
45811571Sjoerg	return 0;
45911571Sjoerg}
460