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