Home | History | Annotate | Line # | Download | only in sh
miscbltin.c revision 1.13
      1  1.13  cgd /*	$NetBSD: miscbltin.c,v 1.13 1995/03/21 09:09:33 cgd Exp $	*/
      2  1.13  cgd 
      3   1.1  cgd /*-
      4   1.7  jtc  * Copyright (c) 1991, 1993
      5   1.7  jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1  cgd  *
      7   1.1  cgd  * This code is derived from software contributed to Berkeley by
      8   1.1  cgd  * Kenneth Almquist.
      9   1.1  cgd  *
     10   1.1  cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1  cgd  * modification, are permitted provided that the following conditions
     12   1.1  cgd  * are met:
     13   1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1  cgd  *    must display the following acknowledgement:
     20   1.1  cgd  *	This product includes software developed by the University of
     21   1.1  cgd  *	California, Berkeley and its contributors.
     22   1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1  cgd  *    may be used to endorse or promote products derived from this software
     24   1.1  cgd  *    without specific prior written permission.
     25   1.1  cgd  *
     26   1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1  cgd  * SUCH DAMAGE.
     37   1.1  cgd  */
     38   1.1  cgd 
     39   1.1  cgd #ifndef lint
     40  1.13  cgd #if 0
     41  1.13  cgd static char sccsid[] = "@(#)miscbltin.c	8.2 (Berkeley) 4/16/94";
     42  1.13  cgd #else
     43  1.13  cgd static char rcsid[] = "$NetBSD: miscbltin.c,v 1.13 1995/03/21 09:09:33 cgd Exp $";
     44  1.13  cgd #endif
     45   1.1  cgd #endif /* not lint */
     46   1.1  cgd 
     47   1.1  cgd /*
     48   1.1  cgd  * Miscelaneous builtins.
     49   1.1  cgd  */
     50   1.1  cgd 
     51   1.8  jtc #include <sys/types.h>
     52   1.8  jtc #include <sys/stat.h>
     53   1.9  jtc #include <unistd.h>
     54   1.9  jtc #include <ctype.h>
     55   1.1  cgd #include "shell.h"
     56   1.1  cgd #include "options.h"
     57   1.1  cgd #include "var.h"
     58   1.1  cgd #include "output.h"
     59   1.1  cgd #include "memalloc.h"
     60   1.1  cgd #include "error.h"
     61   1.1  cgd #include "mystring.h"
     62   1.1  cgd 
     63   1.1  cgd #undef eflag
     64   1.1  cgd 
     65   1.1  cgd extern char **argptr;		/* argument list for builtin command */
     66   1.1  cgd 
     67   1.1  cgd 
     68   1.1  cgd /*
     69   1.1  cgd  * The read builtin.  The -e option causes backslashes to escape the
     70   1.1  cgd  * following character.
     71   1.1  cgd  *
     72   1.1  cgd  * This uses unbuffered input, which may be avoidable in some cases.
     73   1.1  cgd  */
     74   1.1  cgd 
     75  1.12  cgd int
     76  1.12  cgd readcmd(argc, argv)
     77  1.12  cgd 	int argc;
     78  1.12  cgd 	char **argv;
     79  1.12  cgd {
     80   1.1  cgd 	char **ap;
     81   1.1  cgd 	int backslash;
     82   1.1  cgd 	char c;
     83   1.1  cgd 	int eflag;
     84   1.1  cgd 	char *prompt;
     85   1.1  cgd 	char *ifs;
     86   1.1  cgd 	char *p;
     87   1.1  cgd 	int startword;
     88   1.1  cgd 	int status;
     89   1.1  cgd 	int i;
     90   1.1  cgd 
     91   1.1  cgd 	eflag = 0;
     92   1.1  cgd 	prompt = NULL;
     93   1.1  cgd 	while ((i = nextopt("ep:")) != '\0') {
     94   1.1  cgd 		if (i == 'p')
     95   1.1  cgd 			prompt = optarg;
     96   1.1  cgd 		else
     97   1.1  cgd 			eflag = 1;
     98   1.1  cgd 	}
     99   1.1  cgd 	if (prompt && isatty(0)) {
    100   1.1  cgd 		out2str(prompt);
    101   1.1  cgd 		flushall();
    102   1.1  cgd 	}
    103   1.6  cgd 	if (*(ap = argptr) == NULL)
    104   1.1  cgd 		error("arg count");
    105   1.1  cgd 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
    106   1.1  cgd 		ifs = nullstr;
    107   1.1  cgd 	status = 0;
    108   1.1  cgd 	startword = 1;
    109   1.1  cgd 	backslash = 0;
    110   1.1  cgd 	STARTSTACKSTR(p);
    111   1.1  cgd 	for (;;) {
    112   1.1  cgd 		if (read(0, &c, 1) != 1) {
    113   1.1  cgd 			status = 1;
    114   1.1  cgd 			break;
    115   1.1  cgd 		}
    116   1.1  cgd 		if (c == '\0')
    117   1.1  cgd 			continue;
    118   1.1  cgd 		if (backslash) {
    119   1.1  cgd 			backslash = 0;
    120   1.1  cgd 			if (c != '\n')
    121   1.1  cgd 				STPUTC(c, p);
    122   1.1  cgd 			continue;
    123   1.1  cgd 		}
    124   1.1  cgd 		if (eflag && c == '\\') {
    125   1.1  cgd 			backslash++;
    126   1.1  cgd 			continue;
    127   1.1  cgd 		}
    128   1.1  cgd 		if (c == '\n')
    129   1.1  cgd 			break;
    130   1.1  cgd 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
    131   1.1  cgd 			continue;
    132   1.1  cgd 		}
    133   1.1  cgd 		startword = 0;
    134   1.1  cgd 		if (backslash && c == '\\') {
    135   1.1  cgd 			if (read(0, &c, 1) != 1) {
    136   1.1  cgd 				status = 1;
    137   1.1  cgd 				break;
    138   1.1  cgd 			}
    139   1.1  cgd 			STPUTC(c, p);
    140   1.1  cgd 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
    141   1.1  cgd 			STACKSTRNUL(p);
    142   1.1  cgd 			setvar(*ap, stackblock(), 0);
    143   1.1  cgd 			ap++;
    144   1.1  cgd 			startword = 1;
    145   1.1  cgd 			STARTSTACKSTR(p);
    146   1.1  cgd 		} else {
    147   1.1  cgd 			STPUTC(c, p);
    148   1.1  cgd 		}
    149   1.1  cgd 	}
    150   1.1  cgd 	STACKSTRNUL(p);
    151   1.1  cgd 	setvar(*ap, stackblock(), 0);
    152   1.1  cgd 	while (*++ap != NULL)
    153   1.1  cgd 		setvar(*ap, nullstr, 0);
    154   1.1  cgd 	return status;
    155   1.1  cgd }
    156   1.1  cgd 
    157   1.1  cgd 
    158   1.1  cgd 
    159  1.12  cgd int
    160  1.12  cgd umaskcmd(argc, argv)
    161  1.12  cgd 	int argc;
    162  1.12  cgd 	char **argv;
    163  1.12  cgd {
    164   1.8  jtc 	char *ap;
    165   1.1  cgd 	int mask;
    166   1.1  cgd 	int i;
    167   1.8  jtc 	int symbolic_mode = 0;
    168   1.8  jtc 
    169   1.8  jtc 	while ((i = nextopt("S")) != '\0') {
    170   1.8  jtc 		symbolic_mode = 1;
    171   1.8  jtc 	}
    172   1.1  cgd 
    173   1.8  jtc 	INTOFF;
    174   1.8  jtc 	mask = umask(0);
    175   1.8  jtc 	umask(mask);
    176   1.8  jtc 	INTON;
    177   1.8  jtc 
    178   1.8  jtc 	if ((ap = *argptr) == NULL) {
    179   1.8  jtc 		if (symbolic_mode) {
    180   1.8  jtc 			char u[4], g[4], o[4];
    181   1.8  jtc 
    182   1.8  jtc 			i = 0;
    183   1.8  jtc 			if ((mask & S_IRUSR) == 0)
    184   1.8  jtc 				u[i++] = 'r';
    185   1.8  jtc 			if ((mask & S_IWUSR) == 0)
    186   1.8  jtc 				u[i++] = 'w';
    187   1.8  jtc 			if ((mask & S_IXUSR) == 0)
    188   1.8  jtc 				u[i++] = 'x';
    189   1.8  jtc 			u[i] = '\0';
    190   1.8  jtc 
    191   1.8  jtc 			i = 0;
    192   1.8  jtc 			if ((mask & S_IRGRP) == 0)
    193   1.8  jtc 				g[i++] = 'r';
    194   1.8  jtc 			if ((mask & S_IWGRP) == 0)
    195   1.8  jtc 				g[i++] = 'w';
    196   1.8  jtc 			if ((mask & S_IXGRP) == 0)
    197   1.8  jtc 				g[i++] = 'x';
    198   1.8  jtc 			g[i] = '\0';
    199   1.8  jtc 
    200   1.8  jtc 			i = 0;
    201   1.8  jtc 			if ((mask & S_IROTH) == 0)
    202   1.8  jtc 				o[i++] = 'r';
    203   1.8  jtc 			if ((mask & S_IWOTH) == 0)
    204   1.8  jtc 				o[i++] = 'w';
    205   1.8  jtc 			if ((mask & S_IXOTH) == 0)
    206   1.8  jtc 				o[i++] = 'x';
    207   1.8  jtc 			o[i] = '\0';
    208   1.8  jtc 
    209   1.8  jtc 			out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
    210   1.8  jtc 		} else {
    211   1.8  jtc 			out1fmt("%.4o\n", mask);
    212   1.8  jtc 		}
    213   1.1  cgd 	} else {
    214   1.8  jtc 		if (isdigit(*ap)) {
    215   1.8  jtc 			mask = 0;
    216   1.8  jtc 			do {
    217   1.8  jtc 				if (*ap >= '8' || *ap < '0')
    218   1.8  jtc 					error("Illegal number: %s", argv[1]);
    219   1.8  jtc 				mask = (mask << 3) + (*ap - '0');
    220   1.8  jtc 			} while (*++ap != '\0');
    221   1.8  jtc 			umask(mask);
    222   1.8  jtc 		} else {
    223   1.8  jtc 			void *set;
    224   1.8  jtc 			if ((set = setmode (ap)) == 0)
    225   1.8  jtc 					error("Illegal number: %s", ap);
    226   1.8  jtc 
    227   1.8  jtc 			mask = getmode (set, ~mask & 0777);
    228   1.8  jtc 			umask(~mask & 0777);
    229   1.8  jtc 		}
    230   1.1  cgd 	}
    231   1.1  cgd 	return 0;
    232   1.1  cgd }
    233