ulimit builtin (PR #388)

This public domain code, originally by Doug Gwyn, Doug Kingston, Eric
Gisin, and Michael Rendell was ripped from pdksh 5.0.8 and hacked for
use with ash.
This commit is contained in:
jtc 1994-11-04 17:13:27 +00:00
parent bf98208622
commit 6cd3ad1d11
3 changed files with 144 additions and 3 deletions

View File

@ -1,11 +1,11 @@
# from: @(#)Makefile 8.1 (Berkeley) 6/8/93
# $Id: Makefile,v 1.17 1994/06/24 07:33:12 jtc Exp $
# $Id: Makefile,v 1.18 1994/11/04 17:13:27 jtc Exp $
PROG= sh
SRCS= alias.c builtins.c cd.c echo.c error.c eval.c exec.c expand.c \
histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c \
mystring.c nodes.c options.c parser.c redir.c show.c \
syntax.c trap.c output.c var.c
syntax.c trap.c output.c ulimit.c var.c
OBJS+= init.o arith.o arith_lex.o
LDADD+= -ll -ledit -ltermcap
DPADD+= ${LIBL} ${LIBEDIT} ${LIBTERMCAP}

View File

@ -35,7 +35,7 @@
# SUCH DAMAGE.
#
# from: @(#)builtins 8.1 (Berkeley) 5/31/93
# $Id: builtins.def,v 1.9 1994/06/11 16:11:43 mycroft Exp $
# $Id: builtins.def,v 1.10 1994/11/04 17:13:30 jtc Exp $
#
# This file lists all the builtin commands. The first column is the name
@ -92,3 +92,4 @@ unsetcmd unset
waitcmd wait
#foocmd foo
aliascmd alias
ulimitcmd ulimit

140
bin/sh/ulimit.c Normal file
View File

@ -0,0 +1,140 @@
/*
* ulimit builtin
*
* This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
* Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
* ash by J.T. Conklin.
*
* Public domain.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "shell.h"
#include "options.h"
#include "output.h"
struct limits {
const char *name;
int cmd;
int factor; /* multiply by to get rlim_{cur,max} values */
char option;
};
static const struct limits limits[] = {
{ "time(seconds)", RLIMIT_CPU, 1, 't' },
{ "file(blocks)", RLIMIT_FSIZE, 512, 'f' },
{ "data(kbytes)", RLIMIT_DATA, 1024, 'd' },
{ "stack(kbytes)", RLIMIT_STACK, 1024, 's' },
{ "memory(kbytes)", RLIMIT_RSS, 1024, 'm' },
{ "coredump(blocks)", RLIMIT_CORE, 512, 'c' },
{ "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' },
#ifdef RLIMIT_VMEM
{ "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' },
#endif
#ifdef RLIMIT_SWAP
{ "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' },
#endif
{ (char *) 0 }
};
int
ulimitcmd(argc, argv)
int argc;
char **argv;
{
register int c;
quad_t val;
enum { SOFT = 0x1, HARD = 0x2 }
how = SOFT | HARD;
const struct limits *l;
int set, all = 0;
int optc, what;
struct rlimit limit;
what = 'f';
while ((optc = nextopt("HSatfdsmcn")) != '\0')
switch (optc) {
case 'H':
how = HARD;
break;
case 'S':
how = SOFT;
break;
case 'a':
all = 1;
break;
default:
what = optc;
}
for (l = limits; l->name && l->option != what; l++)
;
if (!l->name)
error("ulimit: internal error (%c)\n", what);
set = *argptr ? 1 : 0;
if (set) {
char *p = *argptr;
if (all || argptr[1])
error("ulimit: too many arguments\n");
val = (quad_t) 0;
while ((c = *p++) >= '0' && c <= '9')
{
val = (val * 10) + (long)(c - '0');
if (val < (quad_t) 0)
break;
}
if (c)
error("ulimit: bad number\n");
val *= l->factor;
}
if (all) {
for (l = limits; l->name; l++) {
getrlimit(l->cmd, &limit);
if (how & SOFT)
val = limit.rlim_cur;
else if (how & HARD)
val = limit.rlim_max;
out1fmt("%-20s ", l->name);
if (val == RLIM_INFINITY)
out1fmt("unlimited\n");
else
{
val /= l->factor;
out1fmt("%ld\n", (long) val);
}
}
return 0;
}
getrlimit(l->cmd, &limit);
if (set) {
if (how & SOFT)
limit.rlim_cur = val;
if (how & HARD)
limit.rlim_max = val;
if (setrlimit(l->cmd, &limit) < 0)
error("ulimit: bad limit\n");
} else {
if (how & SOFT)
val = limit.rlim_cur;
else if (how & HARD)
val = limit.rlim_max;
}
if (!set) {
if (val == RLIM_INFINITY)
out1fmt("unlimited\n");
else
{
val /= l->factor;
out1fmt("%ld\n", (long) val);
}
}
return 0;
}