miscbltin.c revision 3044
1214152Sed/*-
2214152Sed * Copyright (c) 1991, 1993
3214152Sed *	The Regents of the University of California.  All rights reserved.
4214152Sed *
5222656Sed * This code is derived from software contributed to Berkeley by
6222656Sed * Kenneth Almquist.
7214152Sed *
8214152Sed * Redistribution and use in source and binary forms, with or without
9214152Sed * modification, are permitted provided that the following conditions
10214152Sed * are met:
11214152Sed * 1. Redistributions of source code must retain the above copyright
12214152Sed *    notice, this list of conditions and the following disclaimer.
13214152Sed * 2. Redistributions in binary form must reproduce the above copyright
14214152Sed *    notice, this list of conditions and the following disclaimer in the
15214152Sed *    documentation and/or other materials provided with the distribution.
16214152Sed * 3. All advertising materials mentioning features or use of this software
17214152Sed *    must display the following acknowledgement:
18214152Sed *	This product includes software developed by the University of
19222656Sed *	California, Berkeley and its contributors.
20222656Sed * 4. Neither the name of the University nor the names of its contributors
21239138Sandrew *    may be used to endorse or promote products derived from this software
22222656Sed *    without specific prior written permission.
23214152Sed *
24214152Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25214152Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26214152Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27214152Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28214152Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29214152Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30214152Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31214152Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32214152Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33214152Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34214152Sed * SUCH DAMAGE.
35214152Sed *
36214152Sed *	$Id$
37214152Sed */
38214152Sed
39214152Sed#ifndef lint
40214152Sedstatic char sccsid[] = "@(#)miscbltin.c	8.2 (Berkeley) 4/16/94";
41214152Sed#endif /* not lint */
42214152Sed
43214152Sed/*
44214152Sed * Miscelaneous builtins.
45214152Sed */
46214152Sed
47214152Sed#include "shell.h"
48214152Sed#include "options.h"
49214152Sed#include "var.h"
50214152Sed#include "output.h"
51214152Sed#include "memalloc.h"
52214152Sed#include "error.h"
53214152Sed#include "mystring.h"
54214152Sed
55214152Sed#undef eflag
56214152Sed
57214152Sedextern char **argptr;		/* argument list for builtin command */
58214152Sed
59
60/*
61 * The read builtin.  The -e option causes backslashes to escape the
62 * following character.
63 *
64 * This uses unbuffered input, which may be avoidable in some cases.
65 */
66
67readcmd(argc, argv)  char **argv; {
68	char **ap;
69	int backslash;
70	char c;
71	int eflag;
72	char *prompt;
73	char *ifs;
74	char *p;
75	int startword;
76	int status;
77	int i;
78
79	eflag = 0;
80	prompt = NULL;
81	while ((i = nextopt("ep:")) != '\0') {
82		if (i == 'p')
83			prompt = optarg;
84		else
85			eflag = 1;
86	}
87	if (prompt && isatty(0)) {
88		out2str(prompt);
89		flushall();
90	}
91	if (*(ap = argptr) == NULL)
92		error("arg count");
93	if ((ifs = bltinlookup("IFS", 1)) == NULL)
94		ifs = nullstr;
95	status = 0;
96	startword = 1;
97	backslash = 0;
98	STARTSTACKSTR(p);
99	for (;;) {
100		if (read(0, &c, 1) != 1) {
101			status = 1;
102			break;
103		}
104		if (c == '\0')
105			continue;
106		if (backslash) {
107			backslash = 0;
108			if (c != '\n')
109				STPUTC(c, p);
110			continue;
111		}
112		if (eflag && c == '\\') {
113			backslash++;
114			continue;
115		}
116		if (c == '\n')
117			break;
118		if (startword && *ifs == ' ' && strchr(ifs, c)) {
119			continue;
120		}
121		startword = 0;
122		if (backslash && c == '\\') {
123			if (read(0, &c, 1) != 1) {
124				status = 1;
125				break;
126			}
127			STPUTC(c, p);
128		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
129			STACKSTRNUL(p);
130			setvar(*ap, stackblock(), 0);
131			ap++;
132			startword = 1;
133			STARTSTACKSTR(p);
134		} else {
135			STPUTC(c, p);
136		}
137	}
138	STACKSTRNUL(p);
139	setvar(*ap, stackblock(), 0);
140	while (*++ap != NULL)
141		setvar(*ap, nullstr, 0);
142	return status;
143}
144
145
146
147umaskcmd(argc, argv)  char **argv; {
148	int mask;
149	char *p;
150	int i;
151
152	if ((p = argv[1]) == NULL) {
153		INTOFF;
154		mask = umask(0);
155		umask(mask);
156		INTON;
157		out1fmt("%.4o\n", mask);	/* %#o might be better */
158	} else {
159		mask = 0;
160		do {
161			if ((unsigned)(i = *p - '0') >= 8)
162				error("Illegal number: %s", argv[1]);
163			mask = (mask << 3) + i;
164		} while (*++p != '\0');
165		umask(mask);
166	}
167	return 0;
168}
169