Create SFFS library out of HGFS
[minix.git] / commands / ash / miscbltin.c
blobd5556782890454dd964813a139ca008c9364cbc2
1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/miscbltin.c,v 1.30 2004/04/06 20:06:51 markm Exp $");
44 * Miscellaneous builtins.
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <sys/select.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #include <stdint.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <termios.h>
60 #include "shell.h"
61 #include "options.h"
62 #include "var.h"
63 #include "output.h"
64 #include "memalloc.h"
65 #include "error.h"
66 #include "mystring.h"
67 #include "builtins.h"
69 #undef eflag
72 * The read builtin. The -r option causes backslashes to be treated like
73 * ordinary characters.
75 * This uses unbuffered input, which may be avoidable in some cases.
78 int
79 readcmd(int argc __unused, char **argv __unused)
81 char **ap;
82 int backslash;
83 char c;
84 int rflag;
85 char *prompt;
86 char *ifs;
87 char *p;
88 int startword;
89 int status;
90 int i;
91 struct timeval tv;
92 char *tvptr;
93 #ifndef __minix_vmd
94 fd_set ifds;
95 #endif
96 struct termios told, tnew;
97 int tsaved;
99 rflag = 0;
100 prompt = NULL;
101 tv.tv_sec = -1;
102 tv.tv_usec = 0;
103 while ((i = nextopt("erp:t:")) != '\0') {
104 switch(i) {
105 case 'p':
106 prompt = shoptarg;
107 break;
108 case 'e':
109 break;
110 case 'r':
111 rflag = 1;
112 break;
113 case 't':
114 tv.tv_sec = strtol(shoptarg, &tvptr, 0);
115 if (tvptr == shoptarg)
116 error("timeout value");
117 switch(*tvptr) {
118 case 0:
119 case 's':
120 break;
121 case 'h':
122 tv.tv_sec *= 60;
123 /* FALLTHROUGH */
124 case 'm':
125 tv.tv_sec *= 60;
126 break;
127 default:
128 error("timeout unit");
130 break;
133 if (prompt && isatty(0)) {
134 out2str(prompt);
135 flushall();
137 if (*(ap = argptr) == NULL)
138 error("arg count");
139 if ((ifs = bltinlookup("IFS", 1)) == NULL)
140 ifs = nullstr;
142 if (tv.tv_sec >= 0) {
143 #ifdef __minix
144 abort();
145 #else
147 * See if we can disable input processing; this will
148 * not give the desired result if we are in a pipeline
149 * and someone upstream is still in line-by-line mode.
151 tsaved = 0;
152 if (tcgetattr(0, &told) == 0) {
153 memcpy(&tnew, &told, sizeof(told));
154 cfmakeraw(&tnew);
155 tcsetattr(0, TCSANOW, &tnew);
156 tsaved = 1;
159 * Wait for something to become available.
161 FD_ZERO(&ifds);
162 FD_SET(0, &ifds);
163 status = select(1, &ifds, NULL, NULL, &tv);
164 if (tsaved)
165 tcsetattr(0, TCSANOW, &told);
167 * If there's nothing ready, return an error.
169 if (status <= 0)
170 return(1);
171 #endif
174 status = 0;
175 startword = 1;
176 backslash = 0;
177 STARTSTACKSTR(p);
178 for (;;) {
179 if (read(STDIN_FILENO, &c, 1) != 1) {
180 status = 1;
181 break;
183 if (c == '\0')
184 continue;
185 if (backslash) {
186 backslash = 0;
187 if (c != '\n')
188 STPUTC(c, p);
189 continue;
191 if (!rflag && c == '\\') {
192 backslash++;
193 continue;
195 if (c == '\n')
196 break;
197 if (startword && *ifs == ' ' && strchr(ifs, c)) {
198 continue;
200 startword = 0;
201 if (backslash && c == '\\') {
202 if (read(STDIN_FILENO, &c, 1) != 1) {
203 status = 1;
204 break;
206 STPUTC(c, p);
207 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
208 STACKSTRNUL(p);
209 setvar(*ap, stackblock(), 0);
210 ap++;
211 startword = 1;
212 STARTSTACKSTR(p);
213 } else {
214 STPUTC(c, p);
217 STACKSTRNUL(p);
218 setvar(*ap, stackblock(), 0);
219 while (*++ap != NULL)
220 setvar(*ap, nullstr, 0);
221 return status;
227 umaskcmd(int argc __unused, char **argv)
229 char *ap;
230 int mask;
231 int i;
232 int symbolic_mode = 0;
234 while ((i = nextopt("S")) != '\0') {
235 symbolic_mode = 1;
238 INTOFF;
239 mask = umask(0);
240 umask(mask);
241 INTON;
243 if ((ap = *argptr) == NULL) {
244 if (symbolic_mode) {
245 char u[4], g[4], o[4];
247 i = 0;
248 if ((mask & S_IRUSR) == 0)
249 u[i++] = 'r';
250 if ((mask & S_IWUSR) == 0)
251 u[i++] = 'w';
252 if ((mask & S_IXUSR) == 0)
253 u[i++] = 'x';
254 u[i] = '\0';
256 i = 0;
257 if ((mask & S_IRGRP) == 0)
258 g[i++] = 'r';
259 if ((mask & S_IWGRP) == 0)
260 g[i++] = 'w';
261 if ((mask & S_IXGRP) == 0)
262 g[i++] = 'x';
263 g[i] = '\0';
265 i = 0;
266 if ((mask & S_IROTH) == 0)
267 o[i++] = 'r';
268 if ((mask & S_IWOTH) == 0)
269 o[i++] = 'w';
270 if ((mask & S_IXOTH) == 0)
271 o[i++] = 'x';
272 o[i] = '\0';
274 out1fmt("u=%s,g=%s,o=%s\n", u, g, o);
275 } else {
276 out1fmt("%.4o\n", mask);
278 } else {
279 if (isdigit(*ap)) {
280 mask = 0;
281 do {
282 if (*ap >= '8' || *ap < '0')
283 error("Illegal number: %s", argv[1]);
284 mask = (mask << 3) + (*ap - '0');
285 } while (*++ap != '\0');
286 umask(mask);
287 } else {
288 void *set;
289 if ((set = setmode (ap)) == 0)
290 error("Illegal number: %s", ap);
292 mask = getmode (set, ~mask & 0777);
293 umask(~mask & 0777);
294 free(set);
297 return 0;
300 #ifdef __minix
301 struct rlimit
303 unsigned long rlim_cur; /* current (soft) limit */
304 unsigned long rlim_max; /* maximum value for rlim_cur */
306 #define RLIM_INFINITY (((unsigned long)1 << 31) - 1)
308 int getrlimit (int, struct rlimit *);
309 int setrlimit (int, const struct rlimit *);
311 int getrlimit(resource, rlp)
312 int resource;
313 struct rlimit *rlp;
315 errno= ENOSYS;
316 return -1;
318 int setrlimit(resource, rlp)
319 int resource;
320 const struct rlimit *rlp;
322 errno= ENOSYS;
323 return -1;
325 #endif
328 * ulimit builtin
330 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
331 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
332 * ash by J.T. Conklin.
334 * Public domain.
337 struct limits {
338 const char *name;
339 const char *units;
340 int cmd;
341 int factor; /* multiply by to get rlim_{cur,max} values */
342 char option;
345 static const struct limits limits[] = {
346 #ifdef RLIMIT_CPU
347 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' },
348 #endif
349 #ifdef RLIMIT_FSIZE
350 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' },
351 #endif
352 #ifdef RLIMIT_DATA
353 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' },
354 #endif
355 #ifdef RLIMIT_STACK
356 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' },
357 #endif
358 #ifdef RLIMIT_CORE
359 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' },
360 #endif
361 #ifdef RLIMIT_RSS
362 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' },
363 #endif
364 #ifdef RLIMIT_MEMLOCK
365 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' },
366 #endif
367 #ifdef RLIMIT_NPROC
368 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' },
369 #endif
370 #ifdef RLIMIT_NOFILE
371 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' },
372 #endif
373 #ifdef RLIMIT_VMEM
374 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' },
375 #endif
376 #ifdef RLIMIT_SWAP
377 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' },
378 #endif
379 #ifdef RLIMIT_SBSIZE
380 { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' },
381 #endif
382 { (char *) 0, (char *)0, 0, 0, '\0' }
386 ulimitcmd(int argc __unused, char **argv __unused)
388 int c;
389 intmax_t val = 0;
390 enum { SOFT = 0x1, HARD = 0x2 }
391 how = SOFT | HARD;
392 const struct limits *l;
393 int set, all = 0;
394 int optc, what;
395 struct rlimit limit;
397 what = 'f';
398 while ((optc = nextopt("HSatfdsmcnuvlb")) != '\0')
399 switch (optc) {
400 case 'H':
401 how = HARD;
402 break;
403 case 'S':
404 how = SOFT;
405 break;
406 case 'a':
407 all = 1;
408 break;
409 default:
410 what = optc;
413 for (l = limits; l->name && l->option != what; l++)
415 if (!l->name)
416 error("internal error (%c)", what);
418 set = *argptr ? 1 : 0;
419 if (set) {
420 char *p = *argptr;
422 if (all || argptr[1])
423 error("too many arguments");
424 if (strcmp(p, "unlimited") == 0)
425 val = RLIM_INFINITY;
426 else {
427 val = 0;
429 while ((c = *p++) >= '0' && c <= '9')
431 val = (val * 10) + (long)(c - '0');
432 if (val < 0)
433 break;
435 if (c)
436 error("bad number");
437 val *= l->factor;
440 if (all) {
441 for (l = limits; l->name; l++) {
442 char optbuf[40];
443 if (getrlimit(l->cmd, &limit) < 0)
444 error("can't get limit: %s", strerror(errno));
445 if (how & SOFT)
446 val = limit.rlim_cur;
447 else if (how & HARD)
448 val = limit.rlim_max;
450 if (l->units)
451 snprintf(optbuf, sizeof(optbuf),
452 "(%s, -%c) ", l->units, l->option);
453 else
454 snprintf(optbuf, sizeof(optbuf),
455 "(-%c) ", l->option);
456 out1fmt("%-18s %18s ", l->name, optbuf);
457 if (val == RLIM_INFINITY)
458 out1fmt("unlimited\n");
459 else
461 val /= l->factor;
462 out1fmt("%jd\n", (intmax_t)val);
465 return 0;
468 if (getrlimit(l->cmd, &limit) < 0)
469 error("can't get limit: %s", strerror(errno));
470 if (set) {
471 if (how & SOFT)
472 limit.rlim_cur = val;
473 if (how & HARD)
474 limit.rlim_max = val;
475 if (setrlimit(l->cmd, &limit) < 0)
476 error("bad limit: %s", strerror(errno));
477 } else {
478 if (how & SOFT)
479 val = limit.rlim_cur;
480 else if (how & HARD)
481 val = limit.rlim_max;
483 if (val == RLIM_INFINITY)
484 out1fmt("unlimited\n");
485 else
487 val /= l->factor;
488 out1fmt("%jd\n", (intmax_t)val);
491 return 0;
495 * $PchId: miscbltin.c,v 1.7 2006/05/23 11:59:08 philip Exp $