Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.4
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Kenneth Almquist.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)miscbltin.c	5.2 (Berkeley) 3/13/91";
     39  1.4  jtc static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/bin/sh/miscbltin.c,v 1.4 1993/07/21 00:02:33 jtc Exp $";
     40  1.1  cgd #endif /* not lint */
     41  1.1  cgd 
     42  1.1  cgd /*
     43  1.1  cgd  * Miscelaneous builtins.
     44  1.1  cgd  */
     45  1.1  cgd 
     46  1.4  jtc #include <sys/types.h>
     47  1.4  jtc #include <sys/stat.h>
     48  1.1  cgd #include "shell.h"
     49  1.1  cgd #include "options.h"
     50  1.1  cgd #include "var.h"
     51  1.1  cgd #include "output.h"
     52  1.1  cgd #include "memalloc.h"
     53  1.1  cgd #include "error.h"
     54  1.1  cgd #include "mystring.h"
     55  1.1  cgd 
     56  1.1  cgd #undef eflag
     57  1.1  cgd 
     58  1.1  cgd extern char **argptr;		/* argument list for builtin command */
     59  1.1  cgd 
     60  1.1  cgd 
     61  1.1  cgd /*
     62  1.1  cgd  * The read builtin.  The -e option causes backslashes to escape the
     63  1.1  cgd  * following character.
     64  1.1  cgd  *
     65  1.1  cgd  * This uses unbuffered input, which may be avoidable in some cases.
     66  1.1  cgd  */
     67  1.1  cgd 
     68  1.1  cgd readcmd(argc, argv)  char **argv; {
     69  1.1  cgd 	char **ap;
     70  1.1  cgd 	int backslash;
     71  1.1  cgd 	char c;
     72  1.1  cgd 	int eflag;
     73  1.1  cgd 	char *prompt;
     74  1.1  cgd 	char *ifs;
     75  1.1  cgd 	char *p;
     76  1.1  cgd 	int startword;
     77  1.1  cgd 	int status;
     78  1.1  cgd 	int i;
     79  1.1  cgd 
     80  1.1  cgd 	eflag = 0;
     81  1.1  cgd 	prompt = NULL;
     82  1.1  cgd 	while ((i = nextopt("ep:")) != '\0') {
     83  1.1  cgd 		if (i == 'p')
     84  1.1  cgd 			prompt = optarg;
     85  1.1  cgd 		else
     86  1.1  cgd 			eflag = 1;
     87  1.1  cgd 	}
     88  1.1  cgd 	if (prompt && isatty(0)) {
     89  1.1  cgd 		out2str(prompt);
     90  1.1  cgd 		flushall();
     91  1.1  cgd 	}
     92  1.1  cgd 	if ((ap = argptr) == NULL)
     93  1.1  cgd 		error("arg count");
     94  1.1  cgd 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
     95  1.1  cgd 		ifs = nullstr;
     96  1.1  cgd 	status = 0;
     97  1.1  cgd 	startword = 1;
     98  1.1  cgd 	backslash = 0;
     99  1.1  cgd 	STARTSTACKSTR(p);
    100  1.1  cgd 	for (;;) {
    101  1.1  cgd 		if (read(0, &c, 1) != 1) {
    102  1.1  cgd 			status = 1;
    103  1.1  cgd 			break;
    104  1.1  cgd 		}
    105  1.1  cgd 		if (c == '\0')
    106  1.1  cgd 			continue;
    107  1.1  cgd 		if (backslash) {
    108  1.1  cgd 			backslash = 0;
    109  1.1  cgd 			if (c != '\n')
    110  1.1  cgd 				STPUTC(c, p);
    111  1.1  cgd 			continue;
    112  1.1  cgd 		}
    113  1.1  cgd 		if (eflag && c == '\\') {
    114  1.1  cgd 			backslash++;
    115  1.1  cgd 			continue;
    116  1.1  cgd 		}
    117  1.1  cgd 		if (c == '\n')
    118  1.1  cgd 			break;
    119  1.1  cgd 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
    120  1.1  cgd 			continue;
    121  1.1  cgd 		}
    122  1.1  cgd 		startword = 0;
    123  1.1  cgd 		if (backslash && c == '\\') {
    124  1.1  cgd 			if (read(0, &c, 1) != 1) {
    125  1.1  cgd 				status = 1;
    126  1.1  cgd 				break;
    127  1.1  cgd 			}
    128  1.1  cgd 			STPUTC(c, p);
    129  1.1  cgd 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
    130  1.1  cgd 			STACKSTRNUL(p);
    131  1.1  cgd 			setvar(*ap, stackblock(), 0);
    132  1.1  cgd 			ap++;
    133  1.1  cgd 			startword = 1;
    134  1.1  cgd 			STARTSTACKSTR(p);
    135  1.1  cgd 		} else {
    136  1.1  cgd 			STPUTC(c, p);
    137  1.1  cgd 		}
    138  1.1  cgd 	}
    139  1.1  cgd 	STACKSTRNUL(p);
    140  1.1  cgd 	setvar(*ap, stackblock(), 0);
    141  1.1  cgd 	while (*++ap != NULL)
    142  1.1  cgd 		setvar(*ap, nullstr, 0);
    143  1.1  cgd 	return status;
    144  1.1  cgd }
    145  1.1  cgd 
    146  1.1  cgd 
    147  1.1  cgd 
    148  1.1  cgd umaskcmd(argc, argv)  char **argv; {
    149  1.4  jtc 	extern void *setmode();
    150  1.4  jtc 	extern mode_t getmode();
    151  1.4  jtc 	char *ap;
    152  1.1  cgd 	int mask;
    153  1.1  cgd 	int i;
    154  1.4  jtc 	int symbolic_mode = 0;
    155  1.4  jtc 
    156  1.4  jtc 	while ((i = nextopt("S")) != '\0') {
    157  1.4  jtc 		symbolic_mode = 1;
    158  1.4  jtc 	}
    159  1.1  cgd 
    160  1.4  jtc 	INTOFF;
    161  1.4  jtc 	mask = umask(0);
    162  1.4  jtc 	umask(mask);
    163  1.4  jtc 	INTON;
    164  1.4  jtc 
    165  1.4  jtc 	if ((ap = *argptr) == NULL) {
    166  1.4  jtc 		if (symbolic_mode) {
    167  1.4  jtc 			char u[4], g[4], o[4];
    168  1.4  jtc 
    169  1.4  jtc 			i = 0;
    170  1.4  jtc 			if ((mask & S_IRUSR) == 0)
    171  1.4  jtc 				u[i++] = 'r';
    172  1.4  jtc 			if ((mask & S_IWUSR) == 0)
    173  1.4  jtc 				u[i++] = 'w';
    174  1.4  jtc 			if ((mask & S_IXUSR) == 0)
    175  1.4  jtc 				u[i++] = 'x';
    176  1.4  jtc 			u[i] = '\0';
    177  1.4  jtc 
    178  1.4  jtc 			i = 0;
    179  1.4  jtc 			if ((mask & S_IRGRP) == 0)
    180  1.4  jtc 				g[i++] = 'r';
    181  1.4  jtc 			if ((mask & S_IWGRP) == 0)
    182  1.4  jtc 				g[i++] = 'w';
    183  1.4  jtc 			if ((mask & S_IXGRP) == 0)
    184  1.4  jtc 				g[i++] = 'x';
    185  1.4  jtc 			g[i] = '\0';
    186  1.4  jtc 
    187  1.4  jtc 			i = 0;
    188  1.4  jtc 			if ((mask & S_IROTH) == 0)
    189  1.4  jtc 				o[i++] = 'r';
    190  1.4  jtc 			if ((mask & S_IWOTH) == 0)
    191  1.4  jtc 				o[i++] = 'w';
    192  1.4  jtc 			if ((mask & S_IXOTH) == 0)
    193  1.4  jtc 				o[i++] = 'x';
    194  1.4  jtc 			o[i] = '\0';
    195  1.4  jtc 
    196  1.4  jtc 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
    197  1.4  jtc 		} else {
    198  1.4  jtc 			out1fmt("%.4o\n", mask);
    199  1.4  jtc 		}
    200  1.1  cgd 	} else {
    201  1.4  jtc 		if (isdigit(*ap)) {
    202  1.4  jtc 			mask = 0;
    203  1.4  jtc 			do {
    204  1.4  jtc 				if (*ap >= '8' || *ap < '0')
    205  1.4  jtc 					error("Illegal number: %s", argv[1]);
    206  1.4  jtc 				mask = (mask << 3) + (*ap - '0');
    207  1.4  jtc 			} while (*++ap != '\0');
    208  1.4  jtc 			umask(mask);
    209  1.4  jtc 		} else {
    210  1.4  jtc 			void *set;
    211  1.4  jtc 			if ((set = setmode (ap)) == 0)
    212  1.4  jtc 					error("Illegal number: %s", ap);
    213  1.4  jtc 
    214  1.4  jtc 			mask = getmode (set, ~mask & 0777);
    215  1.4  jtc 			umask(~mask & 0777);
    216  1.4  jtc 		}
    217  1.1  cgd 	}
    218  1.1  cgd 	return 0;
    219  1.1  cgd }
    220