xref: /freebsd/bin/sh/miscbltin.c (revision 1f40b47b4600d045d67c45a09a4feed9dbff22a2)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
421f40b47bSMartin Cracauer 	"$Id: miscbltin.c,v 1.16 1998/08/24 10:20:36 cracauer Exp $";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes /*
464b88c807SRodney W. Grimes  * Miscelaneous builtins.
474b88c807SRodney W. Grimes  */
484b88c807SRodney W. Grimes 
49aa9caaf6SPeter Wemm #include <sys/types.h>
50aa9caaf6SPeter Wemm #include <sys/stat.h>
51aa9caaf6SPeter Wemm #include <sys/time.h>
52aa9caaf6SPeter Wemm #include <sys/resource.h>
53aa9caaf6SPeter Wemm #include <unistd.h>
54aa9caaf6SPeter Wemm #include <ctype.h>
5516992ff4SPeter Wemm #include <errno.h>
564417f629SPeter Wemm #include <stdio.h>
571f40b47bSMartin Cracauer #include <stdlib.h>
58afa53c8dSMike Smith #include <termios.h>
59aa9caaf6SPeter Wemm 
604b88c807SRodney W. Grimes #include "shell.h"
614b88c807SRodney W. Grimes #include "options.h"
624b88c807SRodney W. Grimes #include "var.h"
634b88c807SRodney W. Grimes #include "output.h"
644b88c807SRodney W. Grimes #include "memalloc.h"
654b88c807SRodney W. Grimes #include "error.h"
664b88c807SRodney W. Grimes #include "mystring.h"
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes #undef eflag
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes extern char **argptr;		/* argument list for builtin command */
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes /*
744b88c807SRodney W. Grimes  * The read builtin.  The -e option causes backslashes to escape the
754b88c807SRodney W. Grimes  * following character.
764b88c807SRodney W. Grimes  *
774b88c807SRodney W. Grimes  * This uses unbuffered input, which may be avoidable in some cases.
784b88c807SRodney W. Grimes  */
794b88c807SRodney W. Grimes 
80aa9caaf6SPeter Wemm int
81aa9caaf6SPeter Wemm readcmd(argc, argv)
82e7a0b024SSteve Price 	int argc __unused;
83e7a0b024SSteve Price 	char **argv __unused;
84aa9caaf6SPeter Wemm {
854b88c807SRodney W. Grimes 	char **ap;
864b88c807SRodney W. Grimes 	int backslash;
874b88c807SRodney W. Grimes 	char c;
884b88c807SRodney W. Grimes 	int eflag;
894b88c807SRodney W. Grimes 	char *prompt;
904b88c807SRodney W. Grimes 	char *ifs;
914b88c807SRodney W. Grimes 	char *p;
924b88c807SRodney W. Grimes 	int startword;
934b88c807SRodney W. Grimes 	int status;
944b88c807SRodney W. Grimes 	int i;
95afa53c8dSMike Smith 	struct timeval tv;
96afa53c8dSMike Smith 	char *tvptr;
97afa53c8dSMike Smith 	fd_set ifds;
98afa53c8dSMike Smith 	struct termios told, tnew;
99afa53c8dSMike Smith 	int tsaved;
1004b88c807SRodney W. Grimes 
1014b88c807SRodney W. Grimes 	eflag = 0;
1024b88c807SRodney W. Grimes 	prompt = NULL;
103afa53c8dSMike Smith 	tv.tv_sec = -1;
104afa53c8dSMike Smith 	tv.tv_usec = 0;
105afa53c8dSMike Smith 	while ((i = nextopt("ep:t:")) != '\0') {
106afa53c8dSMike Smith 		switch(i) {
107afa53c8dSMike Smith 		case 'p':
1084b88c807SRodney W. Grimes 			prompt = optarg;
109afa53c8dSMike Smith 			break;
110afa53c8dSMike Smith 		case 'e':
1114b88c807SRodney W. Grimes 			eflag = 1;
112afa53c8dSMike Smith 			break;
113afa53c8dSMike Smith 		case 't':
114afa53c8dSMike Smith 			tv.tv_sec = strtol(optarg, &tvptr, 0);
115afa53c8dSMike Smith 			if (tvptr == optarg)
116afa53c8dSMike Smith 				error("timeout value");
117afa53c8dSMike Smith 			switch(*tvptr) {
118afa53c8dSMike Smith 			case 0:
119afa53c8dSMike Smith 			case 's':
120afa53c8dSMike Smith 				break;
121afa53c8dSMike Smith 			case 'h':
122afa53c8dSMike Smith 				tv.tv_sec *= 60;
123afa53c8dSMike Smith 				/* FALLTHROUGH */
124afa53c8dSMike Smith 			case 'm':
125afa53c8dSMike Smith 				tv.tv_sec *= 60;
126afa53c8dSMike Smith 				break;
127afa53c8dSMike Smith 			default:
128afa53c8dSMike Smith 				error("timeout unit");
129afa53c8dSMike Smith 			}
130afa53c8dSMike Smith 			break;
131afa53c8dSMike Smith 		}
1324b88c807SRodney W. Grimes 	}
1334b88c807SRodney W. Grimes 	if (prompt && isatty(0)) {
1344b88c807SRodney W. Grimes 		out2str(prompt);
1354b88c807SRodney W. Grimes 		flushall();
1364b88c807SRodney W. Grimes 	}
1374b88c807SRodney W. Grimes 	if (*(ap = argptr) == NULL)
1384b88c807SRodney W. Grimes 		error("arg count");
1394b88c807SRodney W. Grimes 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
1404b88c807SRodney W. Grimes 		ifs = nullstr;
141afa53c8dSMike Smith 
142afa53c8dSMike Smith 	if (tv.tv_sec >= 0) {
143afa53c8dSMike Smith 		/*
144afa53c8dSMike Smith 		 * See if we can disable input processing; this will
145afa53c8dSMike Smith 		 * not give the desired result if we are in a pipeline
146afa53c8dSMike Smith 		 * and someone upstream is still in line-by-line mode.
147afa53c8dSMike Smith 		 */
148afa53c8dSMike Smith 		tsaved = 0;
149afa53c8dSMike Smith 		if (tcgetattr(0, &told) == 0) {
150afa53c8dSMike Smith 			memcpy(&tnew, &told, sizeof(told));
151afa53c8dSMike Smith 			cfmakeraw(&tnew);
152afa53c8dSMike Smith 			tcsetattr(0, TCSANOW, &tnew);
153afa53c8dSMike Smith 			tsaved = 1;
154afa53c8dSMike Smith 		}
155afa53c8dSMike Smith 		/*
156afa53c8dSMike Smith 		 * Wait for something to become available.
157afa53c8dSMike Smith 		 */
158afa53c8dSMike Smith 		FD_ZERO(&ifds);
159afa53c8dSMike Smith 		FD_SET(0, &ifds);
160afa53c8dSMike Smith 		status = select(1, &ifds, NULL, NULL, &tv);
161afa53c8dSMike Smith 		if (tsaved)
162afa53c8dSMike Smith 			tcsetattr(0, TCSANOW, &told);
163afa53c8dSMike Smith 		/*
164afa53c8dSMike Smith 		 * If there's nothing ready, return an error.
165afa53c8dSMike Smith 		 */
166afa53c8dSMike Smith 		if (status <= 0)
167afa53c8dSMike Smith 			return(1);
168afa53c8dSMike Smith 	}
169afa53c8dSMike Smith 
1704b88c807SRodney W. Grimes 	status = 0;
1714b88c807SRodney W. Grimes 	startword = 1;
1724b88c807SRodney W. Grimes 	backslash = 0;
1734b88c807SRodney W. Grimes 	STARTSTACKSTR(p);
1744b88c807SRodney W. Grimes 	for (;;) {
1754b88c807SRodney W. Grimes 		if (read(0, &c, 1) != 1) {
1764b88c807SRodney W. Grimes 			status = 1;
1774b88c807SRodney W. Grimes 			break;
1784b88c807SRodney W. Grimes 		}
1794b88c807SRodney W. Grimes 		if (c == '\0')
1804b88c807SRodney W. Grimes 			continue;
1814b88c807SRodney W. Grimes 		if (backslash) {
1824b88c807SRodney W. Grimes 			backslash = 0;
1834b88c807SRodney W. Grimes 			if (c != '\n')
1844b88c807SRodney W. Grimes 				STPUTC(c, p);
1854b88c807SRodney W. Grimes 			continue;
1864b88c807SRodney W. Grimes 		}
1874b88c807SRodney W. Grimes 		if (eflag && c == '\\') {
1884b88c807SRodney W. Grimes 			backslash++;
1894b88c807SRodney W. Grimes 			continue;
1904b88c807SRodney W. Grimes 		}
1914b88c807SRodney W. Grimes 		if (c == '\n')
1924b88c807SRodney W. Grimes 			break;
1934b88c807SRodney W. Grimes 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
1944b88c807SRodney W. Grimes 			continue;
1954b88c807SRodney W. Grimes 		}
1964b88c807SRodney W. Grimes 		startword = 0;
1974b88c807SRodney W. Grimes 		if (backslash && c == '\\') {
1984b88c807SRodney W. Grimes 			if (read(0, &c, 1) != 1) {
1994b88c807SRodney W. Grimes 				status = 1;
2004b88c807SRodney W. Grimes 				break;
2014b88c807SRodney W. Grimes 			}
2024b88c807SRodney W. Grimes 			STPUTC(c, p);
2034b88c807SRodney W. Grimes 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
2044b88c807SRodney W. Grimes 			STACKSTRNUL(p);
2054b88c807SRodney W. Grimes 			setvar(*ap, stackblock(), 0);
2064b88c807SRodney W. Grimes 			ap++;
2074b88c807SRodney W. Grimes 			startword = 1;
2084b88c807SRodney W. Grimes 			STARTSTACKSTR(p);
2094b88c807SRodney W. Grimes 		} else {
2104b88c807SRodney W. Grimes 			STPUTC(c, p);
2114b88c807SRodney W. Grimes 		}
2124b88c807SRodney W. Grimes 	}
2134b88c807SRodney W. Grimes 	STACKSTRNUL(p);
2144b88c807SRodney W. Grimes 	setvar(*ap, stackblock(), 0);
2154b88c807SRodney W. Grimes 	while (*++ap != NULL)
2164b88c807SRodney W. Grimes 		setvar(*ap, nullstr, 0);
2174b88c807SRodney W. Grimes 	return status;
2184b88c807SRodney W. Grimes }
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes 
222aa9caaf6SPeter Wemm int
223aa9caaf6SPeter Wemm umaskcmd(argc, argv)
224e7a0b024SSteve Price 	int argc __unused;
225aa9caaf6SPeter Wemm 	char **argv;
226aa9caaf6SPeter Wemm {
227aa9caaf6SPeter Wemm 	char *ap;
2284b88c807SRodney W. Grimes 	int mask;
2294b88c807SRodney W. Grimes 	int i;
230aa9caaf6SPeter Wemm 	int symbolic_mode = 0;
2314b88c807SRodney W. Grimes 
232aa9caaf6SPeter Wemm 	while ((i = nextopt("S")) != '\0') {
233aa9caaf6SPeter Wemm 		symbolic_mode = 1;
234aa9caaf6SPeter Wemm 	}
235aa9caaf6SPeter Wemm 
2364b88c807SRodney W. Grimes 	INTOFF;
2374b88c807SRodney W. Grimes 	mask = umask(0);
2384b88c807SRodney W. Grimes 	umask(mask);
2394b88c807SRodney W. Grimes 	INTON;
240aa9caaf6SPeter Wemm 
241aa9caaf6SPeter Wemm 	if ((ap = *argptr) == NULL) {
242aa9caaf6SPeter Wemm 		if (symbolic_mode) {
243aa9caaf6SPeter Wemm 			char u[4], g[4], o[4];
244aa9caaf6SPeter Wemm 
245aa9caaf6SPeter Wemm 			i = 0;
246aa9caaf6SPeter Wemm 			if ((mask & S_IRUSR) == 0)
247aa9caaf6SPeter Wemm 				u[i++] = 'r';
248aa9caaf6SPeter Wemm 			if ((mask & S_IWUSR) == 0)
249aa9caaf6SPeter Wemm 				u[i++] = 'w';
250aa9caaf6SPeter Wemm 			if ((mask & S_IXUSR) == 0)
251aa9caaf6SPeter Wemm 				u[i++] = 'x';
252aa9caaf6SPeter Wemm 			u[i] = '\0';
253aa9caaf6SPeter Wemm 
254aa9caaf6SPeter Wemm 			i = 0;
255aa9caaf6SPeter Wemm 			if ((mask & S_IRGRP) == 0)
256aa9caaf6SPeter Wemm 				g[i++] = 'r';
257aa9caaf6SPeter Wemm 			if ((mask & S_IWGRP) == 0)
258aa9caaf6SPeter Wemm 				g[i++] = 'w';
259aa9caaf6SPeter Wemm 			if ((mask & S_IXGRP) == 0)
260aa9caaf6SPeter Wemm 				g[i++] = 'x';
261aa9caaf6SPeter Wemm 			g[i] = '\0';
262aa9caaf6SPeter Wemm 
263aa9caaf6SPeter Wemm 			i = 0;
264aa9caaf6SPeter Wemm 			if ((mask & S_IROTH) == 0)
265aa9caaf6SPeter Wemm 				o[i++] = 'r';
266aa9caaf6SPeter Wemm 			if ((mask & S_IWOTH) == 0)
267aa9caaf6SPeter Wemm 				o[i++] = 'w';
268aa9caaf6SPeter Wemm 			if ((mask & S_IXOTH) == 0)
269aa9caaf6SPeter Wemm 				o[i++] = 'x';
270aa9caaf6SPeter Wemm 			o[i] = '\0';
271aa9caaf6SPeter Wemm 
272aa9caaf6SPeter Wemm 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
2734b88c807SRodney W. Grimes 		} else {
274aa9caaf6SPeter Wemm 			out1fmt("%.4o\n", mask);
275aa9caaf6SPeter Wemm 		}
276aa9caaf6SPeter Wemm 	} else {
277aa9caaf6SPeter Wemm 		if (isdigit(*ap)) {
2784b88c807SRodney W. Grimes 			mask = 0;
2794b88c807SRodney W. Grimes 			do {
280aa9caaf6SPeter Wemm 				if (*ap >= '8' || *ap < '0')
2814b88c807SRodney W. Grimes 					error("Illegal number: %s", argv[1]);
282aa9caaf6SPeter Wemm 				mask = (mask << 3) + (*ap - '0');
283aa9caaf6SPeter Wemm 			} while (*++ap != '\0');
2844b88c807SRodney W. Grimes 			umask(mask);
285aa9caaf6SPeter Wemm 		} else {
286aa9caaf6SPeter Wemm 			void *set;
287aa9caaf6SPeter Wemm 			if ((set = setmode (ap)) == 0)
288aa9caaf6SPeter Wemm 					error("Illegal number: %s", ap);
289aa9caaf6SPeter Wemm 
290aa9caaf6SPeter Wemm 			mask = getmode (set, ~mask & 0777);
291aa9caaf6SPeter Wemm 			umask(~mask & 0777);
292aa9caaf6SPeter Wemm 		}
2934b88c807SRodney W. Grimes 	}
2944b88c807SRodney W. Grimes 	return 0;
2954b88c807SRodney W. Grimes }
2967a2afe64SJoerg Wunsch 
297aa9caaf6SPeter Wemm /*
298aa9caaf6SPeter Wemm  * ulimit builtin
299aa9caaf6SPeter Wemm  *
300aa9caaf6SPeter Wemm  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
301aa9caaf6SPeter Wemm  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
302aa9caaf6SPeter Wemm  * ash by J.T. Conklin.
303aa9caaf6SPeter Wemm  *
304aa9caaf6SPeter Wemm  * Public domain.
305aa9caaf6SPeter Wemm  */
3067a2afe64SJoerg Wunsch 
307aa9caaf6SPeter Wemm struct limits {
308aa9caaf6SPeter Wemm 	const char *name;
30916992ff4SPeter Wemm 	const char *units;
310aa9caaf6SPeter Wemm 	int	cmd;
311aa9caaf6SPeter Wemm 	int	factor;	/* multiply by to get rlim_{cur,max} values */
312aa9caaf6SPeter Wemm 	char	option;
3137a2afe64SJoerg Wunsch };
3147a2afe64SJoerg Wunsch 
315aa9caaf6SPeter Wemm static const struct limits limits[] = {
316aa9caaf6SPeter Wemm #ifdef RLIMIT_CPU
31716992ff4SPeter Wemm 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
318aa9caaf6SPeter Wemm #endif
319aa9caaf6SPeter Wemm #ifdef RLIMIT_FSIZE
32016992ff4SPeter Wemm 	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
321aa9caaf6SPeter Wemm #endif
322aa9caaf6SPeter Wemm #ifdef RLIMIT_DATA
32316992ff4SPeter Wemm 	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
324aa9caaf6SPeter Wemm #endif
325aa9caaf6SPeter Wemm #ifdef RLIMIT_STACK
32616992ff4SPeter Wemm 	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
327aa9caaf6SPeter Wemm #endif
328aa9caaf6SPeter Wemm #ifdef  RLIMIT_CORE
32916992ff4SPeter Wemm 	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
330aa9caaf6SPeter Wemm #endif
331aa9caaf6SPeter Wemm #ifdef RLIMIT_RSS
33216992ff4SPeter Wemm 	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
333aa9caaf6SPeter Wemm #endif
334aa9caaf6SPeter Wemm #ifdef RLIMIT_MEMLOCK
33516992ff4SPeter Wemm 	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
336aa9caaf6SPeter Wemm #endif
337aa9caaf6SPeter Wemm #ifdef RLIMIT_NPROC
33816992ff4SPeter Wemm 	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
339aa9caaf6SPeter Wemm #endif
340aa9caaf6SPeter Wemm #ifdef RLIMIT_NOFILE
34116992ff4SPeter Wemm 	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
342aa9caaf6SPeter Wemm #endif
343aa9caaf6SPeter Wemm #ifdef RLIMIT_VMEM
34416992ff4SPeter Wemm 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
345aa9caaf6SPeter Wemm #endif
346aa9caaf6SPeter Wemm #ifdef RLIMIT_SWAP
34716992ff4SPeter Wemm 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
348aa9caaf6SPeter Wemm #endif
34916992ff4SPeter Wemm 	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
3507a2afe64SJoerg Wunsch };
3517a2afe64SJoerg Wunsch 
352aa9caaf6SPeter Wemm int
353aa9caaf6SPeter Wemm ulimitcmd(argc, argv)
354e7a0b024SSteve Price 	int argc __unused;
355e7a0b024SSteve Price 	char **argv __unused;
356aa9caaf6SPeter Wemm {
357afb033d5SSteve Price 	int	c;
3584417f629SPeter Wemm 	quad_t val = 0;
359aa9caaf6SPeter Wemm 	enum { SOFT = 0x1, HARD = 0x2 }
360aa9caaf6SPeter Wemm 			how = SOFT | HARD;
361aa9caaf6SPeter Wemm 	const struct limits	*l;
362aa9caaf6SPeter Wemm 	int		set, all = 0;
363aa9caaf6SPeter Wemm 	int		optc, what;
364aa9caaf6SPeter Wemm 	struct rlimit	limit;
3657a2afe64SJoerg Wunsch 
366aa9caaf6SPeter Wemm 	what = 'f';
36716992ff4SPeter Wemm 	while ((optc = nextopt("HSatfdsmcnul")) != '\0')
368aa9caaf6SPeter Wemm 		switch (optc) {
3697a2afe64SJoerg Wunsch 		case 'H':
370aa9caaf6SPeter Wemm 			how = HARD;
3717a2afe64SJoerg Wunsch 			break;
3727a2afe64SJoerg Wunsch 		case 'S':
373aa9caaf6SPeter Wemm 			how = SOFT;
3747a2afe64SJoerg Wunsch 			break;
3757a2afe64SJoerg Wunsch 		case 'a':
376aa9caaf6SPeter Wemm 			all = 1;
3777a2afe64SJoerg Wunsch 			break;
378aa9caaf6SPeter Wemm 		default:
379aa9caaf6SPeter Wemm 			what = optc;
3807a2afe64SJoerg Wunsch 		}
3817a2afe64SJoerg Wunsch 
382aa9caaf6SPeter Wemm 	for (l = limits; l->name && l->option != what; l++)
383aa9caaf6SPeter Wemm 		;
384aa9caaf6SPeter Wemm 	if (!l->name)
385ab0a2172SSteve Price 		error("ulimit: internal error (%c)", what);
3867a2afe64SJoerg Wunsch 
387aa9caaf6SPeter Wemm 	set = *argptr ? 1 : 0;
388aa9caaf6SPeter Wemm 	if (set) {
389aa9caaf6SPeter Wemm 		char *p = *argptr;
390aa9caaf6SPeter Wemm 
391aa9caaf6SPeter Wemm 		if (all || argptr[1])
392ab0a2172SSteve Price 			error("ulimit: too many arguments");
393aa9caaf6SPeter Wemm 		if (strcmp(p, "unlimited") == 0)
3947a2afe64SJoerg Wunsch 			val = RLIM_INFINITY;
3957a2afe64SJoerg Wunsch 		else {
396aa9caaf6SPeter Wemm 			val = (quad_t) 0;
397aa9caaf6SPeter Wemm 
398aa9caaf6SPeter Wemm 			while ((c = *p++) >= '0' && c <= '9')
399aa9caaf6SPeter Wemm 			{
400aa9caaf6SPeter Wemm 				val = (val * 10) + (long)(c - '0');
401aa9caaf6SPeter Wemm 				if (val < (quad_t) 0)
402aa9caaf6SPeter Wemm 					break;
4037a2afe64SJoerg Wunsch 			}
404aa9caaf6SPeter Wemm 			if (c)
405ab0a2172SSteve Price 				error("ulimit: bad number");
406aa9caaf6SPeter Wemm 			val *= l->factor;
407aa9caaf6SPeter Wemm 		}
408aa9caaf6SPeter Wemm 	}
409aa9caaf6SPeter Wemm 	if (all) {
410aa9caaf6SPeter Wemm 		for (l = limits; l->name; l++) {
41116992ff4SPeter Wemm 			char optbuf[40];
41216992ff4SPeter Wemm 			if (getrlimit(l->cmd, &limit) < 0)
413ab0a2172SSteve Price 				error("ulimit: can't get limit: %s", strerror(errno));
414aa9caaf6SPeter Wemm 			if (how & SOFT)
415aa9caaf6SPeter Wemm 				val = limit.rlim_cur;
416aa9caaf6SPeter Wemm 			else if (how & HARD)
417aa9caaf6SPeter Wemm 				val = limit.rlim_max;
418aa9caaf6SPeter Wemm 
41916992ff4SPeter Wemm 			if (l->units)
42016992ff4SPeter Wemm 				snprintf(optbuf, sizeof(optbuf),
4214e4e0959SPeter Wemm 					"(%s, -%c) ", l->units, l->option);
42216992ff4SPeter Wemm 			else
42316992ff4SPeter Wemm 				snprintf(optbuf, sizeof(optbuf),
4244e4e0959SPeter Wemm 					"(-%c) ", l->option);
4254e4e0959SPeter Wemm 			out1fmt("%-18s %18s ", l->name, optbuf);
426aa9caaf6SPeter Wemm 			if (val == RLIM_INFINITY)
427aa9caaf6SPeter Wemm 				out1fmt("unlimited\n");
428aa9caaf6SPeter Wemm 			else
429aa9caaf6SPeter Wemm 			{
430aa9caaf6SPeter Wemm 				val /= l->factor;
4314417f629SPeter Wemm 				out1fmt("%qd\n", (quad_t) val);
4327a2afe64SJoerg Wunsch 			}
4337a2afe64SJoerg Wunsch 		}
4347a2afe64SJoerg Wunsch 		return 0;
4357a2afe64SJoerg Wunsch 	}
436aa9caaf6SPeter Wemm 
43716992ff4SPeter Wemm 	if (getrlimit(l->cmd, &limit) < 0)
438ab0a2172SSteve Price 		error("ulimit: can't get limit: %s", strerror(errno));
439aa9caaf6SPeter Wemm 	if (set) {
440aa9caaf6SPeter Wemm 		if (how & SOFT)
441aa9caaf6SPeter Wemm 			limit.rlim_cur = val;
442aa9caaf6SPeter Wemm 		if (how & HARD)
443aa9caaf6SPeter Wemm 			limit.rlim_max = val;
444aa9caaf6SPeter Wemm 		if (setrlimit(l->cmd, &limit) < 0)
445ab0a2172SSteve Price 			error("ulimit: bad limit: %s", strerror(errno));
446aa9caaf6SPeter Wemm 	} else {
447aa9caaf6SPeter Wemm 		if (how & SOFT)
448aa9caaf6SPeter Wemm 			val = limit.rlim_cur;
449aa9caaf6SPeter Wemm 		else if (how & HARD)
450aa9caaf6SPeter Wemm 			val = limit.rlim_max;
451aa9caaf6SPeter Wemm 
452aa9caaf6SPeter Wemm 		if (val == RLIM_INFINITY)
453aa9caaf6SPeter Wemm 			out1fmt("unlimited\n");
454aa9caaf6SPeter Wemm 		else
455aa9caaf6SPeter Wemm 		{
456aa9caaf6SPeter Wemm 			val /= l->factor;
4574417f629SPeter Wemm 			out1fmt("%qd\n", (quad_t) val);
458aa9caaf6SPeter Wemm 		}
459aa9caaf6SPeter Wemm 	}
460aa9caaf6SPeter Wemm 	return 0;
461aa9caaf6SPeter Wemm }
462