From 8f0722a53b9a88c0b4e60b79e4362d0f31c32c54 Mon Sep 17 00:00:00 2001 From: Glenn L McGrath Date: Mon, 11 Nov 2002 03:00:12 +0000 Subject: Move readlink, mktemp, run-parts and which to a new debianutils dir. --- debianutils/Makefile | 30 ++++++ debianutils/Makefile.in | 36 +++++++ debianutils/config.in | 15 +++ debianutils/mktemp.c | 40 ++++++++ debianutils/readlink.c | 46 +++++++++ debianutils/run_parts.c | 251 ++++++++++++++++++++++++++++++++++++++++++++++++ debianutils/which.c | 81 ++++++++++++++++ 7 files changed, 499 insertions(+) create mode 100644 debianutils/Makefile create mode 100644 debianutils/Makefile.in create mode 100644 debianutils/config.in create mode 100644 debianutils/mktemp.c create mode 100644 debianutils/readlink.c create mode 100644 debianutils/run_parts.c create mode 100644 debianutils/which.c (limited to 'debianutils') diff --git a/debianutils/Makefile b/debianutils/Makefile new file mode 100644 index 000000000..35b32e73b --- /dev/null +++ b/debianutils/Makefile @@ -0,0 +1,30 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2002 Erik Andersen +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +TOPDIR:= ../ +DEBIANUTILS_DIR:=./ +include $(TOPDIR).config +include $(TOPDIR)Rules.mak +include Makefile.in +all: $(libraries-y) +-include $(TOPDIR).depend + +clean: + rm -f *.o *.a $(AR_TARGET) + diff --git a/debianutils/Makefile.in b/debianutils/Makefile.in new file mode 100644 index 000000000..91887bda1 --- /dev/null +++ b/debianutils/Makefile.in @@ -0,0 +1,36 @@ +# Makefile for busybox +# +# Copyright (C) 1999-2002 Erik Andersen +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +DEBIANUTILS_AR:=debianutils.a +ifndef $(DEBIANUTILS_DIR) +DEBIANUTILS_DIR:=$(TOPDIR)debianutils/ +endif + + +DEBIANUTILS-y:= +DEBIANUTILS-$(CONFIG_MKTEMP) += mktemp.o +DEBIANUTILS-$(CONFIG_READLINK) += readlink.o +DEBIANUTILS-$(CONFIG_RUN_PARTS) += run_parts.o +DEBIANUTILS-$(CONFIG_WHICH) += which.o + +libraries-y+=$(DEBIANUTILS_DIR)$(DEBIANUTILS_AR) + +$(DEBIANUTILS_DIR)$(DEBIANUTILS_AR): $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y)) + $(AR) -ro $@ $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y)) + diff --git a/debianutils/config.in b/debianutils/config.in new file mode 100644 index 000000000..7b1d92991 --- /dev/null +++ b/debianutils/config.in @@ -0,0 +1,15 @@ +# +# For a description of the syntax of this configuration file, +# see scripts/kbuild/config-language.txt. +# + +mainmenu_option next_comment +comment 'Debian Utilities' + +bool 'mktemp' CONFIG_MKTEMP +bool 'readlink' CONFIG_READLINK +bool 'run-parts' CONFIG_RUN_PARTS +bool 'which' CONFIG_WHICH + +endmenu + diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c new file mode 100644 index 000000000..bc47d0af0 --- /dev/null +++ b/debianutils/mktemp.c @@ -0,0 +1,40 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini mktemp implementation for busybox + * + * + * Copyright (C) 2000 by Daniel Jacobowitz + * Written by Daniel Jacobowitz + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include +#include +#include "busybox.h" + +extern int mktemp_main(int argc, char **argv) +{ + if (argc != 2 && (argc != 3 || strcmp(argv[1], "-q"))) + show_usage(); + if(mkstemp(argv[argc-1]) < 0) + return EXIT_FAILURE; + (void) puts(argv[argc-1]); + return EXIT_SUCCESS; +} diff --git a/debianutils/readlink.c b/debianutils/readlink.c new file mode 100644 index 000000000..da5259038 --- /dev/null +++ b/debianutils/readlink.c @@ -0,0 +1,46 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini readlink implementation for busybox + * + * Copyright (C) 2000,2001 Matt Kraai + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include "busybox.h" + +int readlink_main(int argc, char **argv) +{ + char *buf = NULL; + + /* no options, no getopt */ + + if (argc != 2) + show_usage(); + + buf = xreadlink(argv[1]); + if (!buf) + return EXIT_FAILURE; + puts(buf); +#ifdef CONFIG_FEATURE_CLEAN_UP + free(buf); +#endif + + return EXIT_SUCCESS; +} diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c new file mode 100644 index 000000000..3ec4b9d10 --- /dev/null +++ b/debianutils/run_parts.c @@ -0,0 +1,251 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini run-parts implementation for busybox + * + * + * Copyright (C) 2001 by Emanuele Aina + * + * Based on the Debian run-parts program, version 1.15 + * Copyright (C) 1996 Jeff Noxon , + * Copyright (C) 1996-1999 Guy Maor + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + * + */ + +/* This is my first attempt to write a program in C (well, this is my first + * attempt to write a program! :-) . */ + +/* This piece of code is heavily based on the original version of run-parts, + * taken from debian-utils. I've only removed the long options and a the + * report mode. As the original run-parts support only long options, I've + * broken compatibility because the BusyBox policy doesn't allow them. + * The supported options are: + * -t test. Print the name of the files to be executed, without + * execute them. + * -a ARG argument. Pass ARG as an argument the program executed. It can + * be repeated to pass multiple arguments. + * -u MASK umask. Set the umask of the program executed to MASK. */ + +/* TODO + * done - convert calls to error in perror... and remove error() + * done - convert malloc/realloc to their x... counterparts + * done - remove catch_sigchld + * done - use bb's concat_path_file() + * done - declare run_parts_main() as extern and any other function as static? + */ + +#include +#include +#include +/* #include */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "busybox.h" + +static int test_mode = 0; +static int exitstatus = 0; + +static int argcount = 0, argsize = 0; +static char **args = 0; + + +/* set_umask */ +/* Check and set the umask of the program executed. As stated in the original + * run-parts, the octal conversion in libc is not foolproof; it will take the + * 8 and 9 digits under some circumstances. We'll just have to live with it. + */ + +static void set_umask (void) +{ + int mask, result; + + /*TODO + * We must substitute sscanf, according to bb's style guide? */ + result = sscanf (optarg, "%o", &mask); + if ((result != 1) || (mask > 07777) || (mask < 0)) { + perror_msg_and_die ("bad umask value"); + } + + umask (mask); +} + +/* add_argument */ +/* Add an argument to the commands that we will call. Called once for + every argument. */ +static void add_argument (char *newarg) +{ + if (argcount+1 >= argsize) { + argsize = argsize ? argsize*2 : 4; + /*TODO if we convert to xrealloc we lose the verbose error message */ + args = realloc(args, argsize * (sizeof(char*))); + if (!args) { + perror_msg_and_die ("failed to reallocate memory for arguments"); + } + } + args[argcount++] = newarg; + args[argcount] = 0; +} + +/* valid_name */ +/* True or false? Is this a valid filename (upper/lower alpha, digits, + * underscores, and hyphens only?) + */ + +static int valid_name (const struct dirent *d) +{ + char *c = d->d_name; + while (*c) { + if (!isalnum(*c) && *c!='_' && *c!='-') { + return 0; + } + ++c; + } + return 1; +} + + +/* run_part */ +/* Execute a file */ + +static void run_part (char *progname) +{ + int result; + int pid; + + + if ((pid=fork()) < 0) { + perror_msg_and_die ("failed to fork"); + } + else if (!pid) { + args[0] = progname; + execv (progname, args); + perror_msg_and_die ("failed to exec %s", progname); + } + + if (0) { + + } else { + + waitpid(pid, &result, 0); + } + + if (WIFEXITED (result) && WEXITSTATUS(result)) { + perror_msg ("%s exited with return code %d", progname, WEXITSTATUS(result)); + exitstatus = 1; + } + else if (WIFSIGNALED (result)) { + perror_msg ("%s exited because of uncaught signal %d", progname, + WTERMSIG(result)); + exitstatus = 1; + } +} + +/* run_parts */ +/* Find the parts to run & call run_part() */ + +static void run_parts (char *dir_name) +{ + struct dirent **namelist = 0; + char *filename; + int entries, i; + struct stat st; + + /* -- */ + + /* scandir() isn't POSIX, but it makes things easy. */ + entries = scandir (dir_name, &namelist, valid_name, alphasort); + + if (entries < 0) { + perror_msg_and_die ("failed to open directory %s", dir_name); + } + + for (i = 0; i < entries; i++) { + + /* -- */ + + filename = concat_path_file (dir_name, namelist[i]->d_name); + + if (stat (filename, &st) < 0) { + perror_msg_and_die ("failed to stat component %s", filename); + } + if (S_ISREG(st.st_mode) && !access (filename, X_OK)) { + if (test_mode) + printf ("run-parts would run %s\n", filename); + else { + run_part (filename); + } + } + + else if (!S_ISDIR(st.st_mode)) { + error_msg ("component %s is not an executable plain file", + filename); + exitstatus = 1; + } + + free (namelist[i]); + free (filename); + } + free (namelist); +} + +/* run_parts_main */ +/* Process options */ +int run_parts_main (int argc, char *argv[]) +{ + umask (022); + add_argument(0); + + for (;;) { + int c; + + opterr = 0; + c = getopt(argc, argv, "tu:a:"); + + if (c == EOF) + break; + switch (c) { + case 't': /* Enable test mode */ + test_mode = 1; + break; + case 'u': /* Set the umask of the programs executed */ + set_umask (); + break; + case 'a': /* Pass an argument to the programs */ + add_argument (optarg); + break; + default: + show_usage(); + } + } + + /* We require exactly one argument: the directory name */ + if (optind != (argc - 1)) { + show_usage(); + } + + run_parts (argv[optind]); + + return exitstatus; +} diff --git a/debianutils/which.c b/debianutils/which.c new file mode 100644 index 000000000..b2af5a8ea --- /dev/null +++ b/debianutils/which.c @@ -0,0 +1,81 @@ +/* vi: set sw=4 ts=4: */ +/* + * Which implementation for busybox + * + * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen + * Copyright (C) 1999,2000,2001 by Erik Andersen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +/* getopt not needed */ +#include +#include +#include +#include "busybox.h" + +extern int which_main(int argc, char **argv) +{ + char *path_list, *path_n; + struct stat filestat; + int i, count=1, found, status = EXIT_SUCCESS; + + if (argc <= 1 || **(argv + 1) == '-') + show_usage(); + argc--; + + path_list = getenv("PATH"); + if (path_list != NULL) { + for(i=strlen(path_list); i > 0; i--) + if (path_list[i]==':') { + path_list[i]=0; + count++; + } + } else { + path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin"; + count = 5; + } + + while(argc-- > 0) { + path_n = path_list; + argv++; + found = 0; + for (i = 0; i < count; i++) { + char *buf; + buf = concat_path_file(path_n, *argv); + if (stat (buf, &filestat) == 0 + && filestat.st_mode & S_IXUSR) + { + puts(buf); + found = 1; + break; + } + free(buf); + path_n += (strlen(path_n) + 1); + } + if (!found) + status = EXIT_FAILURE; + } + return status; +} + +/* +Local Variables: +c-file-style: "linux" +c-basic-offset: 4 +tab-width: 4 +End: +*/ -- cgit v1.2.3