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 char *ap; 151 int mask; 152 int i; 153 int symbolic_mode = 0; 154 155 while ((i = nextopt("S")) != '\0') { 156 symbolic_mode = 1; 157 } 158 159 INTOFF; 160 mask = umask(0); 161 umask(mask); 162 INTON; 163 164 if ((ap = *argptr) == NULL) { 165 if (symbolic_mode) { 166 char u[4], g[4], o[4]; 167 168 i = 0; 169 if ((mask & S_IRUSR) == 0) 170 u[i++] = 'r'; 171 if ((mask & S_IWUSR) == 0) 172 u[i++] = 'w'; 173 if ((mask & S_IXUSR) == 0) 174 u[i++] = 'x'; 175 u[i] = '\0'; 176 177 i = 0; 178 if ((mask & S_IRGRP) == 0) 179 g[i++] = 'r'; 180 if ((mask & S_IWGRP) == 0) 181 g[i++] = 'w'; 182 if ((mask & S_IXGRP) == 0) 183 g[i++] = 'x'; 184 g[i] = '\0'; 185 186 i = 0; 187 if ((mask & S_IROTH) == 0) 188 o[i++] = 'r'; 189 if ((mask & S_IWOTH) == 0) 190 o[i++] = 'w'; 191 if ((mask & S_IXOTH) == 0) 192 o[i++] = 'x'; 193 o[i] = '\0'; 194 195 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 196 } else { 197 out1fmt("%.4o\n", mask); 198 } 199 } else { 200 if (isdigit(*ap)) { 201 mask = 0; 202 do { 203 if (*ap >= '8' || *ap < '0') 204 error("Illegal number: %s", argv[1]); 205 mask = (mask << 3) + (*ap - '0'); 206 } while (*++ap != '\0'); 207 umask(mask); 208 } else { 209 void *set; 210 if ((set = setmode (ap)) == 0) 211 error("Illegal number: %s", ap); 212 213 mask = getmode (set, ~mask & 0777); 214 umask(~mask & 0777); 215 } 216 } 217 return 0; 218 } 219