miscbltin.c revision 59436
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 3. All advertising materials mentioning features or use of this software 171556Srgrimes * must display the following acknowledgement: 181556Srgrimes * This product includes software developed by the University of 191556Srgrimes * California, Berkeley and its contributors. 201556Srgrimes * 4. Neither the name of the University nor the names of its contributors 211556Srgrimes * may be used to endorse or promote products derived from this software 221556Srgrimes * without specific prior written permission. 231556Srgrimes * 241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341556Srgrimes * SUCH DAMAGE. 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3836150Scharnier#if 0 3936150Scharnierstatic char sccsid[] = "@(#)miscbltin.c 8.4 (Berkeley) 5/4/95"; 4036150Scharnier#endif 4136150Scharnierstatic const char rcsid[] = 4250471Speter "$FreeBSD: head/bin/sh/miscbltin.c 59436 2000-04-20 09:49:16Z cracauer $"; 431556Srgrimes#endif /* not lint */ 441556Srgrimes 451556Srgrimes/* 4646684Skris * Miscellaneous builtins. 471556Srgrimes */ 481556Srgrimes 4917987Speter#include <sys/types.h> 5017987Speter#include <sys/stat.h> 5117987Speter#include <sys/time.h> 5217987Speter#include <sys/resource.h> 5317987Speter#include <unistd.h> 5417987Speter#include <ctype.h> 5518016Speter#include <errno.h> 5618018Speter#include <stdio.h> 5738536Scracauer#include <stdlib.h> 5829983Smsmith#include <termios.h> 5917987Speter 601556Srgrimes#include "shell.h" 611556Srgrimes#include "options.h" 621556Srgrimes#include "var.h" 631556Srgrimes#include "output.h" 641556Srgrimes#include "memalloc.h" 651556Srgrimes#include "error.h" 661556Srgrimes#include "mystring.h" 671556Srgrimes 681556Srgrimes#undef eflag 691556Srgrimes 701556Srgrimes/* 7150394Stg * The read builtin. The -r option causes backslashes to be treated like 7250394Stg * ordinary characters. 731556Srgrimes * 741556Srgrimes * This uses unbuffered input, which may be avoidable in some cases. 751556Srgrimes */ 761556Srgrimes 7717987Speterint 7817987Speterreadcmd(argc, argv) 7925905Ssteve int argc __unused; 8025905Ssteve char **argv __unused; 8117987Speter{ 821556Srgrimes char **ap; 831556Srgrimes int backslash; 841556Srgrimes char c; 8550394Stg int rflag; 861556Srgrimes char *prompt; 871556Srgrimes char *ifs; 881556Srgrimes char *p; 891556Srgrimes int startword; 901556Srgrimes int status; 911556Srgrimes int i; 9229983Smsmith struct timeval tv; 9329983Smsmith char *tvptr; 9429983Smsmith fd_set ifds; 9529983Smsmith struct termios told, tnew; 9629983Smsmith int tsaved; 971556Srgrimes 9850394Stg rflag = 0; 991556Srgrimes prompt = NULL; 10029983Smsmith tv.tv_sec = -1; 10129983Smsmith tv.tv_usec = 0; 10250394Stg while ((i = nextopt("erp:t:")) != '\0') { 10329983Smsmith switch(i) { 10429983Smsmith case 'p': 10559436Scracauer prompt = shoptarg; 10629983Smsmith break; 10729983Smsmith case 'e': 10829983Smsmith break; 10950394Stg case 'r': 11050394Stg rflag = 1; 11150394Stg break; 11229983Smsmith case 't': 11359436Scracauer tv.tv_sec = strtol(shoptarg, &tvptr, 0); 11459436Scracauer if (tvptr == shoptarg) 11529983Smsmith error("timeout value"); 11629983Smsmith switch(*tvptr) { 11729983Smsmith case 0: 11829983Smsmith case 's': 11929983Smsmith break; 12029983Smsmith case 'h': 12129983Smsmith tv.tv_sec *= 60; 12229983Smsmith /* FALLTHROUGH */ 12329983Smsmith case 'm': 12429983Smsmith tv.tv_sec *= 60; 12529983Smsmith break; 12629983Smsmith default: 12729983Smsmith error("timeout unit"); 12829983Smsmith } 12929983Smsmith break; 13029983Smsmith } 1311556Srgrimes } 1321556Srgrimes if (prompt && isatty(0)) { 1331556Srgrimes out2str(prompt); 1341556Srgrimes flushall(); 1351556Srgrimes } 1361556Srgrimes if (*(ap = argptr) == NULL) 1371556Srgrimes error("arg count"); 1381556Srgrimes if ((ifs = bltinlookup("IFS", 1)) == NULL) 1391556Srgrimes ifs = nullstr; 14029983Smsmith 14129983Smsmith if (tv.tv_sec >= 0) { 14229983Smsmith /* 14329983Smsmith * See if we can disable input processing; this will 14429983Smsmith * not give the desired result if we are in a pipeline 14529983Smsmith * and someone upstream is still in line-by-line mode. 14629983Smsmith */ 14729983Smsmith tsaved = 0; 14829983Smsmith if (tcgetattr(0, &told) == 0) { 14929983Smsmith memcpy(&tnew, &told, sizeof(told)); 15029983Smsmith cfmakeraw(&tnew); 15129983Smsmith tcsetattr(0, TCSANOW, &tnew); 15229983Smsmith tsaved = 1; 15329983Smsmith } 15429983Smsmith /* 15529983Smsmith * Wait for something to become available. 15629983Smsmith */ 15729983Smsmith FD_ZERO(&ifds); 15829983Smsmith FD_SET(0, &ifds); 15929983Smsmith status = select(1, &ifds, NULL, NULL, &tv); 16029983Smsmith if (tsaved) 16129983Smsmith tcsetattr(0, TCSANOW, &told); 16229983Smsmith /* 16329983Smsmith * If there's nothing ready, return an error. 16429983Smsmith */ 16529983Smsmith if (status <= 0) 16629983Smsmith return(1); 16729983Smsmith } 16829983Smsmith 1691556Srgrimes status = 0; 1701556Srgrimes startword = 1; 1711556Srgrimes backslash = 0; 1721556Srgrimes STARTSTACKSTR(p); 1731556Srgrimes for (;;) { 1741556Srgrimes if (read(0, &c, 1) != 1) { 1751556Srgrimes status = 1; 1761556Srgrimes break; 1771556Srgrimes } 1781556Srgrimes if (c == '\0') 1791556Srgrimes continue; 1801556Srgrimes if (backslash) { 1811556Srgrimes backslash = 0; 1821556Srgrimes if (c != '\n') 1831556Srgrimes STPUTC(c, p); 1841556Srgrimes continue; 1851556Srgrimes } 18650394Stg if (!rflag && c == '\\') { 1871556Srgrimes backslash++; 1881556Srgrimes continue; 1891556Srgrimes } 1901556Srgrimes if (c == '\n') 1911556Srgrimes break; 1921556Srgrimes if (startword && *ifs == ' ' && strchr(ifs, c)) { 1931556Srgrimes continue; 1941556Srgrimes } 1951556Srgrimes startword = 0; 1961556Srgrimes if (backslash && c == '\\') { 1971556Srgrimes if (read(0, &c, 1) != 1) { 1981556Srgrimes status = 1; 1991556Srgrimes break; 2001556Srgrimes } 2011556Srgrimes STPUTC(c, p); 2021556Srgrimes } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { 2031556Srgrimes STACKSTRNUL(p); 2041556Srgrimes setvar(*ap, stackblock(), 0); 2051556Srgrimes ap++; 2061556Srgrimes startword = 1; 2071556Srgrimes STARTSTACKSTR(p); 2081556Srgrimes } else { 2091556Srgrimes STPUTC(c, p); 2101556Srgrimes } 2111556Srgrimes } 2121556Srgrimes STACKSTRNUL(p); 2131556Srgrimes setvar(*ap, stackblock(), 0); 2141556Srgrimes while (*++ap != NULL) 2151556Srgrimes setvar(*ap, nullstr, 0); 2161556Srgrimes return status; 2171556Srgrimes} 2181556Srgrimes 2191556Srgrimes 2201556Srgrimes 22117987Speterint 22217987Speterumaskcmd(argc, argv) 22325905Ssteve int argc __unused; 22420425Ssteve char **argv; 22517987Speter{ 22617987Speter char *ap; 2271556Srgrimes int mask; 2281556Srgrimes int i; 22917987Speter int symbolic_mode = 0; 2301556Srgrimes 23117987Speter while ((i = nextopt("S")) != '\0') { 23217987Speter symbolic_mode = 1; 2331556Srgrimes } 23411571Sjoerg 23517987Speter INTOFF; 23617987Speter mask = umask(0); 23717987Speter umask(mask); 23817987Speter INTON; 23911571Sjoerg 24017987Speter if ((ap = *argptr) == NULL) { 24117987Speter if (symbolic_mode) { 24217987Speter char u[4], g[4], o[4]; 24311571Sjoerg 24417987Speter i = 0; 24517987Speter if ((mask & S_IRUSR) == 0) 24617987Speter u[i++] = 'r'; 24717987Speter if ((mask & S_IWUSR) == 0) 24817987Speter u[i++] = 'w'; 24917987Speter if ((mask & S_IXUSR) == 0) 25017987Speter u[i++] = 'x'; 25117987Speter u[i] = '\0'; 25211571Sjoerg 25317987Speter i = 0; 25417987Speter if ((mask & S_IRGRP) == 0) 25517987Speter g[i++] = 'r'; 25617987Speter if ((mask & S_IWGRP) == 0) 25717987Speter g[i++] = 'w'; 25817987Speter if ((mask & S_IXGRP) == 0) 25917987Speter g[i++] = 'x'; 26017987Speter g[i] = '\0'; 26111571Sjoerg 26217987Speter i = 0; 26317987Speter if ((mask & S_IROTH) == 0) 26417987Speter o[i++] = 'r'; 26517987Speter if ((mask & S_IWOTH) == 0) 26617987Speter o[i++] = 'w'; 26717987Speter if ((mask & S_IXOTH) == 0) 26817987Speter o[i++] = 'x'; 26917987Speter o[i] = '\0'; 27011571Sjoerg 27117987Speter out1fmt("u=%s,g=%s,o=%s\n", u, g, o); 27217987Speter } else { 27317987Speter out1fmt("%.4o\n", mask); 27417987Speter } 27517987Speter } else { 27617987Speter if (isdigit(*ap)) { 27717987Speter mask = 0; 27817987Speter do { 27917987Speter if (*ap >= '8' || *ap < '0') 28017987Speter error("Illegal number: %s", argv[1]); 28117987Speter mask = (mask << 3) + (*ap - '0'); 28217987Speter } while (*++ap != '\0'); 28317987Speter umask(mask); 28417987Speter } else { 28520425Ssteve void *set; 28617987Speter if ((set = setmode (ap)) == 0) 28741844Simp error("Illegal number: %s", ap); 28811571Sjoerg 28917987Speter mask = getmode (set, ~mask & 0777); 29017987Speter umask(~mask & 0777); 29141844Simp free(set); 29217987Speter } 29317987Speter } 29411571Sjoerg return 0; 29511571Sjoerg} 29611571Sjoerg 29717987Speter/* 29817987Speter * ulimit builtin 29917987Speter * 30017987Speter * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and 30117987Speter * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with 30217987Speter * ash by J.T. Conklin. 30317987Speter * 30417987Speter * Public domain. 30517987Speter */ 30611571Sjoerg 30717987Speterstruct limits { 30817987Speter const char *name; 30918016Speter const char *units; 31017987Speter int cmd; 31117987Speter int factor; /* multiply by to get rlim_{cur,max} values */ 31217987Speter char option; 31317987Speter}; 31411571Sjoerg 31517987Speterstatic const struct limits limits[] = { 31617987Speter#ifdef RLIMIT_CPU 31718016Speter { "cpu time", "seconds", RLIMIT_CPU, 1, 't' }, 31817987Speter#endif 31917987Speter#ifdef RLIMIT_FSIZE 32018016Speter { "file size", "512-blocks", RLIMIT_FSIZE, 512, 'f' }, 32117987Speter#endif 32217987Speter#ifdef RLIMIT_DATA 32318016Speter { "data seg size", "kbytes", RLIMIT_DATA, 1024, 'd' }, 32417987Speter#endif 32517987Speter#ifdef RLIMIT_STACK 32618016Speter { "stack size", "kbytes", RLIMIT_STACK, 1024, 's' }, 32717987Speter#endif 32817987Speter#ifdef RLIMIT_CORE 32918016Speter { "core file size", "512-blocks", RLIMIT_CORE, 512, 'c' }, 33017987Speter#endif 33117987Speter#ifdef RLIMIT_RSS 33218016Speter { "max memory size", "kbytes", RLIMIT_RSS, 1024, 'm' }, 33317987Speter#endif 33417987Speter#ifdef RLIMIT_MEMLOCK 33518016Speter { "locked memory", "kbytes", RLIMIT_MEMLOCK, 1024, 'l' }, 33617987Speter#endif 33717987Speter#ifdef RLIMIT_NPROC 33818016Speter { "max user processes", (char *)0, RLIMIT_NPROC, 1, 'u' }, 33917987Speter#endif 34017987Speter#ifdef RLIMIT_NOFILE 34118016Speter { "open files", (char *)0, RLIMIT_NOFILE, 1, 'n' }, 34217987Speter#endif 34317987Speter#ifdef RLIMIT_VMEM 34418016Speter { "virtual mem size", "kbytes", RLIMIT_VMEM, 1024, 'v' }, 34517987Speter#endif 34617987Speter#ifdef RLIMIT_SWAP 34718016Speter { "swap limit", "kbytes", RLIMIT_SWAP, 1024, 'w' }, 34817987Speter#endif 34952072Sgreen#ifdef RLIMIT_SBSIZE 35052072Sgreen { "sbsize", "bytes", RLIMIT_SBSIZE, 1, 'b' }, 35152072Sgreen#endif 35218016Speter { (char *) 0, (char *)0, 0, 0, '\0' } 35317987Speter}; 35417987Speter 35517987Speterint 35617987Speterulimitcmd(argc, argv) 35725905Ssteve int argc __unused; 35825905Ssteve char **argv __unused; 35917987Speter{ 36025222Ssteve int c; 36118018Speter quad_t val = 0; 36217987Speter enum { SOFT = 0x1, HARD = 0x2 } 36317987Speter how = SOFT | HARD; 36417987Speter const struct limits *l; 36517987Speter int set, all = 0; 36617987Speter int optc, what; 36717987Speter struct rlimit limit; 36811571Sjoerg 36917987Speter what = 'f'; 37052072Sgreen while ((optc = nextopt("HSatfdsmcnulb")) != '\0') 37117987Speter switch (optc) { 37211571Sjoerg case 'H': 37317987Speter how = HARD; 37411571Sjoerg break; 37511571Sjoerg case 'S': 37617987Speter how = SOFT; 37711571Sjoerg break; 37811571Sjoerg case 'a': 37917987Speter all = 1; 38011571Sjoerg break; 38117987Speter default: 38217987Speter what = optc; 38311571Sjoerg } 38411571Sjoerg 38517987Speter for (l = limits; l->name && l->option != what; l++) 38617987Speter ; 38717987Speter if (!l->name) 38820425Ssteve error("ulimit: internal error (%c)", what); 38917987Speter 39017987Speter set = *argptr ? 1 : 0; 39117987Speter if (set) { 39217987Speter char *p = *argptr; 39317987Speter 39417987Speter if (all || argptr[1]) 39520425Ssteve error("ulimit: too many arguments"); 39617987Speter if (strcmp(p, "unlimited") == 0) 39711571Sjoerg val = RLIM_INFINITY; 39811571Sjoerg else { 39917987Speter val = (quad_t) 0; 40017987Speter 40117987Speter while ((c = *p++) >= '0' && c <= '9') 40217987Speter { 40317987Speter val = (val * 10) + (long)(c - '0'); 40417987Speter if (val < (quad_t) 0) 40517987Speter break; 40617987Speter } 40717987Speter if (c) 40820425Ssteve error("ulimit: bad number"); 40917987Speter val *= l->factor; 41011571Sjoerg } 41117987Speter } 41217987Speter if (all) { 41318016Speter for (l = limits; l->name; l++) { 41418016Speter char optbuf[40]; 41518016Speter if (getrlimit(l->cmd, &limit) < 0) 41620425Ssteve error("ulimit: can't get limit: %s", strerror(errno)); 41717987Speter if (how & SOFT) 41817987Speter val = limit.rlim_cur; 41917987Speter else if (how & HARD) 42017987Speter val = limit.rlim_max; 42117987Speter 42218016Speter if (l->units) 42318016Speter snprintf(optbuf, sizeof(optbuf), 42418019Speter "(%s, -%c) ", l->units, l->option); 42518016Speter else 42618016Speter snprintf(optbuf, sizeof(optbuf), 42718019Speter "(-%c) ", l->option); 42818019Speter out1fmt("%-18s %18s ", l->name, optbuf); 42917987Speter if (val == RLIM_INFINITY) 43017987Speter out1fmt("unlimited\n"); 43117987Speter else 43217987Speter { 43317987Speter val /= l->factor; 43418018Speter out1fmt("%qd\n", (quad_t) val); 43517987Speter } 43611571Sjoerg } 43717987Speter return 0; 43811571Sjoerg } 43917987Speter 44018016Speter if (getrlimit(l->cmd, &limit) < 0) 44120425Ssteve error("ulimit: can't get limit: %s", strerror(errno)); 44217987Speter if (set) { 44317987Speter if (how & SOFT) 44417987Speter limit.rlim_cur = val; 44517987Speter if (how & HARD) 44617987Speter limit.rlim_max = val; 44717987Speter if (setrlimit(l->cmd, &limit) < 0) 44820425Ssteve error("ulimit: bad limit: %s", strerror(errno)); 44917987Speter } else { 45017987Speter if (how & SOFT) 45117987Speter val = limit.rlim_cur; 45217987Speter else if (how & HARD) 45317987Speter val = limit.rlim_max; 45417987Speter 45517987Speter if (val == RLIM_INFINITY) 45617987Speter out1fmt("unlimited\n"); 45717987Speter else 45817987Speter { 45917987Speter val /= l->factor; 46018018Speter out1fmt("%qd\n", (quad_t) val); 46117987Speter } 46217987Speter } 46311571Sjoerg return 0; 46411571Sjoerg} 465