miscbltin.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1991 The Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * This code is derived from software contributed to Berkeley by
00006  * Kenneth Almquist.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. All advertising materials mentioning features or use of this software
00017  *    must display the following acknowledgement:
00018  *      This product includes software developed by the University of
00019  *      California, Berkeley and its contributors.
00020  * 4. Neither the name of the University nor the names of its contributors
00021  *    may be used to endorse or promote products derived from this software
00022  *    without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00025  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00027  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00030  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00031  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00033  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00034  * SUCH DAMAGE.
00035  */
00036 
00037 #ifndef lint
00038 static char sccsid[] = "@(#)miscbltin.c 5.2 (Berkeley) 3/13/91";
00039 #endif /* not lint */
00040 
00041 /*
00042  * Miscelaneous builtins.
00043  */
00044 
00045 #include "shell.h"
00046 #include "options.h"
00047 #include "var.h"
00048 #include "output.h"
00049 #include "memalloc.h"
00050 #include "error.h"
00051 #include "mystring.h"
00052 
00053 #undef eflag
00054 
00055 extern char **argptr;           /* argument list for builtin command */
00056 
00057 
00058 /*
00059  * The read builtin.  The -e option causes backslashes to escape the
00060  * following character.
00061  *
00062  * This uses unbuffered input, which may be avoidable in some cases.
00063  */
00064 
00065 readcmd(argc, argv)  char **argv; {
00066         char **ap;
00067         int backslash;
00068         char c;
00069         int eflag;
00070         char *prompt;
00071         char *ifs;
00072         char *p;
00073         int startword;
00074         int status;
00075         int i;
00076 
00077         eflag = 0;
00078         prompt = NULL;
00079         while ((i = nextopt("ep:")) != '\0') {
00080                 if (i == 'p')
00081                         prompt = optarg;
00082                 else
00083                         eflag = 1;
00084         }
00085         if (prompt && isatty(0)) {
00086                 out2str(prompt);
00087                 flushall();
00088         }
00089         if (*(ap = argptr) == NULL)
00090                 error("arg count");
00091         if ((ifs = bltinlookup("IFS", 1)) == NULL)
00092                 ifs = nullstr;
00093         status = 0;
00094         startword = 1;
00095         backslash = 0;
00096         STARTSTACKSTR(p);
00097         for (;;) {
00098                 if (read(0, &c, 1) != 1) {
00099                         status = 1;
00100                         break;
00101                 }
00102                 if (c == '\0')
00103                         continue;
00104                 if (backslash) {
00105                         backslash = 0;
00106                         if (c != '\n')
00107                                 STPUTC(c, p);
00108                         continue;
00109                 }
00110                 if (eflag && c == '\\') {
00111                         backslash++;
00112                         continue;
00113                 }
00114                 if (c == '\n')
00115                         break;
00116                 if (startword && *ifs == ' ' && strchr(ifs, c)) {
00117                         continue;
00118                 }
00119                 startword = 0;
00120                 if (backslash && c == '\\') {
00121                         if (read(0, &c, 1) != 1) {
00122                                 status = 1;
00123                                 break;
00124                         }
00125                         STPUTC(c, p);
00126                 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
00127                         STACKSTRNUL(p);
00128                         setvar(*ap, stackblock(), 0);
00129                         ap++;
00130                         startword = 1;
00131                         STARTSTACKSTR(p);
00132                 } else {
00133                         STPUTC(c, p);
00134                 }
00135         }
00136         STACKSTRNUL(p);
00137         setvar(*ap, stackblock(), 0);
00138         while (*++ap != NULL)
00139                 setvar(*ap, nullstr, 0);
00140         return status;
00141 }
00142 
00143 
00144 
00145 umaskcmd(argc, argv)  char **argv; {
00146         int mask;
00147         char *p;
00148         int i;
00149 
00150         if ((p = argv[1]) == NULL) {
00151                 INTOFF;
00152                 mask = umask(0);
00153                 umask(mask);
00154                 INTON;
00155                 out1fmt("%.4o\n", mask);        /* %#o might be better */
00156         } else {
00157                 mask = 0;
00158                 do {
00159                         if ((unsigned)(i = *p - '0') >= 8)
00160                                 error("Illegal number: %s", argv[1]);
00161                         mask = (mask << 3) + i;
00162                 } while (*++p != '\0');
00163                 umask(mask);
00164         }
00165         return 0;
00166 }

Generated on Fri Apr 14 22:56:41 2006 for minix by  doxygen 1.4.6