Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.18
      1 /*	$NetBSD: miscbltin.c,v 1.18 1997/04/11 23:08:15 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
     42 #else
     43 static char rcsid[] = "$NetBSD: miscbltin.c,v 1.18 1997/04/11 23:08:15 christos Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 /*
     48  * Miscelaneous builtins.
     49  */
     50 
     51 #include <sys/types.h>
     52 #include <sys/param.h>
     53 #include <sys/stat.h>
     54 #include <sys/time.h>
     55 #include <sys/resource.h>
     56 #include <unistd.h>
     57 #include <ctype.h>
     58 
     59 #include "shell.h"
     60 #include "options.h"
     61 #include "var.h"
     62 #include "output.h"
     63 #include "memalloc.h"
     64 #include "error.h"
     65 #include "mystring.h"
     66 
     67 #undef eflag
     68 
     69 extern char **argptr;		/* argument list for builtin command */
     70 
     71 
     72 /*
     73  * The read builtin.  The -e option causes backslashes to escape the
     74  * following character.
     75  *
     76  * This uses unbuffered input, which may be avoidable in some cases.
     77  */
     78 
     79 int
     80 readcmd(argc, argv)
     81 	int argc;
     82 	char **argv;
     83 {
     84 	char **ap;
     85 	int backslash;
     86 	char c;
     87 	int eflag;
     88 	char *prompt;
     89 	char *ifs;
     90 	char *p;
     91 	int startword;
     92 	int status;
     93 	int i;
     94 
     95 	eflag = 0;
     96 	prompt = NULL;
     97 	while ((i = nextopt("ep:")) != '\0') {
     98 		if (i == 'p')
     99 			prompt = optarg;
    100 		else
    101 			eflag = 1;
    102 	}
    103 	if (prompt && isatty(0)) {
    104 		out2str(prompt);
    105 		flushall();
    106 	}
    107 	if (*(ap = argptr) == NULL)
    108 		error("arg count");
    109 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
    110 		ifs = nullstr;
    111 	status = 0;
    112 	startword = 1;
    113 	backslash = 0;
    114 	STARTSTACKSTR(p);
    115 	for (;;) {
    116 		if (read(0, &c, 1) != 1) {
    117 			status = 1;
    118 			break;
    119 		}
    120 		if (c == '\0')
    121 			continue;
    122 		if (backslash) {
    123 			backslash = 0;
    124 			if (c != '\n')
    125 				STPUTC(c, p);
    126 			continue;
    127 		}
    128 		if (eflag && c == '\\') {
    129 			backslash++;
    130 			continue;
    131 		}
    132 		if (c == '\n')
    133 			break;
    134 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
    135 			continue;
    136 		}
    137 		startword = 0;
    138 		if (backslash && c == '\\') {
    139 			if (read(0, &c, 1) != 1) {
    140 				status = 1;
    141 				break;
    142 			}
    143 			STPUTC(c, p);
    144 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
    145 			STACKSTRNUL(p);
    146 			setvar(*ap, stackblock(), 0);
    147 			ap++;
    148 			startword = 1;
    149 			STARTSTACKSTR(p);
    150 		} else {
    151 			STPUTC(c, p);
    152 		}
    153 	}
    154 	STACKSTRNUL(p);
    155 	setvar(*ap, stackblock(), 0);
    156 	while (*++ap != NULL)
    157 		setvar(*ap, nullstr, 0);
    158 	return status;
    159 }
    160 
    161 
    162 
    163 int
    164 umaskcmd(argc, argv)
    165 	int argc;
    166 	char **argv;
    167 {
    168 	char *ap;
    169 	int mask;
    170 	int i;
    171 	int symbolic_mode = 0;
    172 
    173 	while ((i = nextopt("S")) != '\0') {
    174 		symbolic_mode = 1;
    175 	}
    176 
    177 	INTOFF;
    178 	mask = umask(0);
    179 	umask(mask);
    180 	INTON;
    181 
    182 	if ((ap = *argptr) == NULL) {
    183 		if (symbolic_mode) {
    184 			char u[4], g[4], o[4];
    185 
    186 			i = 0;
    187 			if ((mask & S_IRUSR) == 0)
    188 				u[i++] = 'r';
    189 			if ((mask & S_IWUSR) == 0)
    190 				u[i++] = 'w';
    191 			if ((mask & S_IXUSR) == 0)
    192 				u[i++] = 'x';
    193 			u[i] = '\0';
    194 
    195 			i = 0;
    196 			if ((mask & S_IRGRP) == 0)
    197 				g[i++] = 'r';
    198 			if ((mask & S_IWGRP) == 0)
    199 				g[i++] = 'w';
    200 			if ((mask & S_IXGRP) == 0)
    201 				g[i++] = 'x';
    202 			g[i] = '\0';
    203 
    204 			i = 0;
    205 			if ((mask & S_IROTH) == 0)
    206 				o[i++] = 'r';
    207 			if ((mask & S_IWOTH) == 0)
    208 				o[i++] = 'w';
    209 			if ((mask & S_IXOTH) == 0)
    210 				o[i++] = 'x';
    211 			o[i] = '\0';
    212 
    213 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
    214 		} else {
    215 			out1fmt("%.4o\n", mask);
    216 		}
    217 	} else {
    218 		if (isdigit(*ap)) {
    219 			mask = 0;
    220 			do {
    221 				if (*ap >= '8' || *ap < '0')
    222 					error("Illegal number: %s", argv[1]);
    223 				mask = (mask << 3) + (*ap - '0');
    224 			} while (*++ap != '\0');
    225 			umask(mask);
    226 		} else {
    227 			void *set;
    228 			if ((set = setmode (ap)) == 0)
    229 					error("Illegal number: %s", ap);
    230 
    231 			mask = getmode (set, ~mask & 0777);
    232 			umask(~mask & 0777);
    233 		}
    234 	}
    235 	return 0;
    236 }
    237 
    238 /*
    239  * ulimit builtin
    240  *
    241  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
    242  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
    243  * ash by J.T. Conklin.
    244  *
    245  * Public domain.
    246  */
    247 
    248 struct limits {
    249 	const char *name;
    250 	int	cmd;
    251 	int	factor;	/* multiply by to get rlim_{cur,max} values */
    252 	char	option;
    253 };
    254 
    255 static const struct limits limits[] = {
    256 #ifdef RLIMIT_CPU
    257 	{ "time(seconds)",		RLIMIT_CPU,	   1, 't' },
    258 #endif
    259 #ifdef RLIMIT_FSIZE
    260 	{ "file(blocks)",		RLIMIT_FSIZE,	 512, 'f' },
    261 #endif
    262 #ifdef RLIMIT_DATA
    263 	{ "data(kbytes)",		RLIMIT_DATA,	1024, 'd' },
    264 #endif
    265 #ifdef RLIMIT_STACK
    266 	{ "stack(kbytes)",		RLIMIT_STACK,	1024, 's' },
    267 #endif
    268 #ifdef  RLIMIT_CORE
    269 	{ "coredump(blocks)",		RLIMIT_CORE,	 512, 'c' },
    270 #endif
    271 #ifdef RLIMIT_RSS
    272 	{ "memory(kbytes)",		RLIMIT_RSS,	1024, 'm' },
    273 #endif
    274 #ifdef RLIMIT_MEMLOCK
    275 	{ "locked memory(kbytes)",	RLIMIT_MEMLOCK, 1024, 'l' },
    276 #endif
    277 #ifdef RLIMIT_NPROC
    278 	{ "process(processes)",		RLIMIT_NPROC,      1, 'p' },
    279 #endif
    280 #ifdef RLIMIT_NOFILE
    281 	{ "nofiles(descriptors)",	RLIMIT_NOFILE,     1, 'n' },
    282 #endif
    283 #ifdef RLIMIT_VMEM
    284 	{ "vmemory(kbytes)",		RLIMIT_VMEM,	1024, 'v' },
    285 #endif
    286 #ifdef RLIMIT_SWAP
    287 	{ "swap(kbytes)",		RLIMIT_SWAP,	1024, 'w' },
    288 #endif
    289 	{ (char *) 0,			0,		   0,  '\0' }
    290 };
    291 
    292 int
    293 ulimitcmd(argc, argv)
    294 	int argc;
    295 	char **argv;
    296 {
    297 	int	c;
    298 	rlim_t val;
    299 	enum { SOFT = 0x1, HARD = 0x2 }
    300 			how = SOFT | HARD;
    301 	const struct limits	*l;
    302 	int		set, all = 0;
    303 	int		optc, what;
    304 	struct rlimit	limit;
    305 
    306 	what = 'f';
    307 	while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
    308 		switch (optc) {
    309 		case 'H':
    310 			how = HARD;
    311 			break;
    312 		case 'S':
    313 			how = SOFT;
    314 			break;
    315 		case 'a':
    316 			all = 1;
    317 			break;
    318 		default:
    319 			what = optc;
    320 		}
    321 
    322 	for (l = limits; l->name && l->option != what; l++)
    323 		;
    324 	if (!l->name)
    325 		error("ulimit: internal error (%c)\n", what);
    326 
    327 	set = *argptr ? 1 : 0;
    328 	if (set) {
    329 		char *p = *argptr;
    330 
    331 		if (all || argptr[1])
    332 			error("ulimit: too many arguments\n");
    333 		if (strcmp(p, "unlimited") == 0)
    334 			val = RLIM_INFINITY;
    335 		else {
    336 			val = (rlim_t) 0;
    337 
    338 			while ((c = *p++) >= '0' && c <= '9')
    339 			{
    340 				val = (val * 10) + (long)(c - '0');
    341 				if (val < (rlim_t) 0)
    342 					break;
    343 			}
    344 			if (c)
    345 				error("ulimit: bad number\n");
    346 			val *= l->factor;
    347 		}
    348 	}
    349 	if (all) {
    350 		for (l = limits; l->name; l++) {
    351 			getrlimit(l->cmd, &limit);
    352 			if (how & SOFT)
    353 				val = limit.rlim_cur;
    354 			else if (how & HARD)
    355 				val = limit.rlim_max;
    356 
    357 			out1fmt("%-20s ", l->name);
    358 			if (val == RLIM_INFINITY)
    359 				out1fmt("unlimited\n");
    360 			else
    361 			{
    362 				val /= l->factor;
    363 #ifdef BSD4_4
    364 				out1fmt("%qd\n", (quad_t) val);
    365 #else
    366 				out1fmt("%ld\n", (long) val);
    367 #endif
    368 			}
    369 		}
    370 		return 0;
    371 	}
    372 
    373 	getrlimit(l->cmd, &limit);
    374 	if (set) {
    375 		if (how & SOFT)
    376 			limit.rlim_cur = val;
    377 		if (how & HARD)
    378 			limit.rlim_max = val;
    379 		if (setrlimit(l->cmd, &limit) < 0)
    380 			error("ulimit: bad limit\n");
    381 	} else {
    382 		if (how & SOFT)
    383 			val = limit.rlim_cur;
    384 		else if (how & HARD)
    385 			val = limit.rlim_max;
    386 
    387 		if (val == RLIM_INFINITY)
    388 			out1fmt("unlimited\n");
    389 		else
    390 		{
    391 			val /= l->factor;
    392 #ifdef BSD4_4
    393 			out1fmt("%qd\n", (quad_t) val);
    394 #else
    395 			out1fmt("%ld\n", (long) val);
    396 #endif
    397 		}
    398 	}
    399 	return 0;
    400 }
    401