1 /* $NetBSD: miscbltin.c,v 1.20 1997/11/05 14:05:28 kleink 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 43 #else 44 __RCSID("$NetBSD: miscbltin.c,v 1.20 1997/11/05 14:05:28 kleink Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 /* 49 * Miscelaneous builtins. 50 */ 51 52 #include <sys/types.h> 53 #include <sys/param.h> 54 #include <sys/stat.h> 55 #include <sys/time.h> 56 #include <sys/resource.h> 57 #include <unistd.h> 58 #include <ctype.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 "miscbltin.h" 67 #include "mystring.h" 68 69 #undef rflag 70 71 extern char **argptr; /* argument list for builtin command */ 72 73 74 /* 75 * The read builtin. The -e option causes backslashes to escape the 76 * following character. 77 * 78 * This uses unbuffered input, which may be avoidable in some cases. 79 */ 80 81 int 82 readcmd(argc, argv) 83 int argc; 84 char **argv; 85 { 86 char **ap; 87 int backslash; 88 char c; 89 int rflag; 90 char *prompt; 91 char *ifs; 92 char *p; 93 int startword; 94 int status; 95 int i; 96 97 rflag = 0; 98 prompt = NULL; 99 while ((i = nextopt("p:r")) != '\0') { 100 if (i == 'p') 101 prompt = optarg; 102 else 103 rflag = 1; 104 } 105 if (prompt && isatty(0)) { 106 out2str(prompt); 107 flushall(); 108 } 109 if (*(ap = argptr) == NULL) 110 error("arg count"); 111 if ((ifs = bltinlookup("IFS", 1)) == NULL) 112 ifs = nullstr; 113 status = 0; 114 startword = 1; 115 backslash = 0; 116 STARTSTACKSTR(p); 117 for (;;) { 118 if (read(0, &c, 1) != 1) { 119 status = 1; 120 break; 121 } 122 if (c == '\0') 123 continue; 124 if (backslash) { 125 backslash = 0; 126 if (c != '\n') 127 STPUTC(c, p); 128 continue; 129 } 130 if (!rflag && c == '\\') { 131 backslash++; 132 continue; 133 } 134 if (c == '\n') 135 break; 136 if (startword && *ifs == ' ' && strchr(ifs, c)) { 137 continue; 138 } 139 startword = 0; 140 if (backslash && c == '\\') { 141 if (read(0, &c, 1) != 1) { 142 status = 1; 143 break; 144 } 145 STPUTC(c, p); 146 } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 147 STACKSTRNUL(p); 148 setvar(*ap, stackblock(), 0); 149 ap++; 150 startword = 1; 151 STARTSTACKSTR(p); 152 } else { 153 STPUTC(c, p); 154 } 155 } 156 STACKSTRNUL(p); 157 setvar(*ap, stackblock(), 0); 158 while (*++ap != NULL) 159 setvar(*ap, nullstr, 0); 160 return status; 161 } 162 163 164 165 int 166 umaskcmd(argc, argv) 167 int argc; 168 char **argv; 169 { 170 char *ap; 171 int mask; 172 int i; 173 int symbolic_mode = 0; 174 175 while ((i = nextopt("S")) != '\0') { 176 symbolic_mode = 1; 177 } 178 179 INTOFF; 180 mask = umask(0); 181 umask(mask); 182 INTON; 183 184 if ((ap = *argptr) == NULL) { 185 if (symbolic_mode) { 186 char u[4], g[4], o[4]; 187 188 i = 0; 189 if ((mask & S_IRUSR) == 0) 190 u[i++] = 'r'; 191 if ((mask & S_IWUSR) == 0) 192 u[i++] = 'w'; 193 if ((mask & S_IXUSR) == 0) 194 u[i++] = 'x'; 195 u[i] = '\0'; 196 197 i = 0; 198 if ((mask & S_IRGRP) == 0) 199 g[i++] = 'r'; 200 if ((mask & S_IWGRP) == 0) 201 g[i++] = 'w'; 202 if ((mask & S_IXGRP) == 0) 203 g[i++] = 'x'; 204 g[i] = '\0'; 205 206 i = 0; 207 if ((mask & S_IROTH) == 0) 208 o[i++] = 'r'; 209 if ((mask & S_IWOTH) == 0) 210 o[i++] = 'w'; 211 if ((mask & S_IXOTH) == 0) 212 o[i++] = 'x'; 213 o[i] = '\0'; 214 215 out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 216 } else { 217 out1fmt("%.4o\n", mask); 218 } 219 } else { 220 if (isdigit(*ap)) { 221 mask = 0; 222 do { 223 if (*ap >= '8' || *ap < '0') 224 error("Illegal number: %s", argv[1]); 225 mask = (mask << 3) + (*ap - '0'); 226 } while (*++ap != '\0'); 227 umask(mask); 228 } else { 229 void *set; 230 if ((set = setmode (ap)) == 0) 231 error("Illegal number: %s", ap); 232 233 mask = getmode (set, ~mask & 0777); 234 umask(~mask & 0777); 235 } 236 } 237 return 0; 238 } 239 240 /* 241 * ulimit builtin 242 * 243 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 244 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 245 * ash by J.T. Conklin. 246 * 247 * Public domain. 248 */ 249 250 struct limits { 251 const char *name; 252 int cmd; 253 int factor; /* multiply by to get rlim_{cur,max} values */ 254 char option; 255 }; 256 257 static const struct limits limits[] = { 258 #ifdef RLIMIT_CPU 259 { "time(seconds)", RLIMIT_CPU, 1, 't' }, 260 #endif 261 #ifdef RLIMIT_FSIZE 262 { "file(blocks)", RLIMIT_FSIZE, 512, 'f' }, 263 #endif 264 #ifdef RLIMIT_DATA 265 { "data(kbytes)", RLIMIT_DATA, 1024, 'd' }, 266 #endif 267 #ifdef RLIMIT_STACK 268 { "stack(kbytes)", RLIMIT_STACK, 1024, 's' }, 269 #endif 270 #ifdef RLIMIT_CORE 271 { "coredump(blocks)", RLIMIT_CORE, 512, 'c' }, 272 #endif 273 #ifdef RLIMIT_RSS 274 { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' }, 275 #endif 276 #ifdef RLIMIT_MEMLOCK 277 { "locked memory(kbytes)", RLIMIT_MEMLOCK, 1024, 'l' }, 278 #endif 279 #ifdef RLIMIT_NPROC 280 { "process(processes)", RLIMIT_NPROC, 1, 'p' }, 281 #endif 282 #ifdef RLIMIT_NOFILE 283 { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' }, 284 #endif 285 #ifdef RLIMIT_VMEM 286 { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' }, 287 #endif 288 #ifdef RLIMIT_SWAP 289 { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' }, 290 #endif 291 { (char *) 0, 0, 0, '\0' } 292 }; 293 294 int 295 ulimitcmd(argc, argv) 296 int argc; 297 char **argv; 298 { 299 int c; 300 rlim_t val = 0; 301 enum { SOFT = 0x1, HARD = 0x2 } 302 how = SOFT | HARD; 303 const struct limits *l; 304 int set, all = 0; 305 int optc, what; 306 struct rlimit limit; 307 308 what = 'f'; 309 while ((optc = nextopt("HSatfdsmcnpl")) != '\0') 310 switch (optc) { 311 case 'H': 312 how = HARD; 313 break; 314 case 'S': 315 how = SOFT; 316 break; 317 case 'a': 318 all = 1; 319 break; 320 default: 321 what = optc; 322 } 323 324 for (l = limits; l->name && l->option != what; l++) 325 ; 326 if (!l->name) 327 error("ulimit: internal error (%c)\n", what); 328 329 set = *argptr ? 1 : 0; 330 if (set) { 331 char *p = *argptr; 332 333 if (all || argptr[1]) 334 error("ulimit: too many arguments\n"); 335 if (strcmp(p, "unlimited") == 0) 336 val = RLIM_INFINITY; 337 else { 338 val = (rlim_t) 0; 339 340 while ((c = *p++) >= '0' && c <= '9') 341 { 342 val = (val * 10) + (long)(c - '0'); 343 if (val < (rlim_t) 0) 344 break; 345 } 346 if (c) 347 error("ulimit: bad number\n"); 348 val *= l->factor; 349 } 350 } 351 if (all) { 352 for (l = limits; l->name; l++) { 353 getrlimit(l->cmd, &limit); 354 if (how & SOFT) 355 val = limit.rlim_cur; 356 else if (how & HARD) 357 val = limit.rlim_max; 358 359 out1fmt("%-20s ", l->name); 360 if (val == RLIM_INFINITY) 361 out1fmt("unlimited\n"); 362 else 363 { 364 val /= l->factor; 365 #ifdef BSD4_4 366 out1fmt("%qd\n", (quad_t) val); 367 #else 368 out1fmt("%ld\n", (long) val); 369 #endif 370 } 371 } 372 return 0; 373 } 374 375 getrlimit(l->cmd, &limit); 376 if (set) { 377 if (how & SOFT) 378 limit.rlim_cur = val; 379 if (how & HARD) 380 limit.rlim_max = val; 381 if (setrlimit(l->cmd, &limit) < 0) 382 error("ulimit: bad limit\n"); 383 } else { 384 if (how & SOFT) 385 val = limit.rlim_cur; 386 else if (how & HARD) 387 val = limit.rlim_max; 388 389 if (val == RLIM_INFINITY) 390 out1fmt("unlimited\n"); 391 else 392 { 393 val /= l->factor; 394 #ifdef BSD4_4 395 out1fmt("%qd\n", (quad_t) val); 396 #else 397 out1fmt("%ld\n", (long) val); 398 #endif 399 } 400 } 401 return 0; 402 } 403