xref: /freebsd/bin/sh/miscbltin.c (revision 86d8da5d5b5043d486b7df8e778ce34a7d810199)
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  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
414b88c807SRodney W. Grimes /*
4246be34b9SKris Kennaway  * Miscellaneous builtins.
434b88c807SRodney W. Grimes  */
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <sys/types.h>
46aa9caaf6SPeter Wemm #include <sys/stat.h>
47aa9caaf6SPeter Wemm #include <sys/time.h>
48aa9caaf6SPeter Wemm #include <sys/resource.h>
49aa9caaf6SPeter Wemm #include <unistd.h>
50aa9caaf6SPeter Wemm #include <ctype.h>
5116992ff4SPeter Wemm #include <errno.h>
520c1661b7SMaxime Henrion #include <stdint.h>
534417f629SPeter Wemm #include <stdio.h>
541f40b47bSMartin Cracauer #include <stdlib.h>
55afa53c8dSMike Smith #include <termios.h>
56aa9caaf6SPeter Wemm 
574b88c807SRodney W. Grimes #include "shell.h"
584b88c807SRodney W. Grimes #include "options.h"
594b88c807SRodney W. Grimes #include "var.h"
604b88c807SRodney W. Grimes #include "output.h"
614b88c807SRodney W. Grimes #include "memalloc.h"
624b88c807SRodney W. Grimes #include "error.h"
634b88c807SRodney W. Grimes #include "mystring.h"
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes #undef eflag
664b88c807SRodney W. Grimes 
6779ea0bd9SStefan Farfeleder int readcmd(int, char **);
6879ea0bd9SStefan Farfeleder int umaskcmd(int, char **);
6979ea0bd9SStefan Farfeleder int ulimitcmd(int, char **);
7079ea0bd9SStefan Farfeleder 
714b88c807SRodney W. Grimes /*
728f0561ccSThomas Gellekum  * The read builtin.  The -r option causes backslashes to be treated like
738f0561ccSThomas Gellekum  * ordinary characters.
744b88c807SRodney W. Grimes  *
754b88c807SRodney W. Grimes  * This uses unbuffered input, which may be avoidable in some cases.
76b6748ec2SStefan Farfeleder  *
77b6748ec2SStefan Farfeleder  * Note that if IFS=' :' then read x y should work so that:
78b6748ec2SStefan Farfeleder  * 'a b'	x='a', y='b'
79b6748ec2SStefan Farfeleder  * ' a b '	x='a', y='b'
80b6748ec2SStefan Farfeleder  * ':b'		x='',  y='b'
81b6748ec2SStefan Farfeleder  * ':'		x='',  y=''
82b6748ec2SStefan Farfeleder  * '::'		x='',  y=''
83b6748ec2SStefan Farfeleder  * ': :'	x='',  y=''
84b6748ec2SStefan Farfeleder  * ':::'	x='',  y='::'
85b6748ec2SStefan Farfeleder  * ':b c:'	x='',  y='b c:'
864b88c807SRodney W. Grimes  */
874b88c807SRodney W. Grimes 
88aa9caaf6SPeter Wemm int
895134c3f7SWarner Losh readcmd(int argc __unused, char **argv __unused)
90aa9caaf6SPeter Wemm {
914b88c807SRodney W. Grimes 	char **ap;
924b88c807SRodney W. Grimes 	int backslash;
934b88c807SRodney W. Grimes 	char c;
948f0561ccSThomas Gellekum 	int rflag;
954b88c807SRodney W. Grimes 	char *prompt;
964b88c807SRodney W. Grimes 	char *ifs;
974b88c807SRodney W. Grimes 	char *p;
984b88c807SRodney W. Grimes 	int startword;
994b88c807SRodney W. Grimes 	int status;
1004b88c807SRodney W. Grimes 	int i;
101b6748ec2SStefan Farfeleder 	int is_ifs;
102b6748ec2SStefan Farfeleder 	int saveall = 0;
103afa53c8dSMike Smith 	struct timeval tv;
104afa53c8dSMike Smith 	char *tvptr;
105afa53c8dSMike Smith 	fd_set ifds;
106afa53c8dSMike Smith 	struct termios told, tnew;
107afa53c8dSMike Smith 	int tsaved;
1084b88c807SRodney W. Grimes 
1098f0561ccSThomas Gellekum 	rflag = 0;
1104b88c807SRodney W. Grimes 	prompt = NULL;
111afa53c8dSMike Smith 	tv.tv_sec = -1;
112afa53c8dSMike Smith 	tv.tv_usec = 0;
1138f0561ccSThomas Gellekum 	while ((i = nextopt("erp:t:")) != '\0') {
114afa53c8dSMike Smith 		switch(i) {
115afa53c8dSMike Smith 		case 'p':
116f01e3d0cSMartin Cracauer 			prompt = shoptarg;
117afa53c8dSMike Smith 			break;
118afa53c8dSMike Smith 		case 'e':
1198f0561ccSThomas Gellekum 			break;
1208f0561ccSThomas Gellekum 		case 'r':
1218f0561ccSThomas Gellekum 			rflag = 1;
122afa53c8dSMike Smith 			break;
123afa53c8dSMike Smith 		case 't':
124f01e3d0cSMartin Cracauer 			tv.tv_sec = strtol(shoptarg, &tvptr, 0);
125f01e3d0cSMartin Cracauer 			if (tvptr == shoptarg)
126afa53c8dSMike Smith 				error("timeout value");
127afa53c8dSMike Smith 			switch(*tvptr) {
128afa53c8dSMike Smith 			case 0:
129afa53c8dSMike Smith 			case 's':
130afa53c8dSMike Smith 				break;
131afa53c8dSMike Smith 			case 'h':
132afa53c8dSMike Smith 				tv.tv_sec *= 60;
133afa53c8dSMike Smith 				/* FALLTHROUGH */
134afa53c8dSMike Smith 			case 'm':
135afa53c8dSMike Smith 				tv.tv_sec *= 60;
136afa53c8dSMike Smith 				break;
137afa53c8dSMike Smith 			default:
138afa53c8dSMike Smith 				error("timeout unit");
139afa53c8dSMike Smith 			}
140afa53c8dSMike Smith 			break;
141afa53c8dSMike Smith 		}
1424b88c807SRodney W. Grimes 	}
1434b88c807SRodney W. Grimes 	if (prompt && isatty(0)) {
1444b88c807SRodney W. Grimes 		out2str(prompt);
1454b88c807SRodney W. Grimes 		flushall();
1464b88c807SRodney W. Grimes 	}
1474b88c807SRodney W. Grimes 	if (*(ap = argptr) == NULL)
1484b88c807SRodney W. Grimes 		error("arg count");
1494b88c807SRodney W. Grimes 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
15086d8da5dSStefan Farfeleder 		ifs = " \t\n";
151afa53c8dSMike Smith 
152afa53c8dSMike Smith 	if (tv.tv_sec >= 0) {
153afa53c8dSMike Smith 		/*
154afa53c8dSMike Smith 		 * See if we can disable input processing; this will
155afa53c8dSMike Smith 		 * not give the desired result if we are in a pipeline
156afa53c8dSMike Smith 		 * and someone upstream is still in line-by-line mode.
157afa53c8dSMike Smith 		 */
158afa53c8dSMike Smith 		tsaved = 0;
159afa53c8dSMike Smith 		if (tcgetattr(0, &told) == 0) {
160afa53c8dSMike Smith 			memcpy(&tnew, &told, sizeof(told));
161afa53c8dSMike Smith 			cfmakeraw(&tnew);
162ae46d958SEd Schouten 			tnew.c_iflag |= told.c_iflag & ICRNL;
163afa53c8dSMike Smith 			tcsetattr(0, TCSANOW, &tnew);
164afa53c8dSMike Smith 			tsaved = 1;
165afa53c8dSMike Smith 		}
166afa53c8dSMike Smith 		/*
167afa53c8dSMike Smith 		 * Wait for something to become available.
168afa53c8dSMike Smith 		 */
169afa53c8dSMike Smith 		FD_ZERO(&ifds);
170afa53c8dSMike Smith 		FD_SET(0, &ifds);
171afa53c8dSMike Smith 		status = select(1, &ifds, NULL, NULL, &tv);
172afa53c8dSMike Smith 		if (tsaved)
173afa53c8dSMike Smith 			tcsetattr(0, TCSANOW, &told);
174afa53c8dSMike Smith 		/*
175afa53c8dSMike Smith 		 * If there's nothing ready, return an error.
176afa53c8dSMike Smith 		 */
177afa53c8dSMike Smith 		if (status <= 0)
178afa53c8dSMike Smith 			return(1);
179afa53c8dSMike Smith 	}
180afa53c8dSMike Smith 
1814b88c807SRodney W. Grimes 	status = 0;
182b6748ec2SStefan Farfeleder 	startword = 2;
1834b88c807SRodney W. Grimes 	backslash = 0;
1844b88c807SRodney W. Grimes 	STARTSTACKSTR(p);
1854b88c807SRodney W. Grimes 	for (;;) {
186e1b4d8d0SSheldon Hearn 		if (read(STDIN_FILENO, &c, 1) != 1) {
1874b88c807SRodney W. Grimes 			status = 1;
1884b88c807SRodney W. Grimes 			break;
1894b88c807SRodney W. Grimes 		}
1904b88c807SRodney W. Grimes 		if (c == '\0')
1914b88c807SRodney W. Grimes 			continue;
1924b88c807SRodney W. Grimes 		if (backslash) {
1934b88c807SRodney W. Grimes 			backslash = 0;
1944b88c807SRodney W. Grimes 			if (c != '\n')
1954b88c807SRodney W. Grimes 				STPUTC(c, p);
1964b88c807SRodney W. Grimes 			continue;
1974b88c807SRodney W. Grimes 		}
1988f0561ccSThomas Gellekum 		if (!rflag && c == '\\') {
1994b88c807SRodney W. Grimes 			backslash++;
2004b88c807SRodney W. Grimes 			continue;
2014b88c807SRodney W. Grimes 		}
2024b88c807SRodney W. Grimes 		if (c == '\n')
2034b88c807SRodney W. Grimes 			break;
204b6748ec2SStefan Farfeleder 		if (strchr(ifs, c))
205b6748ec2SStefan Farfeleder 			is_ifs = strchr(" \t\n", c) ? 1 : 2;
206b6748ec2SStefan Farfeleder 		else
207b6748ec2SStefan Farfeleder 			is_ifs = 0;
208b6748ec2SStefan Farfeleder 
209b6748ec2SStefan Farfeleder 		if (startword != 0) {
210b6748ec2SStefan Farfeleder 			if (is_ifs == 1) {
211b6748ec2SStefan Farfeleder 				/* Ignore leading IFS whitespace */
212b6748ec2SStefan Farfeleder 				if (saveall)
213b6748ec2SStefan Farfeleder 					STPUTC(c, p);
2144b88c807SRodney W. Grimes 				continue;
2154b88c807SRodney W. Grimes 			}
216b6748ec2SStefan Farfeleder 			if (is_ifs == 2 && startword == 1) {
217b6748ec2SStefan Farfeleder 				/* Only one non-whitespace IFS per word */
218b6748ec2SStefan Farfeleder 				startword = 2;
219b6748ec2SStefan Farfeleder 				if (saveall)
220b6748ec2SStefan Farfeleder 					STPUTC(c, p);
221b6748ec2SStefan Farfeleder 				continue;
222b6748ec2SStefan Farfeleder 			}
223b6748ec2SStefan Farfeleder 		}
224b6748ec2SStefan Farfeleder 
225b6748ec2SStefan Farfeleder 		if (is_ifs == 0) {
226b6748ec2SStefan Farfeleder 			/* append this character to the current variable */
2274b88c807SRodney W. Grimes 			startword = 0;
228b6748ec2SStefan Farfeleder 			if (saveall)
229b6748ec2SStefan Farfeleder 				/* Not just a spare terminator */
230b6748ec2SStefan Farfeleder 				saveall++;
231b6748ec2SStefan Farfeleder 			STPUTC(c, p);
232b6748ec2SStefan Farfeleder 			continue;
233b6748ec2SStefan Farfeleder 		}
234b6748ec2SStefan Farfeleder 
235b6748ec2SStefan Farfeleder 		/* end of variable... */
236b6748ec2SStefan Farfeleder 		startword = is_ifs;
237b6748ec2SStefan Farfeleder 
238b6748ec2SStefan Farfeleder 		if (ap[1] == NULL) {
239b6748ec2SStefan Farfeleder 			/* Last variable needs all IFS chars */
240b6748ec2SStefan Farfeleder 			saveall++;
241b6748ec2SStefan Farfeleder 			STPUTC(c, p);
242b6748ec2SStefan Farfeleder 			continue;
243b6748ec2SStefan Farfeleder 		}
244b6748ec2SStefan Farfeleder 
2454b88c807SRodney W. Grimes 		STACKSTRNUL(p);
2464b88c807SRodney W. Grimes 		setvar(*ap, stackblock(), 0);
2474b88c807SRodney W. Grimes 		ap++;
2484b88c807SRodney W. Grimes 		STARTSTACKSTR(p);
2494b88c807SRodney W. Grimes 	}
2504b88c807SRodney W. Grimes 	STACKSTRNUL(p);
251b6748ec2SStefan Farfeleder 
252b6748ec2SStefan Farfeleder 	/* Remove trailing IFS chars */
253b6748ec2SStefan Farfeleder 	for (; stackblock() <= --p; *p = 0) {
254b6748ec2SStefan Farfeleder 		if (!strchr(ifs, *p))
255b6748ec2SStefan Farfeleder 			break;
256b6748ec2SStefan Farfeleder 		if (strchr(" \t\n", *p))
257b6748ec2SStefan Farfeleder 			/* Always remove whitespace */
258b6748ec2SStefan Farfeleder 			continue;
259b6748ec2SStefan Farfeleder 		if (saveall > 1)
260b6748ec2SStefan Farfeleder 			/* Don't remove non-whitespace unless it was naked */
261b6748ec2SStefan Farfeleder 			break;
262b6748ec2SStefan Farfeleder 	}
2634b88c807SRodney W. Grimes 	setvar(*ap, stackblock(), 0);
264b6748ec2SStefan Farfeleder 
265b6748ec2SStefan Farfeleder 	/* Set any remaining args to "" */
2664b88c807SRodney W. Grimes 	while (*++ap != NULL)
2674b88c807SRodney W. Grimes 		setvar(*ap, nullstr, 0);
2684b88c807SRodney W. Grimes 	return status;
2694b88c807SRodney W. Grimes }
2704b88c807SRodney W. Grimes 
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes 
273aa9caaf6SPeter Wemm int
2745134c3f7SWarner Losh umaskcmd(int argc __unused, char **argv)
275aa9caaf6SPeter Wemm {
276aa9caaf6SPeter Wemm 	char *ap;
2774b88c807SRodney W. Grimes 	int mask;
2784b88c807SRodney W. Grimes 	int i;
279aa9caaf6SPeter Wemm 	int symbolic_mode = 0;
2804b88c807SRodney W. Grimes 
281aa9caaf6SPeter Wemm 	while ((i = nextopt("S")) != '\0') {
282aa9caaf6SPeter Wemm 		symbolic_mode = 1;
283aa9caaf6SPeter Wemm 	}
284aa9caaf6SPeter Wemm 
2854b88c807SRodney W. Grimes 	INTOFF;
2864b88c807SRodney W. Grimes 	mask = umask(0);
2874b88c807SRodney W. Grimes 	umask(mask);
2884b88c807SRodney W. Grimes 	INTON;
289aa9caaf6SPeter Wemm 
290aa9caaf6SPeter Wemm 	if ((ap = *argptr) == NULL) {
291aa9caaf6SPeter Wemm 		if (symbolic_mode) {
292aa9caaf6SPeter Wemm 			char u[4], g[4], o[4];
293aa9caaf6SPeter Wemm 
294aa9caaf6SPeter Wemm 			i = 0;
295aa9caaf6SPeter Wemm 			if ((mask & S_IRUSR) == 0)
296aa9caaf6SPeter Wemm 				u[i++] = 'r';
297aa9caaf6SPeter Wemm 			if ((mask & S_IWUSR) == 0)
298aa9caaf6SPeter Wemm 				u[i++] = 'w';
299aa9caaf6SPeter Wemm 			if ((mask & S_IXUSR) == 0)
300aa9caaf6SPeter Wemm 				u[i++] = 'x';
301aa9caaf6SPeter Wemm 			u[i] = '\0';
302aa9caaf6SPeter Wemm 
303aa9caaf6SPeter Wemm 			i = 0;
304aa9caaf6SPeter Wemm 			if ((mask & S_IRGRP) == 0)
305aa9caaf6SPeter Wemm 				g[i++] = 'r';
306aa9caaf6SPeter Wemm 			if ((mask & S_IWGRP) == 0)
307aa9caaf6SPeter Wemm 				g[i++] = 'w';
308aa9caaf6SPeter Wemm 			if ((mask & S_IXGRP) == 0)
309aa9caaf6SPeter Wemm 				g[i++] = 'x';
310aa9caaf6SPeter Wemm 			g[i] = '\0';
311aa9caaf6SPeter Wemm 
312aa9caaf6SPeter Wemm 			i = 0;
313aa9caaf6SPeter Wemm 			if ((mask & S_IROTH) == 0)
314aa9caaf6SPeter Wemm 				o[i++] = 'r';
315aa9caaf6SPeter Wemm 			if ((mask & S_IWOTH) == 0)
316aa9caaf6SPeter Wemm 				o[i++] = 'w';
317aa9caaf6SPeter Wemm 			if ((mask & S_IXOTH) == 0)
318aa9caaf6SPeter Wemm 				o[i++] = 'x';
319aa9caaf6SPeter Wemm 			o[i] = '\0';
320aa9caaf6SPeter Wemm 
321aa9caaf6SPeter Wemm 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
3224b88c807SRodney W. Grimes 		} else {
323aa9caaf6SPeter Wemm 			out1fmt("%.4o\n", mask);
324aa9caaf6SPeter Wemm 		}
325aa9caaf6SPeter Wemm 	} else {
326aa9caaf6SPeter Wemm 		if (isdigit(*ap)) {
3274b88c807SRodney W. Grimes 			mask = 0;
3284b88c807SRodney W. Grimes 			do {
329aa9caaf6SPeter Wemm 				if (*ap >= '8' || *ap < '0')
330e4c880afSStefan Farfeleder 					error("Illegal number: %s", *argptr);
331aa9caaf6SPeter Wemm 				mask = (mask << 3) + (*ap - '0');
332aa9caaf6SPeter Wemm 			} while (*++ap != '\0');
3334b88c807SRodney W. Grimes 			umask(mask);
334aa9caaf6SPeter Wemm 		} else {
335aa9caaf6SPeter Wemm 			void *set;
336670528cdSStefan Farfeleder 			INTOFF;
337aa9caaf6SPeter Wemm 			if ((set = setmode (ap)) == 0)
338aa9caaf6SPeter Wemm 				error("Illegal number: %s", ap);
339aa9caaf6SPeter Wemm 
340aa9caaf6SPeter Wemm 			mask = getmode (set, ~mask & 0777);
341aa9caaf6SPeter Wemm 			umask(~mask & 0777);
342a207266eSWarner Losh 			free(set);
343670528cdSStefan Farfeleder 			INTON;
344aa9caaf6SPeter Wemm 		}
3454b88c807SRodney W. Grimes 	}
3464b88c807SRodney W. Grimes 	return 0;
3474b88c807SRodney W. Grimes }
3487a2afe64SJoerg Wunsch 
349aa9caaf6SPeter Wemm /*
350aa9caaf6SPeter Wemm  * ulimit builtin
351aa9caaf6SPeter Wemm  *
352aa9caaf6SPeter Wemm  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
353aa9caaf6SPeter Wemm  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
354aa9caaf6SPeter Wemm  * ash by J.T. Conklin.
355aa9caaf6SPeter Wemm  *
356aa9caaf6SPeter Wemm  * Public domain.
357aa9caaf6SPeter Wemm  */
3587a2afe64SJoerg Wunsch 
359aa9caaf6SPeter Wemm struct limits {
360aa9caaf6SPeter Wemm 	const char *name;
36116992ff4SPeter Wemm 	const char *units;
362aa9caaf6SPeter Wemm 	int	cmd;
363aa9caaf6SPeter Wemm 	int	factor;	/* multiply by to get rlim_{cur,max} values */
364aa9caaf6SPeter Wemm 	char	option;
3657a2afe64SJoerg Wunsch };
3667a2afe64SJoerg Wunsch 
367aa9caaf6SPeter Wemm static const struct limits limits[] = {
368aa9caaf6SPeter Wemm #ifdef RLIMIT_CPU
36916992ff4SPeter Wemm 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
370aa9caaf6SPeter Wemm #endif
371aa9caaf6SPeter Wemm #ifdef RLIMIT_FSIZE
37216992ff4SPeter Wemm 	{ "file size",		"512-blocks",	RLIMIT_FSIZE,	 512, 'f' },
373aa9caaf6SPeter Wemm #endif
374aa9caaf6SPeter Wemm #ifdef RLIMIT_DATA
37516992ff4SPeter Wemm 	{ "data seg size",	"kbytes",	RLIMIT_DATA,	1024, 'd' },
376aa9caaf6SPeter Wemm #endif
377aa9caaf6SPeter Wemm #ifdef RLIMIT_STACK
37816992ff4SPeter Wemm 	{ "stack size",		"kbytes",	RLIMIT_STACK,	1024, 's' },
379aa9caaf6SPeter Wemm #endif
380aa9caaf6SPeter Wemm #ifdef  RLIMIT_CORE
38116992ff4SPeter Wemm 	{ "core file size",	"512-blocks",	RLIMIT_CORE,	 512, 'c' },
382aa9caaf6SPeter Wemm #endif
383aa9caaf6SPeter Wemm #ifdef RLIMIT_RSS
38416992ff4SPeter Wemm 	{ "max memory size",	"kbytes",	RLIMIT_RSS,	1024, 'm' },
385aa9caaf6SPeter Wemm #endif
386aa9caaf6SPeter Wemm #ifdef RLIMIT_MEMLOCK
38716992ff4SPeter Wemm 	{ "locked memory",	"kbytes",	RLIMIT_MEMLOCK, 1024, 'l' },
388aa9caaf6SPeter Wemm #endif
389aa9caaf6SPeter Wemm #ifdef RLIMIT_NPROC
39016992ff4SPeter Wemm 	{ "max user processes",	(char *)0,	RLIMIT_NPROC,      1, 'u' },
391aa9caaf6SPeter Wemm #endif
392aa9caaf6SPeter Wemm #ifdef RLIMIT_NOFILE
39316992ff4SPeter Wemm 	{ "open files",		(char *)0,	RLIMIT_NOFILE,     1, 'n' },
394aa9caaf6SPeter Wemm #endif
395aa9caaf6SPeter Wemm #ifdef RLIMIT_VMEM
39616992ff4SPeter Wemm 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
397aa9caaf6SPeter Wemm #endif
398aa9caaf6SPeter Wemm #ifdef RLIMIT_SWAP
39916992ff4SPeter Wemm 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
400aa9caaf6SPeter Wemm #endif
4017cde774dSBrian Feldman #ifdef RLIMIT_SBSIZE
4027cde774dSBrian Feldman 	{ "sbsize",		"bytes",	RLIMIT_SBSIZE,	   1, 'b' },
4037cde774dSBrian Feldman #endif
404bc093719SEd Schouten #ifdef RLIMIT_NPTS
405bc093719SEd Schouten 	{ "pseudo-terminals",	(char *)0,	RLIMIT_NPTS,	   1, 'p' },
406bc093719SEd Schouten #endif
40716992ff4SPeter Wemm 	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
4087a2afe64SJoerg Wunsch };
4097a2afe64SJoerg Wunsch 
410aa9caaf6SPeter Wemm int
4115134c3f7SWarner Losh ulimitcmd(int argc __unused, char **argv __unused)
412aa9caaf6SPeter Wemm {
413afb033d5SSteve Price 	int	c;
4140c1661b7SMaxime Henrion 	rlim_t val = 0;
415aa9caaf6SPeter Wemm 	enum { SOFT = 0x1, HARD = 0x2 }
416aa9caaf6SPeter Wemm 			how = SOFT | HARD;
417aa9caaf6SPeter Wemm 	const struct limits	*l;
418aa9caaf6SPeter Wemm 	int		set, all = 0;
419aa9caaf6SPeter Wemm 	int		optc, what;
420aa9caaf6SPeter Wemm 	struct rlimit	limit;
4217a2afe64SJoerg Wunsch 
422aa9caaf6SPeter Wemm 	what = 'f';
423bc093719SEd Schouten 	while ((optc = nextopt("HSatfdsmcnuvlbp")) != '\0')
424aa9caaf6SPeter Wemm 		switch (optc) {
4257a2afe64SJoerg Wunsch 		case 'H':
426aa9caaf6SPeter Wemm 			how = HARD;
4277a2afe64SJoerg Wunsch 			break;
4287a2afe64SJoerg Wunsch 		case 'S':
429aa9caaf6SPeter Wemm 			how = SOFT;
4307a2afe64SJoerg Wunsch 			break;
4317a2afe64SJoerg Wunsch 		case 'a':
432aa9caaf6SPeter Wemm 			all = 1;
4337a2afe64SJoerg Wunsch 			break;
434aa9caaf6SPeter Wemm 		default:
435aa9caaf6SPeter Wemm 			what = optc;
4367a2afe64SJoerg Wunsch 		}
4377a2afe64SJoerg Wunsch 
438aa9caaf6SPeter Wemm 	for (l = limits; l->name && l->option != what; l++)
439aa9caaf6SPeter Wemm 		;
440aa9caaf6SPeter Wemm 	if (!l->name)
441abe2dc61STim J. Robbins 		error("internal error (%c)", what);
4427a2afe64SJoerg Wunsch 
443aa9caaf6SPeter Wemm 	set = *argptr ? 1 : 0;
444aa9caaf6SPeter Wemm 	if (set) {
445aa9caaf6SPeter Wemm 		char *p = *argptr;
446aa9caaf6SPeter Wemm 
447aa9caaf6SPeter Wemm 		if (all || argptr[1])
448abe2dc61STim J. Robbins 			error("too many arguments");
449aa9caaf6SPeter Wemm 		if (strcmp(p, "unlimited") == 0)
4507a2afe64SJoerg Wunsch 			val = RLIM_INFINITY;
4517a2afe64SJoerg Wunsch 		else {
4520c1661b7SMaxime Henrion 			val = 0;
453aa9caaf6SPeter Wemm 
454aa9caaf6SPeter Wemm 			while ((c = *p++) >= '0' && c <= '9')
455aa9caaf6SPeter Wemm 			{
456aa9caaf6SPeter Wemm 				val = (val * 10) + (long)(c - '0');
4570c1661b7SMaxime Henrion 				if (val < 0)
458aa9caaf6SPeter Wemm 					break;
4597a2afe64SJoerg Wunsch 			}
460aa9caaf6SPeter Wemm 			if (c)
461abe2dc61STim J. Robbins 				error("bad number");
462aa9caaf6SPeter Wemm 			val *= l->factor;
463aa9caaf6SPeter Wemm 		}
464aa9caaf6SPeter Wemm 	}
465aa9caaf6SPeter Wemm 	if (all) {
466aa9caaf6SPeter Wemm 		for (l = limits; l->name; l++) {
46716992ff4SPeter Wemm 			char optbuf[40];
46816992ff4SPeter Wemm 			if (getrlimit(l->cmd, &limit) < 0)
469abe2dc61STim J. Robbins 				error("can't get limit: %s", strerror(errno));
470aa9caaf6SPeter Wemm 			if (how & SOFT)
471aa9caaf6SPeter Wemm 				val = limit.rlim_cur;
472aa9caaf6SPeter Wemm 			else if (how & HARD)
473aa9caaf6SPeter Wemm 				val = limit.rlim_max;
474aa9caaf6SPeter Wemm 
47516992ff4SPeter Wemm 			if (l->units)
47616992ff4SPeter Wemm 				snprintf(optbuf, sizeof(optbuf),
4774e4e0959SPeter Wemm 					"(%s, -%c) ", l->units, l->option);
47816992ff4SPeter Wemm 			else
47916992ff4SPeter Wemm 				snprintf(optbuf, sizeof(optbuf),
4804e4e0959SPeter Wemm 					"(-%c) ", l->option);
4814e4e0959SPeter Wemm 			out1fmt("%-18s %18s ", l->name, optbuf);
482aa9caaf6SPeter Wemm 			if (val == RLIM_INFINITY)
483aa9caaf6SPeter Wemm 				out1fmt("unlimited\n");
484aa9caaf6SPeter Wemm 			else
485aa9caaf6SPeter Wemm 			{
486aa9caaf6SPeter Wemm 				val /= l->factor;
4870c1661b7SMaxime Henrion 				out1fmt("%jd\n", (intmax_t)val);
4887a2afe64SJoerg Wunsch 			}
4897a2afe64SJoerg Wunsch 		}
4907a2afe64SJoerg Wunsch 		return 0;
4917a2afe64SJoerg Wunsch 	}
492aa9caaf6SPeter Wemm 
49316992ff4SPeter Wemm 	if (getrlimit(l->cmd, &limit) < 0)
494abe2dc61STim J. Robbins 		error("can't get limit: %s", strerror(errno));
495aa9caaf6SPeter Wemm 	if (set) {
496aa9caaf6SPeter Wemm 		if (how & SOFT)
497aa9caaf6SPeter Wemm 			limit.rlim_cur = val;
498aa9caaf6SPeter Wemm 		if (how & HARD)
499aa9caaf6SPeter Wemm 			limit.rlim_max = val;
500aa9caaf6SPeter Wemm 		if (setrlimit(l->cmd, &limit) < 0)
501abe2dc61STim J. Robbins 			error("bad limit: %s", strerror(errno));
502aa9caaf6SPeter Wemm 	} else {
503aa9caaf6SPeter Wemm 		if (how & SOFT)
504aa9caaf6SPeter Wemm 			val = limit.rlim_cur;
505aa9caaf6SPeter Wemm 		else if (how & HARD)
506aa9caaf6SPeter Wemm 			val = limit.rlim_max;
507aa9caaf6SPeter Wemm 
508aa9caaf6SPeter Wemm 		if (val == RLIM_INFINITY)
509aa9caaf6SPeter Wemm 			out1fmt("unlimited\n");
510aa9caaf6SPeter Wemm 		else
511aa9caaf6SPeter Wemm 		{
512aa9caaf6SPeter Wemm 			val /= l->factor;
5130c1661b7SMaxime Henrion 			out1fmt("%jd\n", (intmax_t)val);
514aa9caaf6SPeter Wemm 		}
515aa9caaf6SPeter Wemm 	}
516aa9caaf6SPeter Wemm 	return 0;
517aa9caaf6SPeter Wemm }
518