Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.55
      1  1.55       kre /*	$NetBSD: miscbltin.c,v 1.55 2024/10/11 09:02:10 kre Exp $	*/
      2  1.13       cgd 
      3   1.1       cgd /*-
      4   1.7       jtc  * Copyright (c) 1991, 1993
      5   1.7       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Kenneth Almquist.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.32       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19  christos #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.13       cgd #if 0
     38  1.14  christos static char sccsid[] = "@(#)miscbltin.c	8.4 (Berkeley) 5/4/95";
     39  1.13       cgd #else
     40  1.55       kre __RCSID("$NetBSD: miscbltin.c,v 1.55 2024/10/11 09:02:10 kre Exp $");
     41  1.13       cgd #endif
     42   1.1       cgd #endif /* not lint */
     43   1.1       cgd 
     44   1.1       cgd /*
     45  1.47    andvar  * Miscellaneous builtins.
     46   1.1       cgd  */
     47   1.1       cgd 
     48  1.23  christos #include <sys/param.h>		/* BSD4_4 */
     49  1.55       kre #include <sys/resource.h>
     50   1.8       jtc #include <sys/stat.h>
     51  1.14  christos #include <sys/time.h>
     52  1.55       kre #include <sys/types.h>		/* quad_t */
     53  1.55       kre 
     54   1.9       jtc #include <ctype.h>
     55  1.28  christos #include <errno.h>
     56  1.55       kre #include <limits.h>
     57  1.55       kre #include <stdlib.h>
     58  1.55       kre #ifndef SMALL
     59  1.55       kre #include <termios.h>
     60  1.55       kre #endif
     61  1.55       kre #include <unistd.h>
     62  1.14  christos 
     63   1.1       cgd #include "shell.h"
     64   1.1       cgd #include "options.h"
     65   1.1       cgd #include "var.h"
     66  1.55       kre #include "input.h"		/* for whichprompt */
     67   1.1       cgd #include "output.h"
     68  1.55       kre #include "parser.h"		/* for getprompt() */
     69   1.1       cgd #include "memalloc.h"
     70   1.1       cgd #include "error.h"
     71  1.39  christos #include "builtins.h"
     72   1.1       cgd #include "mystring.h"
     73  1.45       kre #include "redir.h"		/* for user_fd_limit */
     74   1.1       cgd 
     75   1.1       cgd /*
     76  1.35       dsl  * The read builtin.
     77  1.47    andvar  * Backslashes escape the next char unless -r is specified.
     78   1.1       cgd  *
     79   1.1       cgd  * This uses unbuffered input, which may be avoidable in some cases.
     80  1.35       dsl  *
     81  1.35       dsl  * Note that if IFS=' :' then read x y should work so that:
     82  1.35       dsl  * 'a b'	x='a', y='b'
     83  1.35       dsl  * ' a b '	x='a', y='b'
     84  1.35       dsl  * ':b'		x='',  y='b'
     85  1.35       dsl  * ':'		x='',  y=''
     86  1.35       dsl  * '::'		x='',  y=''
     87  1.35       dsl  * ': :'	x='',  y=''
     88  1.35       dsl  * ':::'	x='',  y='::'
     89  1.35       dsl  * ':b c:'	x='',  y='b c:'
     90   1.1       cgd  */
     91   1.1       cgd 
     92  1.55       kre #ifndef SMALL
     93  1.55       kre static int b_flag;
     94  1.55       kre 
     95  1.55       kre static int
     96  1.55       kre setrawmode(int fd, int on, int end, struct termios *t)
     97  1.55       kre {
     98  1.55       kre 	struct termios n;
     99  1.55       kre 
    100  1.55       kre 	if (on) {
    101  1.55       kre 		if (tcgetattr(fd, t) != 0)
    102  1.55       kre 			return 0;
    103  1.55       kre 		n = *t;
    104  1.55       kre 		if (on == 1 && b_flag) {
    105  1.55       kre 			n.c_cc[VEOL] = end;
    106  1.55       kre 		} else {
    107  1.55       kre 			cfmakeraw(&n);
    108  1.55       kre 			n.c_iflag |= ICRNL;
    109  1.55       kre 			n.c_oflag = t->c_oflag;
    110  1.55       kre 			n.c_lflag |= ECHO | ISIG;
    111  1.55       kre 		}
    112  1.55       kre 		if (tcsetattr(fd, TCSADRAIN | TCSASOFT, &n) == 0)
    113  1.55       kre 			return 1;
    114  1.55       kre 	} else
    115  1.55       kre 		(void)tcsetattr(fd, TCSADRAIN | TCSASOFT, t);
    116  1.55       kre 	return 0;
    117  1.55       kre }
    118  1.55       kre 
    119  1.55       kre static int
    120  1.55       kre is_a_pipe(int fd)
    121  1.55       kre {
    122  1.55       kre 	if (lseek(fd, 0, SEEK_CUR) == -1 && errno == ESPIPE) {
    123  1.55       kre 		errno = 0;
    124  1.55       kre 		return 1;
    125  1.55       kre 	}
    126  1.55       kre 	return 0;
    127  1.55       kre }
    128  1.55       kre 
    129  1.55       kre #define	READ_BUFFER_SIZE	512
    130  1.55       kre 
    131  1.55       kre static int
    132  1.55       kre next_read_char(int fd, size_t max)
    133  1.55       kre {
    134  1.55       kre 	static char buffer[READ_BUFFER_SIZE];
    135  1.55       kre 	static int pos = 0, len = 0;
    136  1.55       kre 
    137  1.55       kre 	if (max == 0) {
    138  1.55       kre 		pos = len = 0;
    139  1.55       kre 		return -1;
    140  1.55       kre 	}
    141  1.55       kre 	if (max == (size_t)-1) {
    142  1.55       kre 		/*
    143  1.55       kre 		 * If possible, and necessary, rewind the file
    144  1.55       kre 		 * so unprocessed data  can be read again next time
    145  1.55       kre 		 *
    146  1.55       kre 		 * If that fails, never mind (-b allows that to happen)
    147  1.55       kre 		 */
    148  1.55       kre 		if (b_flag && pos < len)
    149  1.55       kre 			(void)lseek(fd, (off_t)(pos - len), SEEK_CUR);
    150  1.55       kre 		return -1;
    151  1.55       kre 	}
    152  1.55       kre 
    153  1.55       kre 	if (b_flag == 0) {
    154  1.55       kre 		char c;
    155  1.55       kre 
    156  1.55       kre 		(void) max;
    157  1.55       kre 		if (read(fd, &c, 1) != 1)
    158  1.55       kre 			return -1;
    159  1.55       kre 		return (c & 0xFF);
    160  1.55       kre 	}
    161  1.55       kre 
    162  1.55       kre 	if (pos >= len) {
    163  1.55       kre 		pos = 0;
    164  1.55       kre 		if (max > sizeof buffer)
    165  1.55       kre 			max = sizeof buffer;
    166  1.55       kre 		len = read(fd, buffer, max);
    167  1.55       kre 		if (len <= 0)
    168  1.55       kre 			return -1;
    169  1.55       kre 	}
    170  1.55       kre 
    171  1.55       kre 	return buffer[pos++] & 0xFF;
    172  1.55       kre }
    173  1.55       kre 
    174  1.55       kre #define READ_OPTS	"bd:n:p:r"
    175  1.55       kre 
    176  1.55       kre #else
    177  1.55       kre 
    178  1.55       kre static inline int
    179  1.55       kre next_read_char(int fd, size_t max)
    180  1.55       kre {
    181  1.55       kre 	char c;
    182  1.55       kre 
    183  1.55       kre 	if (max == 0 || max == (size_t)-1)
    184  1.55       kre 		return 0;
    185  1.55       kre 
    186  1.55       kre 	if (read(fd, &c, 1) != 1)
    187  1.55       kre 		return -1;
    188  1.55       kre 	return (c & 0xFF);
    189  1.55       kre }
    190  1.55       kre 
    191  1.55       kre #define n_flag 0
    192  1.55       kre #define maxlen 0
    193  1.55       kre 
    194  1.55       kre #define READ_OPTS	"d:p:r"
    195  1.55       kre 
    196  1.55       kre #endif
    197  1.55       kre 
    198  1.12       cgd int
    199  1.31  christos readcmd(int argc, char **argv)
    200  1.12       cgd {
    201   1.1       cgd 	char **ap;
    202  1.55       kre 	int c;
    203  1.53       kre 	char end;
    204  1.55       kre 	int r_flag;
    205   1.1       cgd 	char *prompt;
    206  1.35       dsl 	const char *ifs;
    207   1.1       cgd 	char *p;
    208   1.1       cgd 	int startword;
    209   1.1       cgd 	int status;
    210   1.1       cgd 	int i;
    211  1.35       dsl 	int is_ifs;
    212  1.35       dsl 	int saveall = 0;
    213  1.55       kre 	int read_tty = 0;
    214  1.51       kre 	ptrdiff_t wordlen = 0;
    215  1.54       kre 	char *newifs = NULL;
    216  1.54       kre 	struct stackmark mk;
    217   1.1       cgd 
    218  1.55       kre #ifndef SMALL
    219  1.55       kre 	struct termios ttystate;
    220  1.55       kre 	int n_flag, maxlen;
    221  1.55       kre 	int setraw = 0;
    222  1.55       kre 
    223  1.55       kre 	b_flag = 0;
    224  1.55       kre 	n_flag = 0;
    225  1.55       kre 	maxlen = READ_BUFFER_SIZE - 1;
    226  1.55       kre #endif
    227  1.55       kre 
    228  1.53       kre 	end = '\n';				/* record delimiter */
    229  1.55       kre 	r_flag = 0;
    230   1.1       cgd 	prompt = NULL;
    231  1.55       kre 	whichprompt = 2;			/* for continuation lines */
    232  1.55       kre 
    233  1.55       kre 	while ((i = nextopt(READ_OPTS)) != '\0') {
    234  1.53       kre 		switch (i) {
    235  1.53       kre 		case 'd':
    236  1.53       kre 			end = *optionarg;	/* even if '\0' */
    237  1.53       kre 			break;
    238  1.53       kre 		case 'p':
    239  1.30  christos 			prompt = optionarg;
    240  1.53       kre 			break;
    241  1.53       kre 		case 'r':
    242  1.55       kre 			r_flag = 1;
    243  1.53       kre 			break;
    244  1.55       kre #ifndef SMALL
    245  1.55       kre 		case 'n':
    246  1.55       kre 			maxlen = number(optionarg);
    247  1.55       kre 			if (maxlen > (INT_MAX >> 8) + 1)	/* sanity */
    248  1.55       kre 				error("-n %s too large", optionarg);
    249  1.55       kre 			n_flag = 1;
    250  1.55       kre 			break;
    251  1.55       kre 		case 'b':
    252  1.55       kre 			if (!is_a_pipe(0))
    253  1.55       kre 				b_flag = 1;
    254  1.55       kre 			break;
    255  1.55       kre #endif
    256  1.53       kre 		}
    257   1.1       cgd 	}
    258  1.35       dsl 
    259  1.52       kre 	if (*(ap = argptr) == NULL)
    260  1.52       kre 		error("variable name required\n"
    261  1.55       kre #ifdef SMALL
    262  1.55       kre 		      "Usage: read [-r] [-d C] [-p prompt] var...");
    263  1.55       kre #else
    264  1.55       kre 		      "Usage: read [-br] [-d C] [-n len] [-p prompt] var...");
    265  1.52       kre 
    266  1.55       kre 	(void)next_read_char(0, 0);	/* make sure the buffer is empty */
    267  1.55       kre #endif
    268  1.55       kre 
    269  1.55       kre 	if (isatty(0)) {
    270  1.55       kre 		read_tty = 1;
    271  1.55       kre 		if (prompt) {
    272  1.55       kre 			out2str(prompt);
    273  1.55       kre 			flushall();
    274  1.55       kre 		}
    275  1.55       kre #ifndef SMALL
    276  1.55       kre 		b_flag = 1;	/* always buffer reads from ttys */
    277  1.55       kre 
    278  1.55       kre 		if (n_flag || end != '\n')
    279  1.55       kre 			setraw = setrawmode(0, 1 + n_flag, end, &ttystate);
    280  1.55       kre #endif
    281   1.1       cgd 	}
    282  1.35       dsl 
    283  1.55       kre /*
    284   1.1       cgd 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
    285  1.35       dsl 		ifs = " \t\n";
    286  1.55       kre */
    287  1.55       kre 	ifs = ifsval();
    288  1.35       dsl 
    289  1.54       kre 	setstackmark(&mk);
    290   1.1       cgd 	status = 0;
    291  1.35       dsl 	startword = 2;
    292   1.1       cgd 	STARTSTACKSTR(p);
    293  1.55       kre 
    294  1.55       kre #ifdef SMALL
    295  1.55       kre 	for ( ; ; ) {
    296  1.55       kre #else
    297  1.55       kre 	for ( ; !n_flag || --maxlen >= 0 ; ) {
    298  1.55       kre #endif
    299  1.55       kre 		if ((c = next_read_char(0, maxlen + 1)) < 0) {
    300   1.1       cgd 			status = 1;
    301   1.1       cgd 			break;
    302   1.1       cgd 		}
    303  1.55       kre 		if (c == '\\' && c != end && !r_flag) {
    304  1.55       kre #ifndef SMALL
    305  1.55       kre 			if (n_flag && --maxlen < 0)
    306  1.55       kre 				break;
    307  1.55       kre #endif
    308  1.55       kre 			if ((c = next_read_char(0, maxlen + 1)) < 0) {
    309  1.35       dsl 				status = 1;
    310  1.35       dsl 				break;
    311  1.35       dsl 			}
    312  1.53       kre 			if (c != '\n')	/* \ \n is always just removed */
    313  1.51       kre 				goto wdch;
    314  1.55       kre 			if (read_tty)
    315  1.55       kre 				out2str(getprompt(NULL));
    316   1.1       cgd 			continue;
    317   1.1       cgd 		}
    318  1.53       kre 		if (c == end)
    319   1.1       cgd 			break;
    320  1.53       kre 		if (c == '\0')
    321  1.53       kre 			continue;
    322  1.35       dsl 		if (strchr(ifs, c))
    323  1.35       dsl 			is_ifs = strchr(" \t\n", c) ? 1 : 2;
    324  1.35       dsl 		else
    325  1.35       dsl 			is_ifs = 0;
    326  1.35       dsl 
    327  1.35       dsl 		if (startword != 0) {
    328  1.35       dsl 			if (is_ifs == 1) {
    329  1.35       dsl 				/* Ignore leading IFS whitespace */
    330  1.35       dsl 				if (saveall)
    331  1.35       dsl 					STPUTC(c, p);
    332  1.35       dsl 				continue;
    333  1.35       dsl 			}
    334  1.35       dsl 			if (is_ifs == 2 && startword == 1) {
    335  1.35       dsl 				/* Only one non-whitespace IFS per word */
    336  1.35       dsl 				startword = 2;
    337  1.35       dsl 				if (saveall)
    338  1.35       dsl 					STPUTC(c, p);
    339  1.35       dsl 				continue;
    340  1.35       dsl 			}
    341  1.35       dsl 		}
    342  1.35       dsl 
    343  1.35       dsl 		if (is_ifs == 0) {
    344  1.51       kre   wdch:;
    345  1.55       kre 			if (c == '\0') /* always ignore attempts to input \0 */
    346  1.53       kre 				continue;
    347  1.35       dsl 			/* append this character to the current variable */
    348  1.35       dsl 			startword = 0;
    349  1.35       dsl 			if (saveall)
    350  1.35       dsl 				/* Not just a spare terminator */
    351  1.35       dsl 				saveall++;
    352  1.35       dsl 			STPUTC(c, p);
    353  1.51       kre 			wordlen = p - stackblock();
    354   1.1       cgd 			continue;
    355   1.1       cgd 		}
    356  1.35       dsl 
    357  1.35       dsl 		/* end of variable... */
    358  1.35       dsl 		startword = is_ifs;
    359  1.35       dsl 
    360  1.35       dsl 		if (ap[1] == NULL) {
    361  1.35       dsl 			/* Last variable needs all IFS chars */
    362  1.35       dsl 			saveall++;
    363   1.1       cgd 			STPUTC(c, p);
    364  1.35       dsl 			continue;
    365   1.1       cgd 		}
    366  1.35       dsl 
    367  1.54       kre 		if (equal(*ap, "IFS")) {
    368  1.54       kre 			/*
    369  1.54       kre 			 * we must not alter the value of IFS, as our
    370  1.54       kre 			 * local "ifs" var is (perhaps) pointing at it,
    371  1.54       kre 			 * at best we would be using data after free()
    372  1.54       kre 			 * the next time we reference ifs - but that mem
    373  1.54       kre 			 * may have been reused for something different.
    374  1.54       kre 			 *
    375  1.54       kre 			 * note that this might occur several times
    376  1.54       kre 			 */
    377  1.54       kre 			STPUTC('\0', p);
    378  1.54       kre 			newifs = grabstackstr(p);
    379  1.54       kre 		} else {
    380  1.54       kre 			STACKSTRNUL(p);
    381  1.54       kre 			setvar(*ap, stackblock(), 0);
    382  1.54       kre 		}
    383  1.35       dsl 		ap++;
    384  1.35       dsl 		STARTSTACKSTR(p);
    385  1.51       kre 		wordlen = 0;
    386   1.1       cgd 	}
    387   1.1       cgd 	STACKSTRNUL(p);
    388  1.35       dsl 
    389  1.55       kre #ifndef SMALL
    390  1.55       kre 	(void)next_read_char(0, (size_t)-1);	/* attempt to seek back */
    391  1.55       kre 	if (setraw)
    392  1.55       kre 		setrawmode(0, 0, end, &ttystate);
    393  1.55       kre #endif
    394  1.55       kre 
    395  1.55       kre 
    396  1.35       dsl 	/* Remove trailing IFS chars */
    397  1.51       kre 	for (; stackblock() + wordlen <= --p; *p = 0) {
    398  1.35       dsl 		if (!strchr(ifs, *p))
    399  1.35       dsl 			break;
    400  1.35       dsl 		if (strchr(" \t\n", *p))
    401  1.35       dsl 			/* Always remove whitespace */
    402  1.35       dsl 			continue;
    403  1.35       dsl 		if (saveall > 1)
    404  1.35       dsl 			/* Don't remove non-whitespace unless it was naked */
    405  1.35       dsl 			break;
    406  1.35       dsl 	}
    407  1.54       kre 
    408  1.54       kre 	/*
    409  1.54       kre 	 * If IFS was one of the variables named, we can finally set it now
    410  1.54       kre 	 * (no further references to ifs will be made)
    411  1.54       kre 	 */
    412  1.54       kre 	if (newifs != NULL)
    413  1.54       kre 		setvar("IFS", newifs, 0);
    414  1.54       kre 
    415  1.54       kre 	/*
    416  1.54       kre 	 * Now we can assign to the final variable (which might
    417  1.54       kre 	 * also be IFS, hence the ordering here)
    418  1.54       kre 	 */
    419   1.1       cgd 	setvar(*ap, stackblock(), 0);
    420  1.35       dsl 
    421  1.35       dsl 	/* Set any remaining args to "" */
    422   1.1       cgd 	while (*++ap != NULL)
    423   1.1       cgd 		setvar(*ap, nullstr, 0);
    424  1.54       kre 
    425  1.54       kre 	popstackmark(&mk);
    426   1.1       cgd 	return status;
    427   1.1       cgd }
    428   1.1       cgd 
    429   1.1       cgd 
    430   1.1       cgd 
    431  1.12       cgd int
    432  1.31  christos umaskcmd(int argc, char **argv)
    433  1.12       cgd {
    434   1.8       jtc 	char *ap;
    435  1.49       kre 	mode_t mask;
    436   1.1       cgd 	int i;
    437   1.8       jtc 	int symbolic_mode = 0;
    438   1.8       jtc 
    439   1.8       jtc 	while ((i = nextopt("S")) != '\0') {
    440   1.8       jtc 		symbolic_mode = 1;
    441   1.8       jtc 	}
    442   1.1       cgd 
    443   1.8       jtc 	INTOFF;
    444   1.8       jtc 	mask = umask(0);
    445   1.8       jtc 	umask(mask);
    446   1.8       jtc 	INTON;
    447   1.8       jtc 
    448   1.8       jtc 	if ((ap = *argptr) == NULL) {
    449   1.8       jtc 		if (symbolic_mode) {
    450   1.8       jtc 			char u[4], g[4], o[4];
    451   1.8       jtc 
    452   1.8       jtc 			i = 0;
    453   1.8       jtc 			if ((mask & S_IRUSR) == 0)
    454   1.8       jtc 				u[i++] = 'r';
    455   1.8       jtc 			if ((mask & S_IWUSR) == 0)
    456   1.8       jtc 				u[i++] = 'w';
    457   1.8       jtc 			if ((mask & S_IXUSR) == 0)
    458   1.8       jtc 				u[i++] = 'x';
    459   1.8       jtc 			u[i] = '\0';
    460   1.8       jtc 
    461   1.8       jtc 			i = 0;
    462   1.8       jtc 			if ((mask & S_IRGRP) == 0)
    463   1.8       jtc 				g[i++] = 'r';
    464   1.8       jtc 			if ((mask & S_IWGRP) == 0)
    465   1.8       jtc 				g[i++] = 'w';
    466   1.8       jtc 			if ((mask & S_IXGRP) == 0)
    467   1.8       jtc 				g[i++] = 'x';
    468   1.8       jtc 			g[i] = '\0';
    469   1.8       jtc 
    470   1.8       jtc 			i = 0;
    471   1.8       jtc 			if ((mask & S_IROTH) == 0)
    472   1.8       jtc 				o[i++] = 'r';
    473   1.8       jtc 			if ((mask & S_IWOTH) == 0)
    474   1.8       jtc 				o[i++] = 'w';
    475   1.8       jtc 			if ((mask & S_IXOTH) == 0)
    476   1.8       jtc 				o[i++] = 'x';
    477   1.8       jtc 			o[i] = '\0';
    478   1.8       jtc 
    479   1.8       jtc 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
    480   1.8       jtc 		} else {
    481   1.8       jtc 			out1fmt("%.4o\n", mask);
    482   1.8       jtc 		}
    483   1.1       cgd 	} else {
    484  1.25  christos 		if (isdigit((unsigned char)*ap)) {
    485  1.49       kre 			int range = 0;
    486  1.49       kre 
    487   1.8       jtc 			mask = 0;
    488   1.8       jtc 			do {
    489   1.8       jtc 				if (*ap >= '8' || *ap < '0')
    490  1.48       kre 					error("Not a valid octal number: '%s'",
    491  1.49       kre 					    *argptr);
    492   1.8       jtc 				mask = (mask << 3) + (*ap - '0');
    493  1.49       kre 				if (mask & ~07777)
    494  1.49       kre 					range = 1;
    495   1.8       jtc 			} while (*++ap != '\0');
    496  1.49       kre 			if (range)
    497  1.49       kre 			    error("Mask constant '%s' out of range", *argptr);
    498   1.8       jtc 			umask(mask);
    499   1.8       jtc 		} else {
    500  1.16  christos 			void *set;
    501   1.8       jtc 
    502  1.26     itohy 			INTOFF;
    503  1.26     itohy 			if ((set = setmode(ap)) != 0) {
    504  1.26     itohy 				mask = getmode(set, ~mask & 0777);
    505  1.26     itohy 				ckfree(set);
    506  1.26     itohy 			}
    507  1.26     itohy 			INTON;
    508  1.26     itohy 			if (!set)
    509  1.36  christos 				error("Cannot set mode `%s' (%s)", ap,
    510  1.36  christos 				    strerror(errno));
    511  1.26     itohy 
    512   1.8       jtc 			umask(~mask & 0777);
    513  1.14  christos 		}
    514  1.14  christos 	}
    515  1.46       kre 	flushout(out1);
    516  1.46       kre 	if (io_err(out1)) {
    517  1.46       kre 		out2str("umask: I/O error\n");
    518  1.46       kre 		return 1;
    519  1.46       kre 	}
    520  1.14  christos 	return 0;
    521  1.14  christos }
    522  1.14  christos 
    523  1.14  christos /*
    524  1.14  christos  * ulimit builtin
    525  1.14  christos  *
    526  1.14  christos  * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
    527  1.14  christos  * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
    528  1.14  christos  * ash by J.T. Conklin.
    529  1.14  christos  *
    530  1.14  christos  * Public domain.
    531  1.14  christos  */
    532  1.14  christos 
    533  1.14  christos struct limits {
    534  1.14  christos 	const char *name;
    535  1.40  christos 	const char *unit;
    536  1.14  christos 	char	option;
    537  1.50       kre 	int8_t	cmd;		/* all RLIMIT_xxx are <= 127 */
    538  1.50       kre 	unsigned short factor;	/* multiply by to get rlim_{cur,max} values */
    539  1.14  christos };
    540  1.14  christos 
    541  1.50       kre #define	OPTSTRING_BASE "HSa"
    542  1.50       kre 
    543  1.14  christos static const struct limits limits[] = {
    544  1.14  christos #ifdef RLIMIT_CPU
    545  1.50       kre 	{ "time",	"seconds",	't',	RLIMIT_CPU,	   1 },
    546  1.50       kre #define	OPTSTRING_t	OPTSTRING_BASE "t"
    547  1.50       kre #else
    548  1.50       kre #define	OPTSTRING_t	OPTSTRING_BASE
    549  1.14  christos #endif
    550  1.14  christos #ifdef RLIMIT_FSIZE
    551  1.50       kre 	{ "file",	"blocks",	'f',	RLIMIT_FSIZE,	 512 },
    552  1.50       kre #define	OPTSTRING_f	OPTSTRING_t "f"
    553  1.50       kre #else
    554  1.50       kre #define	OPTSTRING_f	OPTSTRING_t
    555  1.14  christos #endif
    556  1.14  christos #ifdef RLIMIT_DATA
    557  1.50       kre 	{ "data",	"kbytes",	'd',	RLIMIT_DATA,	1024 },
    558  1.50       kre #define	OPTSTRING_d	OPTSTRING_f "d"
    559  1.50       kre #else
    560  1.50       kre #define	OPTSTRING_d	OPTSTRING_f
    561  1.14  christos #endif
    562  1.14  christos #ifdef RLIMIT_STACK
    563  1.50       kre 	{ "stack",	"kbytes",	's',	RLIMIT_STACK,	1024 },
    564  1.50       kre #define	OPTSTRING_s	OPTSTRING_d "s"
    565  1.50       kre #else
    566  1.50       kre #define	OPTSTRING_s	OPTSTRING_d
    567  1.14  christos #endif
    568  1.44      gson #ifdef RLIMIT_CORE
    569  1.50       kre 	{ "coredump",	"blocks",	'c',	RLIMIT_CORE,	 512 },
    570  1.50       kre #define	OPTSTRING_c	OPTSTRING_s "c"
    571  1.50       kre #else
    572  1.50       kre #define	OPTSTRING_c	OPTSTRING_s
    573  1.14  christos #endif
    574  1.14  christos #ifdef RLIMIT_RSS
    575  1.50       kre 	{ "memory",	"kbytes",	'm',	RLIMIT_RSS,	1024 },
    576  1.50       kre #define	OPTSTRING_m	OPTSTRING_c "m"
    577  1.50       kre #else
    578  1.50       kre #define	OPTSTRING_m	OPTSTRING_c
    579  1.14  christos #endif
    580  1.14  christos #ifdef RLIMIT_MEMLOCK
    581  1.50       kre 	{ "locked memory","kbytes",	'l',	RLIMIT_MEMLOCK, 1024 },
    582  1.50       kre #define	OPTSTRING_l	OPTSTRING_m "l"
    583  1.50       kre #else
    584  1.50       kre #define	OPTSTRING_l	OPTSTRING_m
    585  1.14  christos #endif
    586  1.41  christos #ifdef RLIMIT_NTHR
    587  1.50       kre 	{ "thread",	"threads",	'r',	RLIMIT_NTHR,       1 },
    588  1.50       kre #define	OPTSTRING_r	OPTSTRING_l "r"
    589  1.50       kre #else
    590  1.50       kre #define	OPTSTRING_r	OPTSTRING_l
    591  1.41  christos #endif
    592  1.14  christos #ifdef RLIMIT_NPROC
    593  1.50       kre 	{ "process",	"processes",	'p',	RLIMIT_NPROC,      1 },
    594  1.50       kre #define	OPTSTRING_p	OPTSTRING_r "p"
    595  1.50       kre #else
    596  1.50       kre #define	OPTSTRING_p	OPTSTRING_r
    597  1.14  christos #endif
    598  1.14  christos #ifdef RLIMIT_NOFILE
    599  1.50       kre 	{ "nofiles",	"descriptors",	'n',	RLIMIT_NOFILE,     1 },
    600  1.50       kre #define	OPTSTRING_n	OPTSTRING_p "n"
    601  1.50       kre #else
    602  1.50       kre #define	OPTSTRING_n	OPTSTRING_p
    603  1.14  christos #endif
    604  1.14  christos #ifdef RLIMIT_VMEM
    605  1.50       kre 	{ "vmemory",	"kbytes",	'v',	RLIMIT_VMEM,	1024 },
    606  1.50       kre #define	OPTSTRING_v	OPTSTRING_n "v"
    607  1.50       kre #else
    608  1.50       kre #define	OPTSTRING_v	OPTSTRING_n
    609  1.14  christos #endif
    610  1.14  christos #ifdef RLIMIT_SWAP
    611  1.50       kre 	{ "swap",	"kbytes",	'w',	RLIMIT_SWAP,	1024 },
    612  1.50       kre #define	OPTSTRING_w	OPTSTRING_v "w"
    613  1.50       kre #else
    614  1.50       kre #define	OPTSTRING_w	OPTSTRING_v
    615  1.14  christos #endif
    616  1.33  christos #ifdef RLIMIT_SBSIZE
    617  1.50       kre 	{ "sbsize",	"bytes",	'b',	RLIMIT_SBSIZE,	   1 },
    618  1.50       kre #define	OPTSTRING_b	OPTSTRING_w "b"
    619  1.50       kre #else
    620  1.50       kre #define	OPTSTRING_b	OPTSTRING_w
    621  1.33  christos #endif
    622  1.50       kre 	{ NULL,		NULL,		'\0',	0,		   0 }
    623  1.14  christos };
    624  1.50       kre #define	OPTSTRING	OPTSTRING_b
    625  1.14  christos 
    626  1.14  christos int
    627  1.31  christos ulimitcmd(int argc, char **argv)
    628  1.14  christos {
    629  1.17       tls 	int	c;
    630  1.19  christos 	rlim_t val = 0;
    631  1.14  christos 	enum { SOFT = 0x1, HARD = 0x2 }
    632  1.45       kre 			how = 0, which;
    633  1.14  christos 	const struct limits	*l;
    634  1.14  christos 	int		set, all = 0;
    635  1.14  christos 	int		optc, what;
    636  1.14  christos 	struct rlimit	limit;
    637  1.14  christos 
    638  1.14  christos 	what = 'f';
    639  1.50       kre 	while ((optc = nextopt(OPTSTRING)) != '\0')
    640  1.14  christos 		switch (optc) {
    641  1.14  christos 		case 'H':
    642  1.45       kre 			how |= HARD;
    643  1.14  christos 			break;
    644  1.14  christos 		case 'S':
    645  1.45       kre 			how |= SOFT;
    646  1.14  christos 			break;
    647  1.14  christos 		case 'a':
    648  1.14  christos 			all = 1;
    649  1.14  christos 			break;
    650  1.14  christos 		default:
    651  1.14  christos 			what = optc;
    652  1.14  christos 		}
    653  1.14  christos 
    654  1.14  christos 	for (l = limits; l->name && l->option != what; l++)
    655  1.14  christos 		;
    656  1.14  christos 	if (!l->name)
    657  1.28  christos 		error("internal error (%c)", what);
    658  1.14  christos 
    659  1.14  christos 	set = *argptr ? 1 : 0;
    660  1.14  christos 	if (set) {
    661  1.14  christos 		char *p = *argptr;
    662  1.14  christos 
    663  1.14  christos 		if (all || argptr[1])
    664  1.28  christos 			error("too many arguments");
    665  1.45       kre 		if (how == 0)
    666  1.45       kre 			how = HARD | SOFT;
    667  1.45       kre 
    668  1.14  christos 		if (strcmp(p, "unlimited") == 0)
    669  1.14  christos 			val = RLIM_INFINITY;
    670  1.14  christos 		else {
    671  1.15       jtc 			val = (rlim_t) 0;
    672  1.14  christos 
    673  1.45       kre 			while ((c = *p++) >= '0' && c <= '9') {
    674  1.45       kre 				if (val >= RLIM_INFINITY/10)
    675  1.50       kre 					error("%s: value overflow", *argptr);
    676  1.45       kre 				val = (val * 10);
    677  1.45       kre 				if (val >= RLIM_INFINITY - (long)(c - '0'))
    678  1.50       kre 					error("%s: value overflow", *argptr);
    679  1.45       kre 				val += (long)(c - '0');
    680  1.45       kre 			}
    681  1.14  christos 			if (c)
    682  1.50       kre 				error("%s: bad number", *argptr);
    683  1.45       kre 			if (val > RLIM_INFINITY / l->factor)
    684  1.50       kre 				error("%s: value overflow", *argptr);
    685  1.14  christos 			val *= l->factor;
    686  1.14  christos 		}
    687  1.45       kre 	} else if (how == 0)
    688  1.45       kre 		how = SOFT;
    689  1.45       kre 
    690  1.14  christos 	if (all) {
    691  1.14  christos 		for (l = limits; l->name; l++) {
    692  1.14  christos 			getrlimit(l->cmd, &limit);
    693  1.45       kre 			out1fmt("%-13s (-%c %-11s)    ", l->name, l->option,
    694  1.45       kre 			    l->unit);
    695  1.14  christos 
    696  1.45       kre 			which = how;
    697  1.45       kre 			while (which != 0) {
    698  1.45       kre 				if (which & SOFT) {
    699  1.45       kre 					val = limit.rlim_cur;
    700  1.45       kre 					which &= ~SOFT;
    701  1.45       kre 				} else if (which & HARD) {
    702  1.45       kre 					val = limit.rlim_max;
    703  1.45       kre 					which &= ~HARD;
    704  1.45       kre 				}
    705  1.45       kre 
    706  1.45       kre 				if (val == RLIM_INFINITY)
    707  1.45       kre 					out1fmt("unlimited");
    708  1.45       kre 				else {
    709  1.45       kre 					val /= l->factor;
    710  1.18  christos #ifdef BSD4_4
    711  1.45       kre 					out1fmt("%9lld", (long long) val);
    712  1.18  christos #else
    713  1.45       kre 					out1fmt("%9ld", (long) val);
    714  1.18  christos #endif
    715  1.45       kre 				}
    716  1.45       kre 				out1fmt("%c", which ? '\t' : '\n');
    717  1.14  christos 			}
    718  1.14  christos 		}
    719  1.46       kre 		goto done;
    720  1.14  christos 	}
    721  1.14  christos 
    722  1.43  christos 	if (getrlimit(l->cmd, &limit) == -1)
    723  1.43  christos 		error("error getting limit (%s)", strerror(errno));
    724  1.14  christos 	if (set) {
    725  1.28  christos 		if (how & HARD)
    726  1.28  christos 			limit.rlim_max = val;
    727  1.14  christos 		if (how & SOFT)
    728  1.14  christos 			limit.rlim_cur = val;
    729  1.14  christos 		if (setrlimit(l->cmd, &limit) < 0)
    730  1.28  christos 			error("error setting limit (%s)", strerror(errno));
    731  1.45       kre 		if (l->cmd == RLIMIT_NOFILE)
    732  1.45       kre 			user_fd_limit = sysconf(_SC_OPEN_MAX);
    733  1.14  christos 	} else {
    734  1.14  christos 		if (how & SOFT)
    735  1.14  christos 			val = limit.rlim_cur;
    736  1.14  christos 		else if (how & HARD)
    737  1.14  christos 			val = limit.rlim_max;
    738  1.14  christos 
    739  1.14  christos 		if (val == RLIM_INFINITY)
    740  1.14  christos 			out1fmt("unlimited\n");
    741  1.14  christos 		else
    742  1.14  christos 		{
    743  1.14  christos 			val /= l->factor;
    744  1.18  christos #ifdef BSD4_4
    745  1.29     lukem 			out1fmt("%lld\n", (long long) val);
    746  1.18  christos #else
    747  1.18  christos 			out1fmt("%ld\n", (long) val);
    748  1.18  christos #endif
    749   1.8       jtc 		}
    750   1.1       cgd 	}
    751  1.46       kre   done:;
    752  1.46       kre 	flushout(out1);
    753  1.46       kre 	if (io_err(out1)) {
    754  1.46       kre 		out2str("ulimit: I/O error (stdout)\n");
    755  1.46       kre 		return 1;
    756  1.46       kre 	}
    757   1.1       cgd 	return 0;
    758   1.1       cgd }
    759