aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-06-21 19:41:37 +0000
committerMatt Kraai <kraai@debian.org>2001-06-21 19:41:37 +0000
commitceeff7381929930fe8d7e33543e285d5fdcf1c68 (patch)
tree3cdbaddffecc92649215fdc71a43b4e8e86b7ea3
parent091781e20eb055ac286b5a617d53a50c7d6c451e (diff)
downloadbusybox-w32-ceeff7381929930fe8d7e33543e285d5fdcf1c68.tar.gz
busybox-w32-ceeff7381929930fe8d7e33543e285d5fdcf1c68.tar.bz2
busybox-w32-ceeff7381929930fe8d7e33543e285d5fdcf1c68.zip
Rewrote mkdir (and touched lots of things in the process).
-rw-r--r--Makefile5
-rw-r--r--archival/dpkg.c2
-rw-r--r--archival/tar.c7
-rw-r--r--coreutils/dirname.c14
-rw-r--r--coreutils/mkdir.c97
-rw-r--r--dirname.c14
-rw-r--r--dpkg.c2
-rw-r--r--editors/sed.c9
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/create_path.c71
-rw-r--r--libbb/dirname.c48
-rw-r--r--libbb/libbb.h4
-rw-r--r--libbb/make_directory.c66
-rw-r--r--libbb/strdup_substr.c32
-rw-r--r--mkdir.c97
-rw-r--r--sed.c9
-rw-r--r--tar.c7
17 files changed, 233 insertions, 255 deletions
diff --git a/Makefile b/Makefile
index 03feb6306..89d56d50c 100644
--- a/Makefile
+++ b/Makefile
@@ -237,7 +237,7 @@ endif
237LIBBB = libbb 237LIBBB = libbb
238LIBBB_LIB = libbb.a 238LIBBB_LIB = libbb.a
239LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \ 239LIBBB_CSRC= ask_confirmation.c chomp.c concat_path_file.c copy_file.c \
240copy_file_chunk.c create_path.c daemon.c device_open.c error_msg.c \ 240copy_file_chunk.c daemon.c device_open.c error_msg.c \
241error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \ 241error_msg_and_die.c fgets_str.c find_mount_point.c find_pid_by_name.c \
242find_root_device.c full_read.c full_write.c get_console.c \ 242find_root_device.c full_read.c full_write.c get_console.c \
243get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \ 243get_last_path_component.c get_line_from_file.c gz_open.c human_readable.c \
@@ -248,7 +248,8 @@ print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \
248safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ 248safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \
249trim.c unarchive.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ 249trim.c unarchive.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
250xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ 250xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \
251copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c 251copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \
252dirname.c make_directory.c strdup_substr.c
252LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 253LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
253LIBBB_CFLAGS = -I$(LIBBB) 254LIBBB_CFLAGS = -I$(LIBBB)
254ifneq ($(strip $(BB_SRC_DIR)),) 255ifneq ($(strip $(BB_SRC_DIR)),)
diff --git a/archival/dpkg.c b/archival/dpkg.c
index 4224672ec..7dd46be0c 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv)
837 optind++; 837 optind++;
838 } 838 }
839 839
840 create_path(infodir, S_IRWXU); 840 make_directory(infodir, S_IRWXU, FILEUTILS_RECUR);
841 841
842 status = status_read(); 842 status = status_read();
843 843
diff --git a/archival/tar.c b/archival/tar.c
index 55fb12c2c..e68194ff7 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
338 if (extractFlag==TRUE && tostdoutFlag==FALSE) { 338 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
339 /* Create the path to the file, just in case it isn't there... 339 /* Create the path to the file, just in case it isn't there...
340 * This should not screw up path permissions or anything. */ 340 * This should not screw up path permissions or anything. */
341 create_path(header->name, 0777); 341 char *dir = dirname (header->name);
342 make_directory (dir, -1, FILEUTILS_RECUR);
343 free (dir);
342 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, 344 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
343 header->mode & ~S_IFMT)) < 0) { 345 header->mode & ~S_IFMT)) < 0) {
344 error_msg(io_error, header->name, strerror(errno)); 346 error_msg(io_error, header->name, strerror(errno));
@@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
397 if (extractFlag==FALSE || tostdoutFlag==TRUE) 399 if (extractFlag==FALSE || tostdoutFlag==TRUE)
398 return( TRUE); 400 return( TRUE);
399 401
400 if (create_path(header->name, header->mode) != TRUE) { 402 if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) {
401 perror_msg("%s: Cannot mkdir", header->name);
402 return( FALSE); 403 return( FALSE);
403 } 404 }
404 /* make the final component, just in case it was 405 /* make the final component, just in case it was
diff --git a/coreutils/dirname.c b/coreutils/dirname.c
index 935a8313c..b534e6950 100644
--- a/coreutils/dirname.c
+++ b/coreutils/dirname.c
@@ -30,21 +30,11 @@
30 30
31extern int dirname_main(int argc, char **argv) 31extern int dirname_main(int argc, char **argv)
32{ 32{
33 char* s;
34
35 if ((argc < 2) || (**(argv + 1) == '-')) 33 if ((argc < 2) || (**(argv + 1) == '-'))
36 show_usage(); 34 show_usage();
37 argv++; 35 argv++;
38 36
39 s=*argv+strlen(*argv)-1; 37 puts (dirname (argv[0]));
40 while (s > *argv && *s == '/') { 38
41 *s-- = '\0';
42 }
43 s = strrchr(*argv, '/');
44 if (s != NULL && s == *argv)
45 s[1] = '\0';
46 else if (s != NULL)
47 *s = '\0';
48 puts(s ? *argv : ".");
49 return EXIT_SUCCESS; 39 return EXIT_SUCCESS;
50} 40}
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c
index d78f57e2b..03c49f098 100644
--- a/coreutils/mkdir.c
+++ b/coreutils/mkdir.c
@@ -2,8 +2,7 @@
2/* 2/*
3 * Mini mkdir implementation for busybox 3 * Mini mkdir implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000,2001 by Lineo, inc. 5 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * 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 8 * it under the terms of the GNU General Public License as published by
@@ -21,79 +20,45 @@
21 * 20 *
22 */ 21 */
23 22
24#include <stdio.h>
25#include <errno.h> 23#include <errno.h>
26#include <string.h> 24#include <getopt.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <fcntl.h>
28#include <unistd.h>
27#include <stdlib.h> 29#include <stdlib.h>
28#include "busybox.h" 30#include <string.h>
29
30
31static int parentFlag = FALSE;
32static mode_t mode = 0777;
33 31
32#include "busybox.h"
34 33
35extern int mkdir_main(int argc, char **argv) 34extern int mkdir_main (int argc, char **argv)
36{ 35{
37 int i = FALSE; 36 mode_t mode = -1;
37 int flags = 0;
38 int status = 0;
39 int i, opt;
38 40
39 argc--; 41 while ((opt = getopt (argc, argv, "m:p")) != -1) {
40 argv++; 42 switch (opt) {
41 43 case 'm':
42 /* Parse any options */ 44 mode = 0777;
43 while (argc > 0 && **argv == '-') { 45 if (!parse_mode (optarg, &mode))
44 while (i == FALSE && *++(*argv)) { 46 error_msg_and_die ("invalid mode `%s'", optarg);
45 switch (**argv) { 47 break;
46 case 'm': 48 case 'p':
47 if (--argc == 0) 49 flags |= FILEUTILS_RECUR;
48 show_usage(); 50 break;
49 /* Find the specified modes */ 51 default:
50 mode = 0; 52 show_usage ();
51 if (parse_mode(*(++argv), &mode) == FALSE) {
52 error_msg_and_die("Unknown mode: %s", *argv);
53 }
54 /* Set the umask for this process so it doesn't
55 * screw up whatever the user just entered. */
56 umask(0);
57 i = TRUE;
58 break;
59 case 'p':
60 parentFlag = TRUE;
61 break;
62 default:
63 show_usage();
64 }
65 } 53 }
66 argc--;
67 argv++;
68 } 54 }
69 55
70 if (argc < 1) { 56 if (optind == argc)
71 show_usage(); 57 show_usage ();
72 }
73 58
74 while (argc > 0) { 59 for (i = optind; i < argc; i++)
75 int status; 60 if (make_directory (argv[i], mode, flags) < 0)
76 struct stat statBuf; 61 status = 1;
77 char buf[BUFSIZ + 1];
78 62
79 if (strlen(*argv) > BUFSIZ - 1) { 63 return status;
80 error_msg_and_die(name_too_long);
81 }
82 strcpy(buf, *argv);
83 status = stat(buf, &statBuf);
84 if (parentFlag == FALSE && status != -1 && errno != ENOENT) {
85 error_msg_and_die("%s: File exists", buf);
86 }
87 if (parentFlag == TRUE) {
88 strcat(buf, "/");
89 create_path(buf, mode);
90 } else {
91 if (mkdir(buf, mode) != 0 && parentFlag == FALSE) {
92 perror_msg_and_die(buf);
93 }
94 }
95 argc--;
96 argv++;
97 }
98 return EXIT_SUCCESS;
99} 64}
diff --git a/dirname.c b/dirname.c
index 935a8313c..b534e6950 100644
--- a/dirname.c
+++ b/dirname.c
@@ -30,21 +30,11 @@
30 30
31extern int dirname_main(int argc, char **argv) 31extern int dirname_main(int argc, char **argv)
32{ 32{
33 char* s;
34
35 if ((argc < 2) || (**(argv + 1) == '-')) 33 if ((argc < 2) || (**(argv + 1) == '-'))
36 show_usage(); 34 show_usage();
37 argv++; 35 argv++;
38 36
39 s=*argv+strlen(*argv)-1; 37 puts (dirname (argv[0]));
40 while (s > *argv && *s == '/') { 38
41 *s-- = '\0';
42 }
43 s = strrchr(*argv, '/');
44 if (s != NULL && s == *argv)
45 s[1] = '\0';
46 else if (s != NULL)
47 *s = '\0';
48 puts(s ? *argv : ".");
49 return EXIT_SUCCESS; 39 return EXIT_SUCCESS;
50} 40}
diff --git a/dpkg.c b/dpkg.c
index 4224672ec..7dd46be0c 100644
--- a/dpkg.c
+++ b/dpkg.c
@@ -837,7 +837,7 @@ extern int dpkg_main(int argc, char **argv)
837 optind++; 837 optind++;
838 } 838 }
839 839
840 create_path(infodir, S_IRWXU); 840 make_directory(infodir, S_IRWXU, FILEUTILS_RECUR);
841 841
842 status = status_read(); 842 status = status_read();
843 843
diff --git a/editors/sed.c b/editors/sed.c
index e7208b0b5..89b16682f 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r
195 return idx; 195 return idx;
196} 196}
197 197
198static char *strdup_substr(const char *str, int start, int end)
199{
200 int size = end - start + 1;
201 char *newstr = xmalloc(size);
202 memcpy(newstr, str+start, size-1);
203 newstr[size-1] = '\0';
204 return newstr;
205}
206
207static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) 198static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
208{ 199{
209 int oldidx, cflags = REG_NEWLINE; 200 int oldidx, cflags = REG_NEWLINE;
diff --git a/include/libbb.h b/include/libbb.h
index e42ca9f2b..c83cb7e7c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid);
243 243
244extern struct hostent *xgethostbyname(const char *name); 244extern struct hostent *xgethostbyname(const char *name);
245 245
246char *dirname (const char *path);
247char *strdup_substr (const char *s, int start, int end);
248int make_directory (char *path, mode_t mode, int flags);
249
246#define CT_AUTO 0 250#define CT_AUTO 0
247#define CT_UNIX2DOS 1 251#define CT_UNIX2DOS 1
248#define CT_DOS2UNIX 2 252#define CT_DOS2UNIX 2
diff --git a/libbb/create_path.c b/libbb/create_path.c
deleted file mode 100644
index 328afc351..000000000
--- a/libbb/create_path.c
+++ /dev/null
@@ -1,71 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
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 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
25 *
26 */
27
28#include <stdio.h>
29#include <errno.h>
30#include <string.h>
31#include "libbb.h"
32
33/*
34 * Attempt to create the directories along the specified path, except for
35 * the final component. The mode is given for the final directory only,
36 * while all previous ones get default protections. Errors are not reported
37 * here, as failures to restore files can be reported later.
38 */
39extern int create_path(const char *name, int mode)
40{
41 char *cp;
42 char *cpOld;
43 char buf[BUFSIZ + 1];
44 int retVal = 0;
45
46 strcpy(buf, name);
47 for (cp = buf; *cp == '/'; cp++);
48 cp = strchr(cp, '/');
49 while (cp) {
50 cpOld = cp;
51 cp = strchr(cp + 1, '/');
52 *cpOld = '\0';
53 retVal = mkdir(buf, cp ? 0777 : mode);
54 if (retVal != 0 && errno != EEXIST) {
55 perror_msg("%s", buf);
56 return FALSE;
57 }
58 *cpOld = '/';
59 }
60 return TRUE;
61}
62
63
64/* END CODE */
65/*
66Local Variables:
67c-file-style: "linux"
68c-basic-offset: 4
69tab-width: 4
70End:
71*/
diff --git a/libbb/dirname.c b/libbb/dirname.c
new file mode 100644
index 000000000..2e89fc17a
--- /dev/null
+++ b/libbb/dirname.c
@@ -0,0 +1,48 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini dirname function.
4 *
5 * Copyright (C) 2001 Matt Kraai.
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#include "libbb.h"
23
24/* Return a string on the heap containing the directory component of PATH. */
25
26char *dirname(const char *path)
27{
28 const char *s;
29
30 /* Go to the end of the string. */
31 s = path + strlen(path) - 1;
32
33 /* Strip off trailing /s (unless it is also the leading /). */
34 while (path < s && s[0] == '/')
35 s--;
36
37 /* Strip the last component. */
38 while (path <= s && s[0] != '/')
39 s--;
40
41 while (path < s && s[0] == '/')
42 s--;
43
44 if (s < path)
45 return xstrdup (".");
46 else
47 return strdup_substr (path, 0, s - path + 1);
48}
diff --git a/libbb/libbb.h b/libbb/libbb.h
index e42ca9f2b..c83cb7e7c 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -243,6 +243,10 @@ extern FILE *gz_open(FILE *compressed_file, int *pid);
243 243
244extern struct hostent *xgethostbyname(const char *name); 244extern struct hostent *xgethostbyname(const char *name);
245 245
246char *dirname (const char *path);
247char *strdup_substr (const char *s, int start, int end);
248int make_directory (char *path, mode_t mode, int flags);
249
246#define CT_AUTO 0 250#define CT_AUTO 0
247#define CT_UNIX2DOS 1 251#define CT_UNIX2DOS 1
248#define CT_DOS2UNIX 2 252#define CT_DOS2UNIX 2
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
new file mode 100644
index 000000000..e2e28a8cd
--- /dev/null
+++ b/libbb/make_directory.c
@@ -0,0 +1,66 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini make_directory implementation for busybox
4 *
5 * Copyright (C) 2001 Matt Kraai.
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 <fcntl.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <unistd.h>
28
29#include "libbb.h"
30
31/* Create the directory PATH with mode MODE, or the default if MODE is -1.
32 * Also create parent directories as necessary if flags contains
33 * FILEUTILS_RECUR. */
34
35int make_directory (char *path, mode_t mode, int flags)
36{
37 if (!(flags & FILEUTILS_RECUR)) {
38 if (mkdir (path, 0777) < 0) {
39 perror_msg ("Cannot create directory `%s'", path);
40 return -1;
41 }
42
43 if (mode != -1 && chmod (path, mode) < 0) {
44 perror_msg ("Cannot set permissions of directory `%s'", path);
45 return -1;
46 }
47 } else {
48 struct stat st;
49
50 if (stat (path, &st) < 0 && errno == ENOENT) {
51 char *parent = dirname (path);
52 mode_t mask = umask (0);
53 umask (mask);
54
55 if (make_directory (parent, (0777 & ~mask) | 0300,
56 FILEUTILS_RECUR) < 0)
57 return -1;
58 free (parent);
59
60 if (make_directory (path, mode, 0) < 0)
61 return -1;
62 }
63 }
64
65 return 0;
66}
diff --git a/libbb/strdup_substr.c b/libbb/strdup_substr.c
new file mode 100644
index 000000000..4542d5fbe
--- /dev/null
+++ b/libbb/strdup_substr.c
@@ -0,0 +1,32 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini strdup_substr function.
4 *
5 * Copyright (C) 2001 Mark Whitley.
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/* Return a substring of STR, starting at index START and ending at END,
23 * allocated on the heap. */
24
25char *strdup_substr(const char *str, int start, int end)
26{
27 int size = end - start + 1;
28 char *newstr = xmalloc(size);
29 memcpy(newstr, str+start, size-1);
30 newstr[size-1] = '\0';
31 return newstr;
32}
diff --git a/mkdir.c b/mkdir.c
index d78f57e2b..03c49f098 100644
--- a/mkdir.c
+++ b/mkdir.c
@@ -2,8 +2,7 @@
2/* 2/*
3 * Mini mkdir implementation for busybox 3 * Mini mkdir implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000,2001 by Lineo, inc. 5 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * 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 8 * it under the terms of the GNU General Public License as published by
@@ -21,79 +20,45 @@
21 * 20 *
22 */ 21 */
23 22
24#include <stdio.h>
25#include <errno.h> 23#include <errno.h>
26#include <string.h> 24#include <getopt.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27#include <fcntl.h>
28#include <unistd.h>
27#include <stdlib.h> 29#include <stdlib.h>
28#include "busybox.h" 30#include <string.h>
29
30
31static int parentFlag = FALSE;
32static mode_t mode = 0777;
33 31
32#include "busybox.h"
34 33
35extern int mkdir_main(int argc, char **argv) 34extern int mkdir_main (int argc, char **argv)
36{ 35{
37 int i = FALSE; 36 mode_t mode = -1;
37 int flags = 0;
38 int status = 0;
39 int i, opt;
38 40
39 argc--; 41 while ((opt = getopt (argc, argv, "m:p")) != -1) {
40 argv++; 42 switch (opt) {
41 43 case 'm':
42 /* Parse any options */ 44 mode = 0777;
43 while (argc > 0 && **argv == '-') { 45 if (!parse_mode (optarg, &mode))
44 while (i == FALSE && *++(*argv)) { 46 error_msg_and_die ("invalid mode `%s'", optarg);
45 switch (**argv) { 47 break;
46 case 'm': 48 case 'p':
47 if (--argc == 0) 49 flags |= FILEUTILS_RECUR;
48 show_usage(); 50 break;
49 /* Find the specified modes */ 51 default:
50 mode = 0; 52 show_usage ();
51 if (parse_mode(*(++argv), &mode) == FALSE) {
52 error_msg_and_die("Unknown mode: %s", *argv);
53 }
54 /* Set the umask for this process so it doesn't
55 * screw up whatever the user just entered. */
56 umask(0);
57 i = TRUE;
58 break;
59 case 'p':
60 parentFlag = TRUE;
61 break;
62 default:
63 show_usage();
64 }
65 } 53 }
66 argc--;
67 argv++;
68 } 54 }
69 55
70 if (argc < 1) { 56 if (optind == argc)
71 show_usage(); 57 show_usage ();
72 }
73 58
74 while (argc > 0) { 59 for (i = optind; i < argc; i++)
75 int status; 60 if (make_directory (argv[i], mode, flags) < 0)
76 struct stat statBuf; 61 status = 1;
77 char buf[BUFSIZ + 1];
78 62
79 if (strlen(*argv) > BUFSIZ - 1) { 63 return status;
80 error_msg_and_die(name_too_long);
81 }
82 strcpy(buf, *argv);
83 status = stat(buf, &statBuf);
84 if (parentFlag == FALSE && status != -1 && errno != ENOENT) {
85 error_msg_and_die("%s: File exists", buf);
86 }
87 if (parentFlag == TRUE) {
88 strcat(buf, "/");
89 create_path(buf, mode);
90 } else {
91 if (mkdir(buf, mode) != 0 && parentFlag == FALSE) {
92 perror_msg_and_die(buf);
93 }
94 }
95 argc--;
96 argv++;
97 }
98 return EXIT_SUCCESS;
99} 64}
diff --git a/sed.c b/sed.c
index e7208b0b5..89b16682f 100644
--- a/sed.c
+++ b/sed.c
@@ -195,15 +195,6 @@ static int get_address(struct sed_cmd *sed_cmd, const char *str, int *linenum, r
195 return idx; 195 return idx;
196} 196}
197 197
198static char *strdup_substr(const char *str, int start, int end)
199{
200 int size = end - start + 1;
201 char *newstr = xmalloc(size);
202 memcpy(newstr, str+start, size-1);
203 newstr[size-1] = '\0';
204 return newstr;
205}
206
207static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) 198static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
208{ 199{
209 int oldidx, cflags = REG_NEWLINE; 200 int oldidx, cflags = REG_NEWLINE;
diff --git a/tar.c b/tar.c
index 55fb12c2c..e68194ff7 100644
--- a/tar.c
+++ b/tar.c
@@ -338,7 +338,9 @@ tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
338 if (extractFlag==TRUE && tostdoutFlag==FALSE) { 338 if (extractFlag==TRUE && tostdoutFlag==FALSE) {
339 /* Create the path to the file, just in case it isn't there... 339 /* Create the path to the file, just in case it isn't there...
340 * This should not screw up path permissions or anything. */ 340 * This should not screw up path permissions or anything. */
341 create_path(header->name, 0777); 341 char *dir = dirname (header->name);
342 make_directory (dir, -1, FILEUTILS_RECUR);
343 free (dir);
342 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, 344 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY,
343 header->mode & ~S_IFMT)) < 0) { 345 header->mode & ~S_IFMT)) < 0) {
344 error_msg(io_error, header->name, strerror(errno)); 346 error_msg(io_error, header->name, strerror(errno));
@@ -397,8 +399,7 @@ tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
397 if (extractFlag==FALSE || tostdoutFlag==TRUE) 399 if (extractFlag==FALSE || tostdoutFlag==TRUE)
398 return( TRUE); 400 return( TRUE);
399 401
400 if (create_path(header->name, header->mode) != TRUE) { 402 if (make_directory(header->name, header->mode, FILEUTILS_RECUR) < 0) {
401 perror_msg("%s: Cannot mkdir", header->name);
402 return( FALSE); 403 return( FALSE);
403 } 404 }
404 /* make the final component, just in case it was 405 /* make the final component, just in case it was