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[] = "from: @(#)miscbltin.c 8.2 (Berkeley) 4/16/94";*/ 39 static char *rcsid = "$Id: miscbltin.c,v 1.11 1994/06/11 16:12:11 mycroft Exp $"; 40 #endif /* not lint */ 41 42 /* 43 * Miscelaneous builtins. 44 */ 45 46 #include <sys/types.h> 47 #include <sys/stat.h> 48 #include <unistd.h> 49 #include <ctype.h> 50 #include "shell.h" 51 #include "options.h" 52 #include "var.h" 53 #include "output.h" 54 #include "memalloc.h" 55 #include "error.h" 56 #include "mystring.h" 57 58 #undef eflag 59 60 extern char **argptr; /* argument list for builtin command */ 61 62 63 /* 64 * The read builtin. The -e option causes backslashes to escape the 65 * following character. 66 * 67 * This uses unbuffered input, which may be avoidable in some cases. 68 */ 69 70 readcmd(argc, argv) char **argv; { 71 char **ap; 72 int backslash; 73 char c; 74 int eflag; 75 char *prompt; 76 char *ifs; 77 char *p; 78 int startword; 79 int status; 80 int i; 81 82 eflag = 0; 83 prompt = NULL; 84 while ((i = nextopt("ep:")) != '\0') { 85 if (i == 'p') 86 prompt = optarg; 87 else 88 eflag = 1; 89 } 90 if (prompt && isatty(0)) { 91 out2str(prompt); 92 flushall(); 93 } 94 if (*(ap = argptr) == NULL) 95 error("arg count"); 96 if ((ifs = bltinlookup("IFS", 1)) == NULL) 97 ifs = nullstr; 98 status = 0; 99 startword = 1; 100 backslash = 0; 101 STARTSTACKSTR(p); 102 for (;;) { 103 if (read(0, &c, 1) != 1) { 104 status = 1; 105 break; 106 } 107 if (c == '\0') 108 continue; 109 if (backslash) { 110 backslash = 0; 111 if (c != '\n') 112 STPUTC(c, p); 113 continue; 114 } 115 if (eflag && c == '\\') { 116 backslash++; 117 continue; 118 } 119 if (c == '\n') 120 break; 121 if (startword && *ifs == ' ' && strchr(ifs, c)) { 122 continue; 123 } 124 startword = 0; 125 if (backslash && c == '\\') { 126 if (read(0, &c, 1) != 1) { 127 status = 1; 128 break; 129 } 130 STPUTC(c, p); 131 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 132 STACKSTRNUL(p); 133 setvar(*ap, stackblock(), 0); 134 ap++; 135 startword = 1; 136 STARTSTACKSTR(p); 137 } else { 138 STPUTC(c, p); 139 } 140 } 141 STACKSTRNUL(p); 142 setvar(*ap, stackblock(), 0); 143 while (*++ap != NULL) 144 setvar(*ap, nullstr, 0); 145 return status; 146 } 147 148 149 150 umaskcmd(argc, argv) char **argv; { 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