xref: /netbsd-src/bin/sh/miscbltin.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * 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	5.2 (Berkeley) 3/13/91";
39 static char rcsid[] = "$Header: /cvsroot/src/bin/sh/miscbltin.c,v 1.4 1993/07/21 00:02:33 jtc Exp $";
40 #endif /* not lint */
41 
42 /*
43  * Miscelaneous builtins.
44  */
45 
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include "shell.h"
49 #include "options.h"
50 #include "var.h"
51 #include "output.h"
52 #include "memalloc.h"
53 #include "error.h"
54 #include "mystring.h"
55 
56 #undef eflag
57 
58 extern char **argptr;		/* argument list for builtin command */
59 
60 
61 /*
62  * The read builtin.  The -e option causes backslashes to escape the
63  * following character.
64  *
65  * This uses unbuffered input, which may be avoidable in some cases.
66  */
67 
68 readcmd(argc, argv)  char **argv; {
69 	char **ap;
70 	int backslash;
71 	char c;
72 	int eflag;
73 	char *prompt;
74 	char *ifs;
75 	char *p;
76 	int startword;
77 	int status;
78 	int i;
79 
80 	eflag = 0;
81 	prompt = NULL;
82 	while ((i = nextopt("ep:")) != '\0') {
83 		if (i == 'p')
84 			prompt = optarg;
85 		else
86 			eflag = 1;
87 	}
88 	if (prompt && isatty(0)) {
89 		out2str(prompt);
90 		flushall();
91 	}
92 	if ((ap = argptr) == NULL)
93 		error("arg count");
94 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
95 		ifs = nullstr;
96 	status = 0;
97 	startword = 1;
98 	backslash = 0;
99 	STARTSTACKSTR(p);
100 	for (;;) {
101 		if (read(0, &c, 1) != 1) {
102 			status = 1;
103 			break;
104 		}
105 		if (c == '\0')
106 			continue;
107 		if (backslash) {
108 			backslash = 0;
109 			if (c != '\n')
110 				STPUTC(c, p);
111 			continue;
112 		}
113 		if (eflag && c == '\\') {
114 			backslash++;
115 			continue;
116 		}
117 		if (c == '\n')
118 			break;
119 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
120 			continue;
121 		}
122 		startword = 0;
123 		if (backslash && c == '\\') {
124 			if (read(0, &c, 1) != 1) {
125 				status = 1;
126 				break;
127 			}
128 			STPUTC(c, p);
129 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
130 			STACKSTRNUL(p);
131 			setvar(*ap, stackblock(), 0);
132 			ap++;
133 			startword = 1;
134 			STARTSTACKSTR(p);
135 		} else {
136 			STPUTC(c, p);
137 		}
138 	}
139 	STACKSTRNUL(p);
140 	setvar(*ap, stackblock(), 0);
141 	while (*++ap != NULL)
142 		setvar(*ap, nullstr, 0);
143 	return status;
144 }
145 
146 
147 
148 umaskcmd(argc, argv)  char **argv; {
149 	extern void *setmode();
150 	extern mode_t getmode();
151 	char *ap;
152 	int mask;
153 	int i;
154 	int symbolic_mode = 0;
155 
156 	while ((i = nextopt("S")) != '\0') {
157 		symbolic_mode = 1;
158 	}
159 
160 	INTOFF;
161 	mask = umask(0);
162 	umask(mask);
163 	INTON;
164 
165 	if ((ap = *argptr) == NULL) {
166 		if (symbolic_mode) {
167 			char u[4], g[4], o[4];
168 
169 			i = 0;
170 			if ((mask & S_IRUSR) == 0)
171 				u[i++] = 'r';
172 			if ((mask & S_IWUSR) == 0)
173 				u[i++] = 'w';
174 			if ((mask & S_IXUSR) == 0)
175 				u[i++] = 'x';
176 			u[i] = '\0';
177 
178 			i = 0;
179 			if ((mask & S_IRGRP) == 0)
180 				g[i++] = 'r';
181 			if ((mask & S_IWGRP) == 0)
182 				g[i++] = 'w';
183 			if ((mask & S_IXGRP) == 0)
184 				g[i++] = 'x';
185 			g[i] = '\0';
186 
187 			i = 0;
188 			if ((mask & S_IROTH) == 0)
189 				o[i++] = 'r';
190 			if ((mask & S_IWOTH) == 0)
191 				o[i++] = 'w';
192 			if ((mask & S_IXOTH) == 0)
193 				o[i++] = 'x';
194 			o[i] = '\0';
195 
196 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
197 		} else {
198 			out1fmt("%.4o\n", mask);
199 		}
200 	} else {
201 		if (isdigit(*ap)) {
202 			mask = 0;
203 			do {
204 				if (*ap >= '8' || *ap < '0')
205 					error("Illegal number: %s", argv[1]);
206 				mask = (mask << 3) + (*ap - '0');
207 			} while (*++ap != '\0');
208 			umask(mask);
209 		} else {
210 			void *set;
211 			if ((set = setmode (ap)) == 0)
212 					error("Illegal number: %s", ap);
213 
214 			mask = getmode (set, ~mask & 0777);
215 			umask(~mask & 0777);
216 		}
217 	}
218 	return 0;
219 }
220