1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 40 #endif 41 static const char rcsid[] = 42 "$Id: miscbltin.c,v 1.17 1998/08/25 09:33:34 cracauer Exp $"; 43 #endif /* not lint */ 44 45 /* 46 * Miscelaneous builtins. 47 */ 48 49 #include <sys/types.h> 50 #include <sys/stat.h> 51 #include <sys/time.h> 52 #include <sys/resource.h> 53 #include <unistd.h> 54 #include <ctype.h> 55 #include <errno.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <termios.h> 59 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 68 #undef eflag 69 70 extern char **argptr; /* argument list for builtin command */ 71 72 73 /* 74 * The read builtin. The -e option causes backslashes to escape the 75 * following character. 76 * 77 * This uses unbuffered input, which may be avoidable in some cases. 78 */ 79 80 int 81 readcmd(argc, argv) 82 int argc __unused; 83 char **argv __unused; 84 { 85 char **ap; 86 int backslash; 87 char c; 88 int eflag; 89 char *prompt; 90 char *ifs; 91 char *p; 92 int startword; 93 int status; 94 int i; 95 struct timeval tv; 96 char *tvptr; 97 fd_set ifds; 98 struct termios told, tnew; 99 int tsaved; 100 101 eflag = 0; 102 prompt = NULL; 103 tv.tv_sec = -1; 104 tv.tv_usec = 0; 105 while ((i = nextopt("ep:t:")) != '\0') { 106 switch(i) { 107 case 'p': 108 prompt = optarg; 109 break; 110 case 'e': 111 eflag = 1; 112 break; 113 case 't': 114 tv.tv_sec = strtol(optarg, &tvptr, 0); 115 if (tvptr == optarg) 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"); 129 } 130 break; 131 } 132 } 133 if (prompt && isatty(0)) { 134 out2str(prompt); 135 flushall(); 136 } 137 if (*(ap = argptr) == NULL) 138 error("arg count"); 139 if ((ifs = bltinlookup("IFS", 1)) == NULL) 140 ifs = nullstr; 141 142 if (tv.tv_sec >= 0) { 143 /* 144 * See if we can disable input processing; this will 145 * not give the desired result if we are in a pipeline 146 * and someone upstream is still in line-by-line mode. 147 */ 148 tsaved = 0; 149 if (tcgetattr(0, &told) == 0) { 150 memcpy(&tnew, &told, sizeof(told)); 151 cfmakeraw(&tnew); 152 tcsetattr(0, TCSANOW, &tnew); 153 tsaved = 1; 154 } 155 /* 156 * Wait for something to become available. 157 */ 158 FD_ZERO(&ifds); 159 FD_SET(0, &ifds); 160 status = select(1, &ifds, NULL, NULL, &tv); 161 if (tsaved) 162 tcsetattr(0, TCSANOW, &told); 163 /* 164 * If there's nothing ready, return an error. 165 */ 166 if (status <= 0) 167 return(1); 168 } 169 170 status = 0; 171 startword = 1; 172 backslash = 0; 173 STARTSTACKSTR(p); 174 for (;;) { 175 if (read(0, &c, 1) != 1) { 176 status = 1; 177 break; 178 } 179 if (c == '\0') 180 continue; 181 if (backslash) { 182 backslash = 0; 183 if (c != '\n') 184 STPUTC(c, p); 185 continue; 186 } 187 if (eflag && c == '\\') { 188 backslash++; 189 continue; 190 } 191 if (c == '\n') 192 break; 193 if (startword && *ifs == ' ' && strchr(ifs, c)) { 194 continue; 195 } 196 startword = 0; 197 if (backslash && c == '\\') { 198 if (read(0, &c, 1) != 1) { 199 status = 1; 200 break; 201 } 202 STPUTC(c, p); 203 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 204 STACKSTRNUL(p); 205 setvar(*ap, stackblock(), 0); 206 ap++; 207 startword = 1; 208 STARTSTACKSTR(p); 209 } else { 210 STPUTC(c, p); 211 } 212 } 213 STACKSTRNUL(p); 214 setvar(*ap, stackblock(), 0); 215 while (*++ap != NULL) 216 setvar(*ap, nullstr, 0); 217 return status; 218 } 219 220 221 222 int 223 umaskcmd(argc, argv) 224 int argc __unused; 225 char **argv; 226 { 227 char *ap; 228 int mask; 229 int i; 230 int symbolic_mode = 0; 231 232 while ((i = nextopt("S")) != '\0') { 233 symbolic_mode = 1; 234 } 235 236 INTOFF; 237 mask = umask(0); 238 umask(mask); 239 INTON; 240 241 if ((ap = *argptr) == NULL) { 242 if (symbolic_mode) { 243 char u[4], g[4], o[4]; 244 245 i = 0; 246 if ((mask & S_IRUSR) == 0) 247 u[i++] = 'r'; 248 if ((mask & S_IWUSR) == 0) 249 u[i++] = 'w'; 250 if ((mask & S_IXUSR) == 0) 251 u[i++] = 'x'; 252 u[i] = '\0'; 253 254 i = 0; 255 if ((mask & S_IRGRP) == 0) 256 g[i++] = 'r'; 257 if ((mask & S_IWGRP) == 0) 258 g[i++] = 'w'; 259 if ((mask & S_IXGRP) == 0) 260 g[i++] = 'x'; 261 g[i] = '\0'; 262 263 i = 0; 264 if ((mask & S_IROTH) == 0) 265 o[i++] = 'r'; 266 if ((mask & S_IWOTH) == 0) 267 o[i++] = 'w'; 268 if ((mask & S_IXOTH) == 0) 269 o[i++] = 'x'; 270 o[i] = '\0'; 271 272 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 273 } else { 274 out1fmt("%.4o\n", mask); 275 } 276 } else { 277 if (isdigit(*ap)) { 278 mask = 0; 279 do { 280 if (*ap >= '8' || *ap < '0') 281 error("Illegal number: %s", argv[1]); 282 mask = (mask << 3) + (*ap - '0'); 283 } while (*++ap != '\0'); 284 umask(mask); 285 } else { 286 void *set; 287 if ((set = setmode (ap)) == 0) 288 error("Illegal number: %s", ap); 289 290 mask = getmode (set, ~mask & 0777); 291 umask(~mask & 0777); 292 free(set); 293 } 294 } 295 return 0; 296 } 297 298 /* 299 * ulimit builtin 300 * 301 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 302 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 303 * ash by J.T. Conklin. 304 * 305 * Public domain. 306 */ 307 308 struct limits { 309 const char *name; 310 const char *units; 311 int cmd; 312 int factor; /* multiply by to get rlim_{cur,max} values */ 313 char option; 314 }; 315 316 static const struct limits limits[] = { 317 #ifdef RLIMIT_CPU 318 { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 319 #endif 320 #ifdef RLIMIT_FSIZE 321 { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 322 #endif 323 #ifdef RLIMIT_DATA 324 { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 325 #endif 326 #ifdef RLIMIT_STACK 327 { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 328 #endif 329 #ifdef RLIMIT_CORE 330 { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 331 #endif 332 #ifdef RLIMIT_RSS 333 { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 334 #endif 335 #ifdef RLIMIT_MEMLOCK 336 { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 337 #endif 338 #ifdef RLIMIT_NPROC 339 { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 340 #endif 341 #ifdef RLIMIT_NOFILE 342 { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 343 #endif 344 #ifdef RLIMIT_VMEM 345 { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 346 #endif 347 #ifdef RLIMIT_SWAP 348 { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 349 #endif 350 { (char *) 0, (char *)0, 0, 0, '\0' } 351 }; 352 353 int 354 ulimitcmd(argc, argv) 355 int argc __unused; 356 char **argv __unused; 357 { 358 int c; 359 quad_t val = 0; 360 enum { SOFT = 0x1, HARD = 0x2 } 361 how = SOFT | HARD; 362 const struct limits *l; 363 int set, all = 0; 364 int optc, what; 365 struct rlimit limit; 366 367 what = 'f'; 368 while ((optc = nextopt("HSatfdsmcnul")) != '\0') 369 switch (optc) { 370 case 'H': 371 how = HARD; 372 break; 373 case 'S': 374 how = SOFT; 375 break; 376 case 'a': 377 all = 1; 378 break; 379 default: 380 what = optc; 381 } 382 383 for (l = limits; l->name && l->option != what; l++) 384 ; 385 if (!l->name) 386 error("ulimit: internal error (%c)", what); 387 388 set = *argptr ? 1 : 0; 389 if (set) { 390 char *p = *argptr; 391 392 if (all || argptr[1]) 393 error("ulimit: too many arguments"); 394 if (strcmp(p, "unlimited") == 0) 395 val = RLIM_INFINITY; 396 else { 397 val = (quad_t) 0; 398 399 while ((c = *p++) >= '0' && c <= '9') 400 { 401 val = (val * 10) + (long)(c - '0'); 402 if (val < (quad_t) 0) 403 break; 404 } 405 if (c) 406 error("ulimit: bad number"); 407 val *= l->factor; 408 } 409 } 410 if (all) { 411 for (l = limits; l->name; l++) { 412 char optbuf[40]; 413 if (getrlimit(l->cmd, &limit) < 0) 414 error("ulimit: can't get limit: %s", strerror(errno)); 415 if (how & SOFT) 416 val = limit.rlim_cur; 417 else if (how & HARD) 418 val = limit.rlim_max; 419 420 if (l->units) 421 snprintf(optbuf, sizeof(optbuf), 422 "(%s, -%c) ", l->units, l->option); 423 else 424 snprintf(optbuf, sizeof(optbuf), 425 "(-%c) ", l->option); 426 out1fmt("%-18s %18s ", l->name, optbuf); 427 if (val == RLIM_INFINITY) 428 out1fmt("unlimited\n"); 429 else 430 { 431 val /= l->factor; 432 out1fmt("%qd\n", (quad_t) val); 433 } 434 } 435 return 0; 436 } 437 438 if (getrlimit(l->cmd, &limit) < 0) 439 error("ulimit: can't get limit: %s", strerror(errno)); 440 if (set) { 441 if (how & SOFT) 442 limit.rlim_cur = val; 443 if (how & HARD) 444 limit.rlim_max = val; 445 if (setrlimit(l->cmd, &limit) < 0) 446 error("ulimit: bad limit: %s", strerror(errno)); 447 } else { 448 if (how & SOFT) 449 val = limit.rlim_cur; 450 else if (how & HARD) 451 val = limit.rlim_max; 452 453 if (val == RLIM_INFINITY) 454 out1fmt("unlimited\n"); 455 else 456 { 457 val /= l->factor; 458 out1fmt("%qd\n", (quad_t) val); 459 } 460 } 461 return 0; 462 } 463