VirtualBox

source: kBuild/trunk/src/kash/miscbltin.c@ 1211

Last change on this file since 1211 was 1207, checked in by bird, 17 years ago

jobs and other stuff.

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
1/* $NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl Exp $");
43#endif
44#endif /* not lint */
45
46/*
47 * Miscelaneous builtins.
48 */
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#include <sys/time.h>
53#include <sys/resource.h>
54#include <unistd.h>
55#include <stdlib.h>
56#include <ctype.h>
57#include <errno.h>
58#ifndef _MSC_VER
59# include "mscfakes.h"
60#endif
61
62#include "shell.h"
63#include "options.h"
64#include "var.h"
65#include "output.h"
66#include "memalloc.h"
67#include "error.h"
68#include "miscbltin.h"
69#include "mystring.h"
70
71#undef rflag
72
73
74
75/*
76 * The read builtin.
77 * Backslahes escape the next char unless -r is specified.
78 *
79 * This uses unbuffered input, which may be avoidable in some cases.
80 *
81 * Note that if IFS=' :' then read x y should work so that:
82 * 'a b' x='a', y='b'
83 * ' a b ' x='a', y='b'
84 * ':b' x='', y='b'
85 * ':' x='', y=''
86 * '::' x='', y=''
87 * ': :' x='', y=''
88 * ':::' x='', y='::'
89 * ':b c:' x='', y='b c:'
90 */
91
92int
93readcmd(shinstance *psh, int argc, char **argv)
94{
95 char **ap;
96 char c;
97 int rflag;
98 char *prompt;
99 const char *ifs;
100 char *p;
101 int startword;
102 int status;
103 int i;
104 int is_ifs;
105 int saveall = 0;
106
107 rflag = 0;
108 prompt = NULL;
109 while ((i = nextopt(psh, "p:r")) != '\0') {
110 if (i == 'p')
111 prompt = psh->optionarg;
112 else
113 rflag = 1;
114 }
115
116 if (prompt && isatty(0)) {
117 out2str(psh, prompt);
118 output_flushall(psh);
119 }
120
121 if (*(ap = psh->argptr) == NULL)
122 error(psh, "arg count");
123
124 if ((ifs = bltinlookup(psh, "IFS", 1)) == NULL)
125 ifs = " \t\n";
126
127 status = 0;
128 startword = 2;
129 STARTSTACKSTR(psh, p);
130 for (;;) {
131 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
132 status = 1;
133 break;
134 }
135 if (c == '\0')
136 continue;
137 if (c == '\\' && !rflag) {
138 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
139 status = 1;
140 break;
141 }
142 if (c != '\n')
143 STPUTC(psh, c, p);
144 continue;
145 }
146 if (c == '\n')
147 break;
148 if (strchr(ifs, c))
149 is_ifs = strchr(" \t\n", c) ? 1 : 2;
150 else
151 is_ifs = 0;
152
153 if (startword != 0) {
154 if (is_ifs == 1) {
155 /* Ignore leading IFS whitespace */
156 if (saveall)
157 STPUTC(psh, c, p);
158 continue;
159 }
160 if (is_ifs == 2 && startword == 1) {
161 /* Only one non-whitespace IFS per word */
162 startword = 2;
163 if (saveall)
164 STPUTC(psh, c, p);
165 continue;
166 }
167 }
168
169 if (is_ifs == 0) {
170 /* append this character to the current variable */
171 startword = 0;
172 if (saveall)
173 /* Not just a spare terminator */
174 saveall++;
175 STPUTC(psh, c, p);
176 continue;
177 }
178
179 /* end of variable... */
180 startword = is_ifs;
181
182 if (ap[1] == NULL) {
183 /* Last variable needs all IFS chars */
184 saveall++;
185 STPUTC(psh, c, p);
186 continue;
187 }
188
189 STACKSTRNUL(psh, p);
190 setvar(psh, *ap, stackblock(psh), 0);
191 ap++;
192 STARTSTACKSTR(psh, p);
193 }
194 STACKSTRNUL(psh, p);
195
196 /* Remove trailing IFS chars */
197 for (; stackblock(psh) <= --p; *p = 0) {
198 if (!strchr(ifs, *p))
199 break;
200 if (strchr(" \t\n", *p))
201 /* Always remove whitespace */
202 continue;
203 if (saveall > 1)
204 /* Don't remove non-whitespace unless it was naked */
205 break;
206 }
207 setvar(psh, *ap, stackblock(psh), 0);
208
209 /* Set any remaining args to "" */
210 while (*++ap != NULL)
211 setvar(psh, *ap, nullstr, 0);
212 return status;
213}
214
215
216
217int
218umaskcmd(shinstance *psh, int argc, char **argv)
219{
220 char *ap;
221 int mask;
222 int i;
223 int symbolic_mode = 0;
224
225 while ((i = nextopt(psh, "S")) != '\0') {
226 symbolic_mode = 1;
227 }
228
229 INTOFF;
230 mask = umask(0);
231 umask(mask);
232 INTON;
233
234 if ((ap = *psh->argptr) == NULL) {
235 if (symbolic_mode) {
236 char u[4], g[4], o[4];
237
238 i = 0;
239 if ((mask & S_IRUSR) == 0)
240 u[i++] = 'r';
241 if ((mask & S_IWUSR) == 0)
242 u[i++] = 'w';
243 if ((mask & S_IXUSR) == 0)
244 u[i++] = 'x';
245 u[i] = '\0';
246
247 i = 0;
248 if ((mask & S_IRGRP) == 0)
249 g[i++] = 'r';
250 if ((mask & S_IWGRP) == 0)
251 g[i++] = 'w';
252 if ((mask & S_IXGRP) == 0)
253 g[i++] = 'x';
254 g[i] = '\0';
255
256 i = 0;
257 if ((mask & S_IROTH) == 0)
258 o[i++] = 'r';
259 if ((mask & S_IWOTH) == 0)
260 o[i++] = 'w';
261 if ((mask & S_IXOTH) == 0)
262 o[i++] = 'x';
263 o[i] = '\0';
264
265 out1fmt(psh, "u=%s,g=%s,o=%s\n", u, g, o);
266 } else {
267 out1fmt(psh, "%.4o\n", mask);
268 }
269 } else {
270 if (isdigit((unsigned char)*ap)) {
271 mask = 0;
272 do {
273 if (*ap >= '8' || *ap < '0')
274 error(psh, "Illegal number: %s", argv[1]);
275 mask = (mask << 3) + (*ap - '0');
276 } while (*++ap != '\0');
277 umask(mask);
278 } else {
279 void *set;
280
281 INTOFF;
282#ifdef __INNOTEK_LIBC__
283 if ((set = bsd_setmode(ap)) != 0) {
284#else
285 if ((set = setmode(ap)) != 0) {
286#endif
287 mask = getmode(set, ~mask & 0777);
288 ckfree(set);
289 }
290 INTON;
291 if (!set)
292 error(psh, "Illegal mode: %s", ap);
293
294 umask(~mask & 0777);
295 }
296 }
297 return 0;
298}
299
300/*
301 * ulimit builtin
302 *
303 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
304 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
305 * ash by J.T. Conklin.
306 *
307 * Public domain.
308 */
309
310struct limits {
311 const char *name;
312 int cmd;
313 int factor; /* multiply by to get rlim_{cur,max} values */
314 char option;
315};
316
317static const struct limits limits[] = {
318#ifdef RLIMIT_CPU
319 { "time(seconds)", RLIMIT_CPU, 1, 't' },
320#endif
321#ifdef RLIMIT_FSIZE
322 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
323#endif
324#ifdef RLIMIT_DATA
325 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
326#endif
327#ifdef RLIMIT_STACK
328 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
329#endif
330#ifdef RLIMIT_CORE
331 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
332#endif
333#ifdef RLIMIT_RSS
334 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
335#endif
336#ifdef RLIMIT_MEMLOCK
337 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
338#endif
339#ifdef RLIMIT_NPROC
340 { "process(processes)", RLIMIT_NPROC, 1, 'p' },
341#endif
342#ifdef RLIMIT_NOFILE
343 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
344#endif
345#ifdef RLIMIT_VMEM
346 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
347#endif
348#ifdef RLIMIT_SWAP
349 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
350#endif
351#ifdef RLIMIT_SBSIZE
352 { "sbsize(bytes)", RLIMIT_SBSIZE, 1, 'b' },
353#endif
354 { (char *) 0, 0, 0, '\0' }
355};
356
357int
358ulimitcmd(shinstance *psh, int argc, char **argv)
359{
360 int c;
361 rlim_t val = 0;
362 enum { SOFT = 0x1, HARD = 0x2 }
363 how = SOFT | HARD;
364 const struct limits *l;
365 int set, all = 0;
366 int optc, what;
367 struct rlimit limit;
368
369 what = 'f';
370 while ((optc = nextopt(psh, "HSabtfdsmcnpl")) != '\0')
371 switch (optc) {
372 case 'H':
373 how = HARD;
374 break;
375 case 'S':
376 how = SOFT;
377 break;
378 case 'a':
379 all = 1;
380 break;
381 default:
382 what = optc;
383 }
384
385 for (l = limits; l->name && l->option != what; l++)
386 ;
387 if (!l->name)
388 error(psh, "internal error (%c)", what);
389
390 set = *psh->argptr ? 1 : 0;
391 if (set) {
392 char *p = *psh->argptr;
393
394 if (all || psh->argptr[1])
395 error(psh, "too many arguments");
396 if (strcmp(p, "unlimited") == 0)
397 val = RLIM_INFINITY;
398 else {
399 val = (rlim_t) 0;
400
401 while ((c = *p++) >= '0' && c <= '9')
402 {
403 val = (val * 10) + (long)(c - '0');
404 if (val < (rlim_t) 0)
405 break;
406 }
407 if (c)
408 error(psh, "bad number");
409 val *= l->factor;
410 }
411 }
412 if (all) {
413 for (l = limits; l->name; l++) {
414 getrlimit(l->cmd, &limit);
415 if (how & SOFT)
416 val = limit.rlim_cur;
417 else if (how & HARD)
418 val = limit.rlim_max;
419
420 out1fmt(psh, "%-20s ", l->name);
421 if (val == RLIM_INFINITY)
422 out1fmt(psh, "unlimited\n");
423 else
424 {
425 val /= l->factor;
426 out1fmt(psh, "%lld\n", (long long) val);
427 }
428 }
429 return 0;
430 }
431
432 getrlimit(l->cmd, &limit);
433 if (set) {
434 if (how & HARD)
435 limit.rlim_max = val;
436 if (how & SOFT)
437 limit.rlim_cur = val;
438 if (setrlimit(l->cmd, &limit) < 0)
439 error(psh, "error setting limit (%s)", strerror(errno));
440 } else {
441 if (how & SOFT)
442 val = limit.rlim_cur;
443 else if (how & HARD)
444 val = limit.rlim_max;
445
446 if (val == RLIM_INFINITY)
447 out1fmt(psh, "unlimited\n");
448 else
449 {
450 val /= l->factor;
451 out1fmt(psh, "%lld\n", (long long) val);
452 }
453 }
454 return 0;
455}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette