summaryrefslogtreecommitdiffhomepage
path: root/libbb
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 /libbb
parent091781e20eb055ac286b5a617d53a50c7d6c451e (diff)
Rewrote mkdir (and touched lots of things in the process).
Diffstat (limited to 'libbb')
-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
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 @@
-/* vi: set sw=4 ts=4: */
-/*
- * Utility routines.
- *
- * Copyright (C) tons of folks. Tracking down who wrote what
- * isn't something I'm going to worry about... If you wrote something
- * here, please feel free to acknowledge your work.
- *
- * 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
- *
- * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
- * Permission has been granted to redistribute this code under the GPL.
- *
- */
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include "libbb.h"
-
-/*
- * Attempt to create the directories along the specified path, except for
- * the final component. The mode is given for the final directory only,
- * while all previous ones get default protections. Errors are not reported
- * here, as failures to restore files can be reported later.
- */
-extern int create_path(const char *name, int mode)
-{
- char *cp;
- char *cpOld;
- char buf[BUFSIZ + 1];
- int retVal = 0;
-
- strcpy(buf, name);
- for (cp = buf; *cp == '/'; cp++);
- cp = strchr(cp, '/');
- while (cp) {
- cpOld = cp;
- cp = strchr(cp + 1, '/');
- *cpOld = '\0';
- retVal = mkdir(buf, cp ? 0777 : mode);
- if (retVal != 0 && errno != EEXIST) {
- perror_msg("%s", buf);
- return FALSE;
- }
- *cpOld = '/';
- }
- return TRUE;
-}
-
-
-/* END CODE */
-/*
-Local Variables:
-c-file-style: "linux"
-c-basic-offset: 4
-tab-width: 4
-End:
-*/
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 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini dirname function.
+ *
+ * Copyright (C) 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 "libbb.h"
+
+/* Return a string on the heap containing the directory component of PATH. */
+
+char *dirname(const char *path)
+{
+ const char *s;
+
+ /* Go to the end of the string. */
+ s = path + strlen(path) - 1;
+
+ /* Strip off trailing /s (unless it is also the leading /). */
+ while (path < s && s[0] == '/')
+ s--;
+
+ /* Strip the last component. */
+ while (path <= s && s[0] != '/')
+ s--;
+
+ while (path < s && s[0] == '/')
+ s--;
+
+ if (s < path)
+ return xstrdup (".");
+ else
+ return strdup_substr (path, 0, s - path + 1);
+}
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);
extern struct hostent *xgethostbyname(const char *name);
+char *dirname (const char *path);
+char *strdup_substr (const char *s, int start, int end);
+int make_directory (char *path, mode_t mode, int flags);
+
#define CT_AUTO 0
#define CT_UNIX2DOS 1
#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 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini make_directory implementation for busybox
+ *
+ * Copyright (C) 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 <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "libbb.h"
+
+/* Create the directory PATH with mode MODE, or the default if MODE is -1.
+ * Also create parent directories as necessary if flags contains
+ * FILEUTILS_RECUR. */
+
+int make_directory (char *path, mode_t mode, int flags)
+{
+ if (!(flags & FILEUTILS_RECUR)) {
+ if (mkdir (path, 0777) < 0) {
+ perror_msg ("Cannot create directory `%s'", path);
+ return -1;
+ }
+
+ if (mode != -1 && chmod (path, mode) < 0) {
+ perror_msg ("Cannot set permissions of directory `%s'", path);
+ return -1;
+ }
+ } else {
+ struct stat st;
+
+ if (stat (path, &st) < 0 && errno == ENOENT) {
+ char *parent = dirname (path);
+ mode_t mask = umask (0);
+ umask (mask);
+
+ if (make_directory (parent, (0777 & ~mask) | 0300,
+ FILEUTILS_RECUR) < 0)
+ return -1;
+ free (parent);
+
+ if (make_directory (path, mode, 0) < 0)
+ return -1;
+ }
+ }
+
+ return 0;
+}
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 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini strdup_substr function.
+ *
+ * Copyright (C) 2001 Mark Whitley.
+ *
+ * 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
+ */
+
+/* Return a substring of STR, starting at index START and ending at END,
+ * allocated on the heap. */
+
+char *strdup_substr(const char *str, int start, int end)
+{
+ int size = end - start + 1;
+ char *newstr = xmalloc(size);
+ memcpy(newstr, str+start, size-1);
+ newstr[size-1] = '\0';
+ return newstr;
+}