commit ff7d142b87376a4954ef3c457f260ba5693fe4d7 Author: Thierry FOURNIER Date: Thu Sep 19 17:19:48 2013 +0200 BUILTIN: closeallfd closeallfd Close all file descriptors from to the last open fd. This function never fail except if the parameter is wrong or missing. diff --git ./builtins/Makefile.in ./builtins/Makefile.in index 7c8599b..6045f13 100644 --- ./builtins/Makefile.in +++ ./builtins/Makefile.in @@ -127,6 +127,7 @@ DEFSRC = $(srcdir)/alias.def $(srcdir)/bind.def $(srcdir)/break.def \ $(srcdir)/mkstemp.def \ $(srcdir)/socketpair.def \ $(srcdir)/canonize.def \ + $(srcdir)/closeallfd.def \ $(srcdir)/builtin.def $(srcdir)/caller.def \ $(srcdir)/cd.def $(srcdir)/colon.def \ $(srcdir)/command.def $(srcdir)/declare.def $(srcdir)/echo.def \ @@ -153,6 +154,7 @@ OFILES = builtins.o \ mkstemp.o \ socketpair.o \ canonize.o \ + closeallfd.o \ alias.o bind.o break.o builtin.o caller.o cd.o colon.o command.o \ common.o declare.o echo.o enable.o eval.o evalfile.o \ evalstring.o exec.o \ @@ -298,6 +300,7 @@ sleep.o: sleep.def mkstemp.o: mkstemp.def socketpair.o: socketpair.def canonize.o: canonize.def +closeallfd.o: closeallfd.def usleep.o: usleep.def sendlog.o: sendlog.def fgets.o: fgets.def diff --git ./builtins/closeallfd.def ./builtins/closeallfd.def new file mode 100644 index 0000000..1011b30 --- /dev/null +++ ./builtins/closeallfd.def @@ -0,0 +1,48 @@ +$BUILTIN closeallfd +$FUNCTION closeallfd_builtin +$PRODUCES closeallfd.c +$SHORT_DOC closeallfd +Close all file descriptors from to the last open fd. This +function never fail except if the parameter is wrong or missing. +$END + +#include + +#include +#include + +#include "../bashtypes.h" +#include "../shell.h" +#include "../jobs.h" +#include "common.h" +#include "bashgetopt.h" + +int closeallfd_builtin(WORD_LIST *list) +{ + long long int fd; + struct rlimit lim; + char *error; + + /* check number of arguments */ + if (list == NULL || list->word == NULL || list->word->word == NULL) { + builtin_error("missing operand"); + return 1; + } + + /* convert input format. */ + fd = strtoll(list->word->word, &error, 10); + if (error[0] != '\0') { + builtin_error("invalid file descriptor %s", list->word->word); + return 1; + } + + /* get max fd */ + if (getrlimit(RLIMIT_NOFILE, &lim) == -1) + return 0; + + /* close all fd */ + for (; fd < lim.rlim_max; fd++) + close(fd); + + return 0; +}