VirtualBox

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

Last change on this file since 3387 was 3240, checked in by bird, 5 years ago

kash: GCC warnings.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use