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.3 1993/03/23 00:28:32 cgd Exp $"; 40 #endif /* not lint */ 41 42 /* 43 * Miscelaneous builtins. 44 */ 45 46 #include "shell.h" 47 #include "options.h" 48 #include "var.h" 49 #include "output.h" 50 #include "memalloc.h" 51 #include "error.h" 52 #include "mystring.h" 53 54 #undef eflag 55 56 extern char **argptr; /* argument list for builtin command */ 57 58 59 /* 60 * The read builtin. The -e option causes backslashes to escape the 61 * following character. 62 * 63 * This uses unbuffered input, which may be avoidable in some cases. 64 */ 65 66 readcmd(argc, argv) char **argv; { 67 char **ap; 68 int backslash; 69 char c; 70 int eflag; 71 char *prompt; 72 char *ifs; 73 char *p; 74 int startword; 75 int status; 76 int i; 77 78 eflag = 0; 79 prompt = NULL; 80 while ((i = nextopt("ep:")) != '\0') { 81 if (i == 'p') 82 prompt = optarg; 83 else 84 eflag = 1; 85 } 86 if (prompt && isatty(0)) { 87 out2str(prompt); 88 flushall(); 89 } 90 if ((ap = argptr) == NULL) 91 error("arg count"); 92 if ((ifs = bltinlookup("IFS", 1)) == NULL) 93 ifs = nullstr; 94 status = 0; 95 startword = 1; 96 backslash = 0; 97 STARTSTACKSTR(p); 98 for (;;) { 99 if (read(0, &c, 1) != 1) { 100 status = 1; 101 break; 102 } 103 if (c == '\0') 104 continue; 105 if (backslash) { 106 backslash = 0; 107 if (c != '\n') 108 STPUTC(c, p); 109 continue; 110 } 111 if (eflag && c == '\\') { 112 backslash++; 113 continue; 114 } 115 if (c == '\n') 116 break; 117 if (startword && *ifs == ' ' && strchr(ifs, c)) { 118 continue; 119 } 120 startword = 0; 121 if (backslash && c == '\\') { 122 if (read(0, &c, 1) != 1) { 123 status = 1; 124 break; 125 } 126 STPUTC(c, p); 127 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 128 STACKSTRNUL(p); 129 setvar(*ap, stackblock(), 0); 130 ap++; 131 startword = 1; 132 STARTSTACKSTR(p); 133 } else { 134 STPUTC(c, p); 135 } 136 } 137 STACKSTRNUL(p); 138 setvar(*ap, stackblock(), 0); 139 while (*++ap != NULL) 140 setvar(*ap, nullstr, 0); 141 return status; 142 } 143 144 145 146 umaskcmd(argc, argv) char **argv; { 147 int mask; 148 char *p; 149 int i; 150 151 if ((p = argv[1]) == NULL) { 152 INTOFF; 153 mask = umask(0); 154 umask(mask); 155 INTON; 156 out1fmt("%.4o\n", mask); /* %#o might be better */ 157 } else { 158 mask = 0; 159 do { 160 if ((unsigned)(i = *p - '0') >= 8) 161 error("Illegal number: %s", argv[1]); 162 mask = (mask << 3) + i; 163 } while (*++p != '\0'); 164 umask(mask); 165 } 166 return 0; 167 } 168