Move awk from textutils to editors. Cleanup run-parts, saves 200 bytes, moves the guts of run_parts to libbb to be used by ifupdown.

This commit is contained in:
Glenn L McGrath 2002-11-11 06:21:00 +00:00
parent 8f0722a53b
commit 545106f8db
8 changed files with 2945 additions and 196 deletions

View File

@ -49,191 +49,46 @@
* done - declare run_parts_main() as extern and any other function as static? * done - declare run_parts_main() as extern and any other function as static?
*/ */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
/* #include <sys/types.h> */
#include <sys/wait.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <string.h> #include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "busybox.h" #include "libbb.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 */ /* run_parts_main */
/* Process options */ /* Process options */
int run_parts_main (int argc, char *argv[]) int run_parts_main(int argc, char **argv)
{ {
umask (022); char **args = xmalloc(2 * sizeof(char *));
add_argument(0); unsigned char test_mode = 0;
unsigned short argcount = 1;
int opt;
for (;;) { umask(022);
int c;
opterr = 0; while ((opt = getopt(argc, argv, "tu:a:")) != -1) {
c = getopt(argc, argv, "tu:a:"); switch (opt) {
if (c == EOF)
break;
switch (c) {
case 't': /* Enable test mode */ case 't': /* Enable test mode */
test_mode = 1; test_mode = 1;
break; break;
case 'u': /* Set the umask of the programs executed */ case 'u': /* Set the umask of the programs executed */
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.
*/
{
const unsigned int mask = (unsigned int) strtol(optarg, NULL, 8);
if (mask > 07777) {
perror_msg_and_die("bad umask value");
}
umask(mask);
}
break; break;
case 'a': /* Pass an argument to the programs */ case 'a': /* Pass an argument to the programs */
add_argument (optarg); /* Add an argument to the commands that we will call.
* Called once for every argument. */
args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
args[argcount++] = optarg;
args[argcount] = 0;
break; break;
default: default:
show_usage(); show_usage();
@ -245,7 +100,6 @@ int run_parts_main (int argc, char *argv[])
show_usage(); show_usage();
} }
run_parts (argv[optind]); args[0] = argv[optind];
return(run_parts(args, test_mode));
return exitstatus;
} }

View File

@ -23,6 +23,7 @@ EDITOR_DIR:=$(TOPDIR)editors/
endif endif
EDITOR-y:= EDITOR-y:=
EDITOR-$(CONFIG_AWK) += awk.o
EDITOR-$(CONFIG_SED) += sed.o EDITOR-$(CONFIG_SED) += sed.o
EDITOR-$(CONFIG_VI) += vi.o EDITOR-$(CONFIG_VI) += vi.o
EDITOR_SRC:= $(EDITOR-y) EDITOR_SRC:= $(EDITOR-y)
@ -30,6 +31,13 @@ EDITOR_OBJ:= $(patsubst %.c,$(EDITOR_DIR)%.o, $(EDITOR_SRC))
libraries-y+=$(EDITOR_DIR)$(EDITOR_AR) libraries-y+=$(EDITOR_DIR)$(EDITOR_AR)
needlibm-y:=
needlibm-$(CONFIG_FEATURE_AWK_MATH) := y
ifeq ($(needlibm-y),y)
LIBRARIES += -lm
endif
$(EDITOR_DIR)$(EDITOR_AR): $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y)) $(EDITOR_DIR)$(EDITOR_AR): $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y))
$(AR) -ro $@ $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y)) $(AR) -ro $@ $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y))

2742
editors/awk.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,6 +6,10 @@
mainmenu_option next_comment mainmenu_option next_comment
comment 'Editors' comment 'Editors'
bool 'awk' CONFIG_AWK
if [ "$CONFIG_AWK" = "y" ]; then
bool ' Enable math functions (requires libm)' CONFIG_FEATURE_AWK_MATH
fi
bool 'sed' CONFIG_SED bool 'sed' CONFIG_SED
bool 'vi' CONFIG_VI bool 'vi' CONFIG_VI
if [ "$CONFIG_VI" = "y" ]; then if [ "$CONFIG_VI" = "y" ]; then

