diff options
| author | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-06-21 19:41:37 +0000 |
|---|---|---|
| committer | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-06-21 19:41:37 +0000 |
| commit | 5f0f7d3428d4c8cc51fef8fcb4e7bf0ac616dab2 (patch) | |
| tree | 3cdbaddffecc92649215fdc71a43b4e8e86b7ea3 /libbb | |
| parent | ce5c25da381038fa6d2640a2f4aa77771ca2ee43 (diff) | |
| download | busybox-w32-5f0f7d3428d4c8cc51fef8fcb4e7bf0ac616dab2.tar.gz busybox-w32-5f0f7d3428d4c8cc51fef8fcb4e7bf0ac616dab2.tar.bz2 busybox-w32-5f0f7d3428d4c8cc51fef8fcb4e7bf0ac616dab2.zip | |
Rewrote mkdir (and touched lots of things in the process).
git-svn-id: svn://busybox.net/trunk/busybox@2873 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/create_path.c | 71 | ||||
| -rw-r--r-- | libbb/dirname.c | 48 | ||||
| -rw-r--r-- | libbb/libbb.h | 4 | ||||
| -rw-r--r-- | libbb/make_directory.c | 66 | ||||
| -rw-r--r-- | libbb/strdup_substr.c | 32 |
5 files changed, 150 insertions, 71 deletions
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 | */ | ||
| 39 | extern 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 | /* | ||
| 66 | Local Variables: | ||
| 67 | c-file-style: "linux" | ||
| 68 | c-basic-offset: 4 | ||
| 69 | tab-width: 4 | ||
| 70 | End: | ||
| 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 | |||
| 26 | char *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 | ||
| 244 | extern struct hostent *xgethostbyname(const char *name); | 244 | extern struct hostent *xgethostbyname(const char *name); |
| 245 | 245 | ||
| 246 | char *dirname (const char *path); | ||
| 247 | char *strdup_substr (const char *s, int start, int end); | ||
| 248 | int 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 | |||
| 35 | int 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 | |||
| 25 | char *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 | } | ||
