aboutsummaryrefslogtreecommitdiff
path: root/debianutils
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-11-11 03:00:12 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-11-11 03:00:12 +0000
commit8f0722a53b9a88c0b4e60b79e4362d0f31c32c54 (patch)
tree2efbea745d3ef22e375df3e60ba4c8f14c04f1c4 /debianutils
parentc11986d89eba564945b226660031e624bf897561 (diff)
downloadbusybox-w32-8f0722a53b9a88c0b4e60b79e4362d0f31c32c54.tar.gz
busybox-w32-8f0722a53b9a88c0b4e60b79e4362d0f31c32c54.tar.bz2
busybox-w32-8f0722a53b9a88c0b4e60b79e4362d0f31c32c54.zip
Move readlink, mktemp, run-parts and which to a new debianutils dir.
Diffstat (limited to 'debianutils')
-rw-r--r--debianutils/Makefile30
-rw-r--r--debianutils/Makefile.in36
-rw-r--r--debianutils/config.in15
-rw-r--r--debianutils/mktemp.c40
-rw-r--r--debianutils/readlink.c46
-rw-r--r--debianutils/run_parts.c251
-rw-r--r--debianutils/which.c81
7 files changed, 499 insertions, 0 deletions
diff --git a/debianutils/Makefile b/debianutils/Makefile
new file mode 100644
index 000000000..35b32e73b
--- /dev/null
+++ b/debianutils/Makefile
@@ -0,0 +1,30 @@
1# Makefile for busybox
2#
3# Copyright (C) 1999-2002 Erik Andersen <andersee@debian.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19
20TOPDIR:= ../
21DEBIANUTILS_DIR:=./
22include $(TOPDIR).config
23include $(TOPDIR)Rules.mak
24include Makefile.in
25all: $(libraries-y)
26-include $(TOPDIR).depend
27
28clean:
29 rm -f *.o *.a $(AR_TARGET)
30
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 @@
1# Makefile for busybox
2#
3# Copyright (C) 1999-2002 Erik Andersen <andersee@debian.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19
20DEBIANUTILS_AR:=debianutils.a
21ifndef $(DEBIANUTILS_DIR)
22DEBIANUTILS_DIR:=$(TOPDIR)debianutils/
23endif
24
25
26DEBIANUTILS-y:=
27DEBIANUTILS-$(CONFIG_MKTEMP) += mktemp.o
28DEBIANUTILS-$(CONFIG_READLINK) += readlink.o
29DEBIANUTILS-$(CONFIG_RUN_PARTS) += run_parts.o
30DEBIANUTILS-$(CONFIG_WHICH) += which.o
31
32libraries-y+=$(DEBIANUTILS_DIR)$(DEBIANUTILS_AR)
33
34$(DEBIANUTILS_DIR)$(DEBIANUTILS_AR): $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y))
35 $(AR) -ro $@ $(patsubst %,$(DEBIANUTILS_DIR)%, $(DEBIANUTILS-y))
36
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 @@
1#
2# For a description of the syntax of this configuration file,
3# see scripts/kbuild/config-language.txt.
4#
5
6mainmenu_option next_comment
7comment 'Debian Utilities'
8
9bool 'mktemp' CONFIG_MKTEMP
10bool 'readlink' CONFIG_READLINK
11bool 'run-parts' CONFIG_RUN_PARTS
12bool 'which' CONFIG_WHICH
13
14endmenu
15
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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini mktemp implementation for busybox
4 *
5 *
6 * Copyright (C) 2000 by Daniel Jacobowitz
7 * Written by Daniel Jacobowitz <dan@debian.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include <unistd.h>
29#include <stdlib.h>
30#include "busybox.h"
31
32extern int mktemp_main(int argc, char **argv)
33{
34 if (argc != 2 && (argc != 3 || strcmp(argv[1], "-q")))
35 show_usage();
36 if(mkstemp(argv[argc-1]) < 0)
37 return EXIT_FAILURE;
38 (void) puts(argv[argc-1]);
39 return EXIT_SUCCESS;
40}
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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini readlink implementation for busybox
4 *
5 * Copyright (C) 2000,2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <errno.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include "busybox.h"
27
28int readlink_main(int argc, char **argv)
29{
30 char *buf = NULL;
31
32 /* no options, no getopt */
33
34 if (argc != 2)
35 show_usage();
36
37 buf = xreadlink(argv[1]);
38 if (!buf)
39 return EXIT_FAILURE;
40 puts(buf);
41#ifdef CONFIG_FEATURE_CLEAN_UP
42 free(buf);
43#endif
44
45 return EXIT_SUCCESS;
46}
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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini run-parts implementation for busybox
4 *
5 *
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
7 *
8 * Based on the Debian run-parts program, version 1.15
9 * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
10 * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
11 *
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 * 02111-1307 USA
27 *
28 */
29
30/* This is my first attempt to write a program in C (well, this is my first
31 * attempt to write a program! :-) . */
32
33/* This piece of code is heavily based on the original version of run-parts,
34 * taken from debian-utils. I've only removed the long options and a the
35 * report mode. As the original run-parts support only long options, I've
36 * broken compatibility because the BusyBox policy doesn't allow them.
37 * The supported options are:
38 * -t test. Print the name of the files to be executed, without
39 * execute them.
40 * -a ARG argument. Pass ARG as an argument the program executed. It can
41 * be repeated to pass multiple arguments.
42 * -u MASK umask. Set the umask of the program executed to MASK. */
43
44/* TODO
45 * done - convert calls to error in perror... and remove error()
46 * done - convert malloc/realloc to their x... counterparts
47 * done - remove catch_sigchld
48 * done - use bb's concat_path_file()
49 * done - declare run_parts_main() as extern and any other function as static?
50 */
51
52#include <stdio.h>
53#include <stdarg.h>
54#include <stdlib.h>
55/* #include <sys/types.h> */
56#include <sys/wait.h>
57#include <dirent.h>
58#include <sys/stat.h>
59#include <unistd.h>
60#include <getopt.h>
61#include <string.h>
62#include <errno.h>
63#include <ctype.h>
64
65#include "busybox.h"
66
67static int test_mode = 0;
68static int exitstatus = 0;
69
70static int argcount = 0, argsize = 0;
71static char **args = 0;
72
73
74/* set_umask */
75/* Check and set the umask of the program executed. As stated in the original
76 * run-parts, the octal conversion in libc is not foolproof; it will take the
77 * 8 and 9 digits under some circumstances. We'll just have to live with it.
78 */
79
80static void set_umask (void)
81{
82 int mask, result;
83
84 /*TODO
85 * We must substitute sscanf, according to bb's style guide? */
86 result = sscanf (optarg, "%o", &mask);
87 if ((result != 1) || (mask > 07777) || (mask < 0)) {
88 perror_msg_and_die ("bad umask value");
89 }
90
91 umask (mask);
92}
93
94/* add_argument */
95/* Add an argument to the commands that we will call. Called once for
96 every argument. */
97static void add_argument (char *newarg)
98{
99 if (argcount+1 >= argsize) {
100 argsize = argsize ? argsize*2 : 4;
101 /*TODO if we convert to xrealloc we lose the verbose error message */
102 args = realloc(args, argsize * (sizeof(char*)));
103 if (!args) {
104 perror_msg_and_die ("failed to reallocate memory for arguments");
105 }
106 }
107 args[argcount++] = newarg;
108 args[argcount] = 0;
109}
110
111/* valid_name */
112/* True or false? Is this a valid filename (upper/lower alpha, digits,
113 * underscores, and hyphens only?)
114 */
115
116static int valid_name (const struct dirent *d)
117{
118 char *c = d->d_name;
119 while (*c) {
120 if (!isalnum(*c) && *c!='_' && *c!='-') {
121 return 0;
122 }
123 ++c;
124 }
125 return 1;
126}
127
128
129/* run_part */
130/* Execute a file */
131
132static void run_part (char *progname)
133{
134 int result;
135 int pid;
136
137
138 if ((pid=fork()) < 0) {
139 perror_msg_and_die ("failed to fork");
140 }
141 else if (!pid) {
142 args[0] = progname;
143 execv (progname, args);
144 perror_msg_and_die ("failed to exec %s", progname);
145 }
146
147 if (0) {
148
149 } else {
150
151 waitpid(pid, &result, 0);
152 }
153
154 if (WIFEXITED (result) && WEXITSTATUS(result)) {
155 perror_msg ("%s exited with return code %d", progname, WEXITSTATUS(result));
156 exitstatus = 1;
157 }
158 else if (WIFSIGNALED (result)) {
159 perror_msg ("%s exited because of uncaught signal %d", progname,
160 WTERMSIG(result));
161 exitstatus = 1;
162 }
163}
164
165/* run_parts */
166/* Find the parts to run & call run_part() */
167
168static void run_parts (char *dir_name)
169{
170 struct dirent **namelist = 0;
171 char *filename;
172 int entries, i;
173 struct stat st;
174
175 /* -- */
176
177 /* scandir() isn't POSIX, but it makes things easy. */
178 entries = scandir (dir_name, &namelist, valid_name, alphasort);
179
180 if (entries < 0) {
181 perror_msg_and_die ("failed to open directory %s", dir_name);
182 }
183
184 for (i = 0; i < entries; i++) {
185
186 /* -- */
187
188 filename = concat_path_file (dir_name, namelist[i]->d_name);
189
190 if (stat (filename, &st) < 0) {
191 perror_msg_and_die ("failed to stat component %s", filename);
192 }
193 if (S_ISREG(st.st_mode) && !access (filename, X_OK)) {
194 if (test_mode)
195 printf ("run-parts would run %s\n", filename);
196 else {
197 run_part (filename);
198 }
199 }
200
201 else if (!S_ISDIR(st.st_mode)) {
202 error_msg ("component %s is not an executable plain file",
203 filename);
204 exitstatus = 1;
205 }
206
207 free (namelist[i]);
208 free (filename);
209 }
210 free (namelist);
211}
212
213/* run_parts_main */
214/* Process options */
215int run_parts_main (int argc, char *argv[])
216{
217 umask (022);
218 add_argument(0);
219
220 for (;;) {
221 int c;
222
223 opterr = 0;
224 c = getopt(argc, argv, "tu:a:");
225
226 if (c == EOF)
227 break;
228 switch (c) {
229 case 't': /* Enable test mode */
230 test_mode = 1;
231 break;
232 case 'u': /* Set the umask of the programs executed */
233 set_umask ();
234 break;
235 case 'a': /* Pass an argument to the programs */
236 add_argument (optarg);
237 break;
238 default:
239 show_usage();
240 }
241 }
242
243 /* We require exactly one argument: the directory name */
244 if (optind != (argc - 1)) {
245 show_usage();
246 }
247
248 run_parts (argv[optind]);
249
250 return exitstatus;
251}
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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Which implementation for busybox
4 *
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24/* getopt not needed */
25#include <string.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include "busybox.h"
29
30extern int which_main(int argc, char **argv)
31{
32 char *path_list, *path_n;
33 struct stat filestat;
34 int i, count=1, found, status = EXIT_SUCCESS;
35
36 if (argc <= 1 || **(argv + 1) == '-')
37 show_usage();
38 argc--;
39
40 path_list = getenv("PATH");
41 if (path_list != NULL) {
42 for(i=strlen(path_list); i > 0; i--)
43 if (path_list[i]==':') {
44 path_list[i]=0;
45 count++;
46 }
47 } else {
48 path_list = "/bin\0/sbin\0/usr/bin\0/usr/sbin\0/usr/local/bin";
49 count = 5;
50 }
51
52 while(argc-- > 0) {
53 path_n = path_list;
54 argv++;
55 found = 0;
56 for (i = 0; i < count; i++) {
57 char *buf;
58 buf = concat_path_file(path_n, *argv);
59 if (stat (buf, &filestat) == 0
60 && filestat.st_mode & S_IXUSR)
61 {
62 puts(buf);
63 found = 1;
64 break;
65 }
66 free(buf);
67 path_n += (strlen(path_n) + 1);
68 }
69 if (!found)
70 status = EXIT_FAILURE;
71 }
72 return status;
73}
74
75/*
76Local Variables:
77c-file-style: "linux"
78c-basic-offset: 4
79tab-width: 4
80End:
81*/