View File

@ -324,6 +324,7 @@ void bb_asprintf(char **string_ptr, const char *format, ...) __attribute__ ((for
#define FAIL_DELAY 3 #define FAIL_DELAY 3
extern void change_identity ( const struct passwd *pw ); extern void change_identity ( const struct passwd *pw );
extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args ); extern void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args );
extern int run_parts(char **args, const unsigned char test_mode);
extern int restricted_shell ( const char *shell ); extern int restricted_shell ( const char *shell );
extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw ); extern void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw );
extern int correct_password ( const struct passwd *pw ); extern int correct_password ( const struct passwd *pw );

View File

@ -34,8 +34,8 @@ LIBBB_SRC:= \
my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \ my_getgrgid.c my_getpwnam.c my_getpwnamegid.c my_getpwuid.c \
parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \
print_file.c process_escape_sequence.c read_package_field.c \ print_file.c process_escape_sequence.c read_package_field.c \
recursive_action.c safe_read.c safe_strncpy.c syscalls.c \ recursive_action.c run_parts.c safe_read.c safe_strncpy.c \
syslog_msg_with_name.c time_string.c trim.c \ syscalls.c syslog_msg_with_name.c time_string.c trim.c \
vdprintf.c verror_msg.c vperror_msg.c wfopen.c xgetcwd.c xreadlink.c \ vdprintf.c verror_msg.c vperror_msg.c wfopen.c xgetcwd.c xreadlink.c \
xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \ xregcomp.c interface.c remove_file.c last_char_is.c copyfd.c \
vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \

139
libbb/run_parts.c Normal file
View File

@ -0,0 +1,139 @@
/* vi: set sw=4 ts=4: */
/*
* Mini run-parts implementation for busybox
*
*
* Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
*
* Based on the Debian run-parts program, version 1.15
* Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
* Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
*
*
* 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 <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <ctype.h>
#include "libbb.h"
/* 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_parts */
/* Find the parts to run & call run_part() */
extern int run_parts(char **args, const unsigned char test_mode)
{
struct dirent **namelist = 0;
struct stat st;
char *filename;
int entries;
int i;
int exitstatus = 0;
/* scandir() isn't POSIX, but it makes things easy. */
entries = scandir(args[0], &namelist, valid_name, alphasort);
if (entries == -1) {
perror_msg_and_die("failed to open directory %s", args[0]);
}
for (i = 0; i < entries; i++) {
filename = concat_path_file(args[0], 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 {
int result;
int pid;
if ((pid = fork()) < 0) {
perror_msg_and_die("failed to fork");
} else if (!pid) {
execv(args[0], args);
perror_msg_and_die("failed to exec %s", args[0]);
}
waitpid(pid, &result, 0);
if (WIFEXITED(result) && WEXITSTATUS(result)) {
perror_msg("%s exited with return code %d", args[0], WEXITSTATUS(result));
exitstatus = 1;
} else if (WIFSIGNALED(result)) {
perror_msg("%s exited because of uncaught signal %d", args[0], WTERMSIG(result));
exitstatus = 1;
}
}
}
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);
return(exitstatus);
}

View File

@ -1017,10 +1017,10 @@ static int doit(char *str)
return (1); return (1);
} }
static int execute_all(interface_defn *ifd, execfn *exec, char *opt) static int execute_all(interface_defn *ifd, execfn *exec, const char *opt)
{ {
int i; int i;
char buf[100]; // char buf[100];
for (i = 0; i < ifd->n_options; i++) { for (i = 0; i < ifd->n_options; i++) {
if (strcmp(ifd->option[i].name, opt) == 0) { if (strcmp(ifd->option[i].name, opt) == 0) {
@ -1030,8 +1030,9 @@ static int execute_all(interface_defn *ifd, execfn *exec, char *opt)
} }
} }
sprintf(buf, "run-parts /etc/network/if-%s.d", opt); runparts("/etc/network/if-"opt".d");
(*exec) (buf); // sprintf(buf, "run-parts /etc/network/if-%s.d", opt);
// (*exec) (buf);
return (1); return (1);
} }