VirtualBox

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

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

keywords.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 9.4 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#if 0
36#ifndef lint
37static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
38#else
39__RCSID("$NetBSD: miscbltin.c,v 1.35 2005/03/19 14:22:50 dsl Exp $");
40#endif /* not lint */
41#endif
42
43/*
44 * Miscelaneous builtins.
45 */
46
47#include <sys/types.h>
48#include <stdlib.h>
49#include <ctype.h>
50#include <errno.h>
51
52#include "shell.h"
53#include "options.h"
54#include "var.h"
55#include "output.h"
56#include "memalloc.h"
57#include "error.h"
58#include "miscbltin.h"
59#include "mystring.h"
60#include "shinstance.h"
61
62#undef rflag
63
64void *bsd_setmode(shinstance *psh, const char *p);
65mode_t bsd_getmode(const void *bbox, mode_t omode);
66
67
68/*
69 * The read builtin.
70 * Backslahes escape the next char unless -r is specified.
71 *
72 * This uses unbuffered input, which may be avoidable in some cases.
73 *
74 * Note that if IFS=' :' then read x y should work so that:
75 * 'a b' x='a', y='b'
76 * ' a b ' x='a', y='b'
77 * ':b' x='', y='b'
78 * ':' x='', y=''
79 * '::' x='', y=''
80 * ': :' x='', y=''
81 * ':::' x='', y='::'
82 * ':b c:' x='', y='b c:'
83 */
84
85int
86readcmd(shinstance *psh, int argc, char **argv)
87{
88 char **ap;
89 char c;
90 int rflag;
91 char *prompt;
92 const char *ifs;
93 char *p;
94 int startword;
95 int status;
96 int i;
97 int is_ifs;
98 int saveall = 0;
99
100 rflag = 0;
101 prompt = NULL;
102 while ((i = nextopt(psh, "p:r")) != '\0') {
103 if (i == 'p')
104 prompt = psh->optionarg;
105 else
106 rflag = 1;
107 }
108
109 if (prompt && isatty(0)) {
110 out2str(psh, prompt);
111 output_flushall(psh);
112 }
113
114 if (*(ap = psh->argptr) == NULL)
115 error(psh, "arg count");
116
117 if ((ifs = bltinlookup(psh, "IFS", 1)) == NULL)
118 ifs = " \t\n";
119
120 status = 0;
121 startword = 2;
122 STARTSTACKSTR(psh, p);
123 for (;;) {
124 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
125 status = 1;
126 break;
127 }
128 if (c == '\0')
129 continue;
130 if (c == '\\' && !rflag) {
131 if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
132 status = 1;
133 break;
134 }
135 if (c != '\n')
136 STPUTC(psh, c, p);
137 continue;
138 }
139 if (c == '\n')
140 break;
141 if (strchr(ifs, c))
142 is_ifs = strchr(" \t\n", c) ? 1 : 2;
143 else
144 is_ifs = 0;
145
146 if (startword != 0) {
147 if (is_ifs == 1) {
148 /* Ignore leading IFS whitespace */
149 if (saveall)
150 STPUTC(psh, c, p);
151 continue;
152 }
153 if (is_ifs == 2 && startword == 1) {
154 /* Only one non-whitespace IFS per word */
155 startword = 2;
156 if (saveall)
157 STPUTC(psh, c, p);
158 continue;
159 }
160 }
161
162 if (is_ifs == 0) {
163 /* append this character to the current variable */
164 startword = 0;
165 if (saveall)
166 /* Not just a spare terminator */
167 saveall++;
168 STPUTC(psh, c, p);
169 continue;
170 }
171
172 /* end of variable... */
173 startword = is_ifs;
174
175 if (ap[1] == NULL) {
176 /* Last variable needs all IFS chars */
177 saveall++;
178 STPUTC(psh, c, p);
179 continue;
180 }
181
182 STACKSTRNUL(psh, p);
183 setvar(psh, *ap, stackblock(psh), 0);
184 ap++;
185 STARTSTACKSTR(psh, p);
186 }
187 STACKSTRNUL(psh, p);
188
189 /* Remove trailing IFS chars */
190 for (; stackblock(psh) <= --p; *p = 0) {
191 if (!strchr(ifs, *p))
192 break;
193 if (strchr(" \t\n", *p))
194 /* Always remove whitespace */
195 continue;
196 if (saveall > 1)
197 /* Don't remove non-whitespace unless it was naked */
198 break;
199 }
200 setvar(psh, *ap, stackblock(psh), 0);
201
202 /* Set any remaining args to "" */
203 while (*++ap != NULL)
204 setvar(psh, *ap, nullstr, 0);
205 return status;
206}
207
208
209
210int
211umaskcmd(shinstance *psh, int argc, char **argv)
212{
213 char *ap;
214 int mask;
215 int i;
216 int symbolic_mode = 0;
217
218 while ((i = nextopt(psh, "S")) != '\0') {
219 symbolic_mode = 1;
220 }
221
222 INTOFF;
223 mask = umask(0);
224 umask(mask);
225 INTON;
226
227 if ((ap = *psh->argptr) == NULL) {
228 if (symbolic_mode) {
229 char u[4], g[4], o[4];
230
231 i = 0;
232 if ((mask & S_IRUSR) == 0)
233 u[i++] = 'r';
234 if ((mask & S_IWUSR) == 0)
235 u[i++] = 'w';
236 if ((mask & S_IXUSR) == 0)
237 u[i++] = 'x';
238 u[i] = '\0';
239
240 i = 0;
241 if ((mask & S_IRGRP) == 0)
242 g[i++] = 'r';
243 if ((mask & S_IWGRP) == 0)
244 g[i++] = 'w';
245 if ((mask & S_IXGRP) == 0)
246 g[i++] = 'x';
247 g[i] = '\0';
248
249 i = 0;
250 if ((mask & S_IROTH) == 0)
251 o[i++] = 'r';
252 if ((mask & S_IWOTH) == 0)
253 o[i++] = 'w';
254 if ((mask & S_IXOTH) == 0)
255 o[i++] = 'x';
256 o[i] = '\0';
257
258 out1fmt(psh, "u=%s,g=%s,o=%s\n", u, g, o);
259 } else {
260 out1fmt(psh, "%.4o\n", mask);
261 }
262 } else {
263 if (isdigit((unsigned char)*ap)) {
264 mask = 0;
265 do {
266 if (*ap >= '8' || *ap < '0')
267 error(psh, "Illegal number: %s", argv[1]);
268 mask = (mask << 3) + (*ap - '0');
269 } while (*++ap != '\0');
270 umask(mask);
271 } else {
272 void *set;
273
274 INTOFF;
275 if ((set = bsd_setmode(psh, ap)) != 0) {
276 mask = bsd_getmode(set, ~mask & 0777);
277 ckfree(set);
278 }
279 INTON;
280 if (!set)
281 error(psh, "Illegal mode: %s", ap);
282
283 umask(~mask & 0777);
284 }
285 }
286 return 0;
287}
288
289/*
290 * ulimit builtin
291 *
292 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
293 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
294 * ash by J.T. Conklin.
295 *
296 * Public domain.
297 */
298
299struct limits {
300 const char *name;
301 int cmd;
302 int factor; /* multiply by to get rlim_{cur,max} values */
303 char option;
304};
305
306static const struct limits limits[] = {
307#ifdef RLIMIT_CPU
308 { "time(seconds)", RLIMIT_CPU, 1, 't' },
309#endif
310#ifdef RLIMIT_FSIZE
311 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
312#endif
313#ifdef RLIMIT_DATA
314 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
315#endif
316#ifdef RLIMIT_STACK
317 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
318#endif
319#ifdef RLIMIT_CORE
320 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
321#endif
322#ifdef RLIMIT_RSS
323 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
324#endif
325#ifdef RLIMIT_MEMLOCK
326 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' },
327#endif
328#ifdef RLIMIT_NPROC
329 { "process(processes)", RLIMIT_NPROC, 1, 'p' },
330#endif
331#ifdef RLIMIT_NOFILE
332 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
333#endif
334#ifdef RLIMIT_VMEM
335 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
336#endif
337#ifdef RLIMIT_SWAP
338 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
339#endif
340#ifdef RLIMIT_SBSIZE
341 { "sbsize(bytes)", RLIMIT_SBSIZE, 1, 'b' },
342#endif
343 { (char *) 0, 0, 0, '\0' }
344};
345
346int
347ulimitcmd(shinstance *psh, int argc, char **argv)
348{
349 int c;
350 shrlim_t val = 0;
351 enum { SOFT = 0x1, HARD = 0x2 }
352 how = SOFT | HARD;
353 const struct limits *l;
354 int set, all = 0;
355 int optc, what;
356 shrlimit limit;
357
358 what = 'f';
359 while ((optc = nextopt(psh, "HSabtfdsmcnpl")) != '\0')
360 switch (optc) {
361 case 'H':
362 how = HARD;
363 break;
364 case 'S':
365 how = SOFT;
366 break;
367 case 'a':
368 all = 1;
369 break;
370 default:
371 what = optc;
372 }
373
374 for (l = limits; l->name && l->option != what; l++)
375 ;
376 if (!l->name)
377 error(psh, "internal error (%c)", what);
378
379 set = *psh->argptr ? 1 : 0;
380 if (set) {
381 char *p = *psh->argptr;
382
383 if (all || psh->argptr[1])
384 error(psh, "too many arguments");
385 if (strcmp(p, "unlimited") == 0)
386 val = RLIM_INFINITY;
387 else {
388 val = (shrlim_t) 0;
389
390 while ((c = *p++) >= '0' && c <= '9')
391 {
392 val = (val * 10) + (long)(c - '0');
393 if (val < (shrlim_t) 0)
394 break;
395 }
396 if (c)
397 error(psh, "bad number");
398 val *= l->factor;
399 }
400 }
401 if (all) {
402 for (l = limits; l->name; l++) {
403 sh_getrlimit(psh, l->cmd, &limit);
404 if (how & SOFT)
405 val = limit.rlim_cur;
406 else if (how & HARD)
407 val = limit.rlim_max;
408
409 out1fmt(psh, "%-20s ", l->name);
410 if (val == RLIM_INFINITY)
411 out1fmt(psh, "unlimited\n");
412 else
413 {
414 val /= l->factor;
415 out1fmt(psh, "%lld\n", (long long) val);
416 }
417 }
418 return 0;
419 }
420
421 sh_getrlimit(psh, l->cmd, &limit);
422 if (set) {
423 if (how & HARD)
424 limit.rlim_max = val;
425 if (how & SOFT)
426 limit.rlim_cur = val;
427 if (sh_setrlimit(psh, l->cmd, &limit) < 0)
428 error(psh, "error setting limit (%s)", strerror(errno));
429 } else {
430 if (how & SOFT)
431 val = limit.rlim_cur;
432 else if (how & HARD)
433 val = limit.rlim_max;
434
435 if (val == RLIM_INFINITY)
436 out1fmt(psh, "unlimited\n");
437 else
438 {
439 val /= l->factor;
440 out1fmt(psh, "%lld\n", (long long) val);
441 }
442 }
443 return 0;
444}
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