Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.31
      1 /*	$NetBSD: miscbltin.c,v 1.31 2002/11/24 22:35:41 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 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: miscbltin.c,v 1.31 2002/11/24 22:35:41 christos Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 /*
     49  * Miscelaneous builtins.
     50  */
     51 
     52 #include <sys/types.h>		/* quad_t */
     53 #include <sys/param.h>		/* BSD4_4 */
     54 #include <sys/stat.h>
     55 #include <sys/time.h>
     56 #include <sys/resource.h>
     57 #include <unistd.h>
     58 #include <stdlib.h>
     59 #include <ctype.h>
     60 #include <errno.h>
     61 
     62 #include "shell.h"
     63 #include "options.h"
     64 #include "var.h"
     65 #include "output.h"
     66 #include "memalloc.h"
     67 #include "error.h"
     68 #include "miscbltin.h"
     69 #include "mystring.h"
     70 
     71 #undef rflag
     72 
     73 
     74 
     75 /*
     76  * The read builtin.  The -e option causes backslashes to escape the
     77  * following character.
     78  *
     79  * This uses unbuffered input, which may be avoidable in some cases.
     80  */
     81 
     82 int
     83 readcmd(int argc, char **argv)
     84 {
     85 	char **ap;
     86 	int backslash;
     87 	char c;
     88 	int rflag;
     89 	char *prompt;
     90 	char *ifs;
     91 	char *p;
     92 	int startword;
     93 	int status;
     94 	int i;
     95 
     96 	rflag = 0;
     97 	prompt = NULL;
     98 	while ((i = nextopt("p:r")) != '\0') {
     99 		if (i == 'p')
    100 			prompt = optionarg;
    101 		else
    102 			rflag = 1;
    103 	}
    104 	if (prompt && isatty(0)) {
    105 		out2str(prompt);
    106 		flushall();
    107 	}
    108 	if (*(ap = argptr) == NULL)
    109 		error("arg count");
    110 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
    111 		ifs = nullstr;
    112 	status = 0;
    113 	startword = 1;
    114 	backslash = 0;
    115 	STARTSTACKSTR(p);
    116 	for (;;) {
    117 		if (read(0, &c, 1) != 1) {
    118 			status = 1;
    119 			break;
    120 		}
    121 		if (c == '\0')
    122 			continue;
    123 		if (backslash) {
    124 			backslash = 0;
    125 			if (c != '\n')
    126 				STPUTC(c, p);
    127 			continue;
    128 		}
    129 		if (!rflag && c == '\\') {
    130 			backslash++;
    131 			continue;
    132 		}
    133 		if (c == '\n')
    134 			break;
    135 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
    136 			continue;
    137 		}
    138 		startword = 0;
    139 		if (backslash && c == '\\') {
    140 			if (read(0, &c, 1) != 1) {
    141 				status = 1;
    142 				break;
    143 			}
    144 			STPUTC(c, p);
    145 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
    146 			STACKSTRNUL(p);
    147 			setvar(*ap, stackblock(), 0);
    148 			ap++;
    149 			startword = 1;
    150 			STARTSTACKSTR(p);
    151 		} else {
    152 			STPUTC(c, p);
    153 		}
    154 	}
    155 	STACKSTRNUL(p);
    156 	/* Remove trailing blanks */
    157 	while (stackblock() <= --p && strchr(ifs, *p) != NULL)
    158 		*p = '\0';
    159 	setvar(*ap, stackblock(), 0);
    160 	while (*++ap != NULL)
    161 		setvar(*ap, nullstr, 0);
    162 	return status;
    163 }
    164 
    165 
    166 
    167 int
    168 umaskcmd(int argc, char **argv)
    169 {
    170 	char *ap;
    171 	int mask;
    172 	int i;
    173 	int symbolic_mode = 0;
    174 
    175 	while ((i = nextopt("S")) != '\0') {
    176 		symbolic_mode = 1;
    177 	}
    178 
    179 	INTOFF;
    180 	mask = umask(0);
    181 	umask(mask);
    182 	INTON;
    183 
    184 	if ((ap = *argptr) == NULL) {
    185 		if (symbolic_mode) {
    186 			char u[4], g[4], o[4];
    187 
    188 			i = 0;
    189 			if ((mask & S_IRUSR) == 0)
    190 				u[i++] = 'r';
    191 			if ((mask & S_IWUSR) == 0)
    192 				u[i++] = 'w';
    193 			if ((mask & S_IXUSR) == 0)
    194 				u[i++] = 'x';
    195 			u[i] = '\0';
    196 
    197 			i = 0;
    198 			if ((mask & S_IRGRP) == 0)
    199 				g[i++] = 'r';
    200 			if ((mask & S_IWGRP) == 0)
    201 				g[i++] = 'w';
    202 			if ((mask & S_IXGRP) == 0)
    203 				g[i++] = 'x';
    204 			g[i] = '\0';
    205 
    206 			i = 0;
    207 			if ((mask & S_IROTH) == 0)
    208 				o[i++] = 'r';
    209 			if ((mask & S_IWOTH) == 0)
    210 				o[i++] = 'w';
    211 			if ((mask & S_IXOTH) == 0)
    212 				o[i++] = 'x';
    213 			o[i] = '\0';
    214 
    215 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
    216 		} else {
    217 			out1fmt("%.4o\n", mask);
    218 		}
    219 	} else {
    220 		if (isdigit((unsigned char)*ap)) {
    221 			mask = 0;
    222 			do {
    223 				if (*ap >= '8' || *ap < '0')
    224 					error("Illegal number: %s", argv[1]);
    225 				mask = (mask << 3) + (*ap - '0');
    226 			} while (*++ap != '\0');
    227 			umask(mask);
    228 		} else {
    229 			void *set;
    230 
    231 			INTOFF;
    232 			if ((set = setmode(ap)) != 0) {
    233 				mask = getmode(set, ~mask & 0777);
    234 				ckfree(set);
    235 			}
    236 			INTON;
    237 			if (!set)
    238 				error("Illegal mode: %s", ap);
    239 
    240 			umask(~mask & 0777);
    241 		}
    242 	}
    243 	return 0;
    244 }
    245 
    246 /*
    247  * ulimit builtin
    248  *
    249  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
    250  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
    251  * ash by J.T. Conklin.
    252  *
    253  * Public domain.
    254  */
    255 
    256 struct limits {
    257 	const char *name;
    258 	int	cmd;
    259 	int	factor;	/* multiply by to get rlim_{cur,max} values */
    260 	char	option;
    261 };
    262 
    263 static const struct limits limits[] = {
    264 #ifdef RLIMIT_CPU
    265 	{ "time(seconds)",		RLIMIT_CPU,	   1, 't' },
    266 #endif
    267 #ifdef RLIMIT_FSIZE
    268 	{ "file(blocks)",		RLIMIT_FSIZE,	 512, 'f' },
    269 #endif
    270 #ifdef RLIMIT_DATA
    271 	{ "data(kbytes)",		RLIMIT_DATA,	1024, 'd' },
    272 #endif
    273 #ifdef RLIMIT_STACK
    274 	{ "stack(kbytes)",		RLIMIT_STACK,	1024, 's' },
    275 #endif
    276 #ifdef  RLIMIT_CORE
    277 	{ "coredump(blocks)",		RLIMIT_CORE,	 512, 'c' },
    278 #endif
    279 #ifdef RLIMIT_RSS
    280 	{ "memory(kbytes)",		RLIMIT_RSS,	1024, 'm' },
    281 #endif
    282 #ifdef RLIMIT_MEMLOCK
    283 	{ "locked memory(kbytes)",	RLIMIT_MEMLOCK, 1024, 'l' },
    284 #endif
    285 #ifdef RLIMIT_NPROC
    286 	{ "process(processes)",		RLIMIT_NPROC,      1, 'p' },
    287 #endif
    288 #ifdef RLIMIT_NOFILE
    289 	{ "nofiles(descriptors)",	RLIMIT_NOFILE,     1, 'n' },
    290 #endif
    291 #ifdef RLIMIT_VMEM
    292 	{ "vmemory(kbytes)",		RLIMIT_VMEM,	1024, 'v' },
    293 #endif
    294 #ifdef RLIMIT_SWAP
    295 	{ "swap(kbytes)",		RLIMIT_SWAP,	1024, 'w' },
    296 #endif
    297 	{ (char *) 0,			0,		   0,  '\0' }
    298 };
    299 
    300 int
    301 ulimitcmd(int argc, char **argv)
    302 {
    303 	int	c;
    304 	rlim_t val = 0;
    305 	enum { SOFT = 0x1, HARD = 0x2 }
    306 			how = SOFT | HARD;
    307 	const struct limits	*l;
    308 	int		set, all = 0;
    309 	int		optc, what;
    310 	struct rlimit	limit;
    311 
    312 	what = 'f';
    313 	while ((optc = nextopt("HSatfdsmcnpl")) != '\0')
    314 		switch (optc) {
    315 		case 'H':
    316 			how = HARD;
    317 			break;
    318 		case 'S':
    319 			how = SOFT;
    320 			break;
    321 		case 'a':
    322 			all = 1;
    323 			break;
    324 		default:
    325 			what = optc;
    326 		}
    327 
    328 	for (l = limits; l->name && l->option != what; l++)
    329 		;
    330 	if (!l->name)
    331 		error("internal error (%c)", what);
    332 
    333 	set = *argptr ? 1 : 0;
    334 	if (set) {
    335 		char *p = *argptr;
    336 
    337 		if (all || argptr[1])
    338 			error("too many arguments");
    339 		if (strcmp(p, "unlimited") == 0)
    340 			val = RLIM_INFINITY;
    341 		else {
    342 			val = (rlim_t) 0;
    343 
    344 			while ((c = *p++) >= '0' && c <= '9')
    345 			{
    346 				val = (val * 10) + (long)(c - '0');
    347 				if (val < (rlim_t) 0)
    348 					break;
    349 			}
    350 			if (c)
    351 				error("bad number");
    352 			val *= l->factor;
    353 		}
    354 	}
    355 	if (all) {
    356 		for (l = limits; l->name; l++) {
    357 			getrlimit(l->cmd, &limit);
    358 			if (how & SOFT)
    359 				val = limit.rlim_cur;
    360 			else if (how & HARD)
    361 				val = limit.rlim_max;
    362 
    363 			out1fmt("%-20s ", l->name);
    364 			if (val == RLIM_INFINITY)
    365 				out1fmt("unlimited\n");
    366 			else
    367 			{
    368 				val /= l->factor;
    369 #ifdef BSD4_4
    370 				out1fmt("%lld\n", (long long) val);
    371 #else
    372 				out1fmt("%ld\n", (long) val);
    373 #endif
    374 			}
    375 		}
    376 		return 0;
    377 	}
    378 
    379 	getrlimit(l->cmd, &limit);
    380 	if (set) {
    381 		if (how & HARD)
    382 			limit.rlim_max = val;
    383 		if (how & SOFT)
    384 			limit.rlim_cur = val;
    385 		if (setrlimit(l->cmd, &limit) < 0)
    386 			error("error setting limit (%s)", strerror(errno));
    387 	} else {
    388 		if (how & SOFT)
    389 			val = limit.rlim_cur;
    390 		else if (how & HARD)
    391 			val = limit.rlim_max;
    392 
    393 		if (val == RLIM_INFINITY)
    394 			out1fmt("unlimited\n");
    395 		else
    396 		{
    397 			val /= l->factor;
    398 #ifdef BSD4_4
    399 			out1fmt("%lld\n", (long long) val);
    400 #else
    401 			out1fmt("%ld\n", (long) val);
    402 #endif
    403 		}
    404 	}
    405 	return 0;
    406 }
    407