[626] | 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 |
|
---|
[1214] | 35 | #if 0
|
---|
[626] | 36 | #ifndef lint
|
---|
| 37 | static 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 $");
|
---|
[1214] | 40 | #endif /* not lint */
|
---|
[626] | 41 | #endif
|
---|
| 42 |
|
---|
| 43 | /*
|
---|
| 44 | * Miscelaneous builtins.
|
---|
| 45 | */
|
---|
| 46 |
|
---|
[1204] | 47 | #include <sys/types.h>
|
---|
[626] | 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"
|
---|
[1214] | 60 | #include "shinstance.h"
|
---|
[2290] | 61 | #include "shfile.h"
|
---|
[626] | 62 |
|
---|
| 63 | #undef rflag
|
---|
| 64 |
|
---|
[2498] | 65 | void *kash_setmode(shinstance *psh, const char *p);
|
---|
| 66 | mode_t kash_getmode(const void *bbox, mode_t omode);
|
---|
[626] | 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 |
|
---|
| 86 | int
|
---|
[1204] | 87 | readcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 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;
|
---|
[1202] | 103 | while ((i = nextopt(psh, "p:r")) != '\0') {
|
---|
[626] | 104 | if (i == 'p')
|
---|
[1204] | 105 | prompt = psh->optionarg;
|
---|
[626] | 106 | else
|
---|
| 107 | rflag = 1;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[2416] | 110 | if (prompt && shfile_isatty(&psh->fdtab, 0)) {
|
---|
[1199] | 111 | out2str(psh, prompt);
|
---|
| 112 | output_flushall(psh);
|
---|
[626] | 113 | }
|
---|
| 114 |
|
---|
[1203] | 115 | if (*(ap = psh->argptr) == NULL)
|
---|
[1199] | 116 | error(psh, "arg count");
|
---|
[626] | 117 |
|
---|
[1202] | 118 | if ((ifs = bltinlookup(psh, "IFS", 1)) == NULL)
|
---|
[626] | 119 | ifs = " \t\n";
|
---|
| 120 |
|
---|
| 121 | status = 0;
|
---|
| 122 | startword = 2;
|
---|
[1198] | 123 | STARTSTACKSTR(psh, p);
|
---|
[626] | 124 | for (;;) {
|
---|
[1207] | 125 | if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
|
---|
[626] | 126 | status = 1;
|
---|
| 127 | break;
|
---|
| 128 | }
|
---|
| 129 | if (c == '\0')
|
---|
| 130 | continue;
|
---|
| 131 | if (c == '\\' && !rflag) {
|
---|
[1207] | 132 | if (shfile_read(&psh->fdtab, 0, &c, 1) != 1) {
|
---|
[626] | 133 | status = 1;
|
---|
| 134 | break;
|
---|
| 135 | }
|
---|
| 136 | if (c != '\n')
|
---|
[1198] | 137 | STPUTC(psh, c, p);
|
---|
[626] | 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)
|
---|
[1198] | 151 | STPUTC(psh, c, p);
|
---|
[626] | 152 | continue;
|
---|
| 153 | }
|
---|
| 154 | if (is_ifs == 2 && startword == 1) {
|
---|
| 155 | /* Only one non-whitespace IFS per word */
|
---|
| 156 | startword = 2;
|
---|
| 157 | if (saveall)
|
---|
[1198] | 158 | STPUTC(psh, c, p);
|
---|
[626] | 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++;
|
---|
[1198] | 169 | STPUTC(psh, c, p);
|
---|
[626] | 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++;
|
---|
[1198] | 179 | STPUTC(psh, c, p);
|
---|
[626] | 180 | continue;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[1198] | 183 | STACKSTRNUL(psh, p);
|
---|
[1202] | 184 | setvar(psh, *ap, stackblock(psh), 0);
|
---|
[626] | 185 | ap++;
|
---|
[1198] | 186 | STARTSTACKSTR(psh, p);
|
---|
[626] | 187 | }
|
---|
[1198] | 188 | STACKSTRNUL(psh, p);
|
---|
[626] | 189 |
|
---|
| 190 | /* Remove trailing IFS chars */
|
---|
[1198] | 191 | for (; stackblock(psh) <= --p; *p = 0) {
|
---|
[626] | 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 | }
|
---|
[1202] | 201 | setvar(psh, *ap, stackblock(psh), 0);
|
---|
[626] | 202 |
|
---|
| 203 | /* Set any remaining args to "" */
|
---|
| 204 | while (*++ap != NULL)
|
---|
[1202] | 205 | setvar(psh, *ap, nullstr, 0);
|
---|
[626] | 206 | return status;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 | int
|
---|
[1204] | 212 | umaskcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 213 | {
|
---|
| 214 | char *ap;
|
---|
| 215 | int mask;
|
---|
| 216 | int i;
|
---|
| 217 | int symbolic_mode = 0;
|
---|
| 218 |
|
---|
[1202] | 219 | while ((i = nextopt(psh, "S")) != '\0') {
|
---|
[626] | 220 | symbolic_mode = 1;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[2290] | 223 | mask = shfile_get_umask(&psh->fdtab);
|
---|
[626] | 224 |
|
---|
[1203] | 225 | if ((ap = *psh->argptr) == NULL) {
|
---|
[626] | 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 |
|
---|
[1199] | 256 | out1fmt(psh, "u=%s,g=%s,o=%s\n", u, g, o);
|
---|
[626] | 257 | } else {
|
---|
[1199] | 258 | out1fmt(psh, "%.4o\n", mask);
|
---|
[626] | 259 | }
|
---|
| 260 | } else {
|
---|
| 261 | if (isdigit((unsigned char)*ap)) {
|
---|
| 262 | mask = 0;
|
---|
| 263 | do {
|
---|
| 264 | if (*ap >= '8' || *ap < '0')
|
---|
[1199] | 265 | error(psh, "Illegal number: %s", argv[1]);
|
---|
[626] | 266 | mask = (mask << 3) + (*ap - '0');
|
---|
| 267 | } while (*++ap != '\0');
|
---|
[2290] | 268 | shfile_set_umask(&psh->fdtab, mask);
|
---|
[626] | 269 | } else {
|
---|
| 270 | void *set;
|
---|
| 271 |
|
---|
| 272 | INTOFF;
|
---|
[2498] | 273 | if ((set = kash_setmode(psh, ap)) != 0) {
|
---|
| 274 | mask = kash_getmode(set, ~mask & 0777);
|
---|
[2290] | 275 | ckfree(psh, set);
|
---|
[626] | 276 | }
|
---|
| 277 | INTON;
|
---|
| 278 | if (!set)
|
---|
[1199] | 279 | error(psh, "Illegal mode: %s", ap);
|
---|
[626] | 280 |
|
---|
[2290] | 281 | shfile_set_umask(&psh->fdtab, ~mask & 0777);
|
---|
[626] | 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 |
|
---|
| 297 | struct limits {
|
---|
| 298 | const char *name;
|
---|
| 299 | int cmd;
|
---|
| 300 | int factor; /* multiply by to get rlim_{cur,max} values */
|
---|
| 301 | char option;
|
---|
| 302 | };
|
---|
| 303 |
|
---|
| 304 | static 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 |
|
---|
| 344 | int
|
---|
[1204] | 345 | ulimitcmd(shinstance *psh, int argc, char **argv)
|
---|
[626] | 346 | {
|
---|
| 347 | int c;
|
---|
[1214] | 348 | shrlim_t val = 0;
|
---|
[626] | 349 | enum { SOFT = 0x1, HARD = 0x2 }
|
---|
| 350 | how = SOFT | HARD;
|
---|
| 351 | const struct limits *l;
|
---|
| 352 | int set, all = 0;
|
---|
| 353 | int optc, what;
|
---|
[1214] | 354 | shrlimit limit;
|
---|
[626] | 355 |
|
---|
| 356 | what = 'f';
|
---|
[1202] | 357 | while ((optc = nextopt(psh, "HSabtfdsmcnpl")) != '\0')
|
---|
[626] | 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)
|
---|
[1199] | 375 | error(psh, "internal error (%c)", what);
|
---|
[626] | 376 |
|
---|
[1203] | 377 | set = *psh->argptr ? 1 : 0;
|
---|
[626] | 378 | if (set) {
|
---|
[1203] | 379 | char *p = *psh->argptr;
|
---|
[626] | 380 |
|
---|
[1203] | 381 | if (all || psh->argptr[1])
|
---|
[1199] | 382 | error(psh, "too many arguments");
|
---|
[626] | 383 | if (strcmp(p, "unlimited") == 0)
|
---|
| 384 | val = RLIM_INFINITY;
|
---|
| 385 | else {
|
---|
[1214] | 386 | val = (shrlim_t) 0;
|
---|
[626] | 387 |
|
---|
| 388 | while ((c = *p++) >= '0' && c <= '9')
|
---|
| 389 | {
|
---|
[3240] | 390 | shrlim_t const prev = val;
|
---|
[626] | 391 | val = (val * 10) + (long)(c - '0');
|
---|
[3240] | 392 | if (val < prev)
|
---|
[626] | 393 | break;
|
---|
| 394 | }
|
---|
| 395 | if (c)
|
---|
[1199] | 396 | error(psh, "bad number");
|
---|
[626] | 397 | val *= l->factor;
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 | if (all) {
|
---|
| 401 | for (l = limits; l->name; l++) {
|
---|
[1214] | 402 | sh_getrlimit(psh, l->cmd, &limit);
|
---|
[626] | 403 | if (how & SOFT)
|
---|
| 404 | val = limit.rlim_cur;
|
---|
| 405 | else if (how & HARD)
|
---|
| 406 | val = limit.rlim_max;
|
---|
| 407 |
|
---|
[1199] | 408 | out1fmt(psh, "%-20s ", l->name);
|
---|
[626] | 409 | if (val == RLIM_INFINITY)
|
---|
[1204] | 410 | out1fmt(psh, "unlimited\n");
|
---|
[626] | 411 | else
|
---|
| 412 | {
|
---|
| 413 | val /= l->factor;
|
---|
[1199] | 414 | out1fmt(psh, "%lld\n", (long long) val);
|
---|
[626] | 415 | }
|
---|
| 416 | }
|
---|
| 417 | return 0;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
[1214] | 420 | sh_getrlimit(psh, l->cmd, &limit);
|
---|
[626] | 421 | if (set) {
|
---|
| 422 | if (how & HARD)
|
---|
| 423 | limit.rlim_max = val;
|
---|
| 424 | if (how & SOFT)
|
---|
| 425 | limit.rlim_cur = val;
|
---|
[1214] | 426 | if (sh_setrlimit(psh, l->cmd, &limit) < 0)
|
---|
[2648] | 427 | error(psh, "error setting limit (%s)", sh_strerror(psh, errno));
|
---|
[626] | 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)
|
---|
[1199] | 435 | out1fmt(psh, "unlimited\n");
|
---|
[626] | 436 | else
|
---|
| 437 | {
|
---|
| 438 | val /= l->factor;
|
---|
[1199] | 439 | out1fmt(psh, "%lld\n", (long long) val);
|
---|
[626] | 440 | }
|
---|
| 441 | }
|
---|
| 442 | return 0;
|
---|
| 443 | }
|
---|