Commit Vladimir's simplify_path.
authorMatt Kraai <kraai@debian.org>
Fri, 10 Aug 2001 15:05:27 +0000 (15:05 -0000)
committerMatt Kraai <kraai@debian.org>
Fri, 10 Aug 2001 15:05:27 +0000 (15:05 -0000)
Makefile
include/libbb.h
libbb/libbb.h
libbb/simplify_path.c [new file with mode: 0644]
mount.c
util-linux/mount.c

index 44d5b5e931a4b7f3b86e4bafde696968cc7dc4eb..242dac4b9ea906fede05ab21381588a415bc88f2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -250,7 +250,8 @@ safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \
 trim.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
 xgetcwd.c xreadlink.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 \
-dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c
+dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \
+simplify_path.c
 LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
 ifeq ($(strip $(BB_SRC_DIR)),)
     LIBBB_CFLAGS += -I$(LIBBB)
index 04ed2ae824ab9e4bf1c20f4bc8a2d7ddaeb74f5a..a7e38325b88115ffb76f6d3fa20f012a440e2d2f 100644 (file)
@@ -264,6 +264,7 @@ char *dirname (const char *path);
 int make_directory (char *path, long mode, int flags);
 
 const char *u_signal_names(const char *str_sig, int *signo, int startnum);
+char *simplify_path(const char *path);
 
 #define CT_AUTO        0
 #define CT_UNIX2DOS    1
index 04ed2ae824ab9e4bf1c20f4bc8a2d7ddaeb74f5a..a7e38325b88115ffb76f6d3fa20f012a440e2d2f 100644 (file)
@@ -264,6 +264,7 @@ char *dirname (const char *path);
 int make_directory (char *path, long mode, int flags);
 
 const char *u_signal_names(const char *str_sig, int *signo, int startnum);
+char *simplify_path(const char *path);
 
 #define CT_AUTO        0
 #define CT_UNIX2DOS    1
diff --git a/libbb/simplify_path.c b/libbb/simplify_path.c
new file mode 100644 (file)
index 0000000..4641bae
--- /dev/null
@@ -0,0 +1,78 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * simplify_path implementation for busybox
+ *
+ *
+ * Copyright (C) 2001  Vladimir N. Oleynik <dzo@simtreas.ru>
+ *
+ *
+ * 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 <stdlib.h>
+
+#include "libbb.h"
+
+static inline char *strcpy_overlap(char *dst, const char *src)
+{
+       char *ptr = dst;
+
+       do *dst++ = *src; while (*src++);
+       return ptr;
+}
+
+char *simplify_path(const char *path)
+{
+       char *s, *start, *next;
+
+       if (path[0] == '/')
+              start = xstrdup(path);
+       else {
+              s = xgetcwd(NULL);
+              start = concat_path_file(s, path);
+              free(s);
+       }
+       s = start;
+       /* remove . and .. */
+       while(*s) {
+               if(*s++ == '/' && (*s == '/' || *s == 0)) {
+                       /* remove duplicate and trailing slashes */
+                       s = strcpy_overlap(s-1, s);
+               }
+               else if(*(s-1) == '.' && *(s-2)=='/') {
+                       if(*s == '/' || *s == 0) {
+                               /* remove . */
+                               s = strcpy_overlap(s-1, s); /* maybe set // */
+                               s--;
+                       } else if(*s == '.') {
+                               next = s+1;     /* set after ".." */
+                               if(*next == '/' || *next == 0) {  /* "../" */
+                                       if((s-=2) > start)
+                                               /* skip previous dir */
+                                               do s--; while(*s != '/');
+                                       /* remove previous dir */
+                                       strcpy_overlap(s, next);
+                               }
+
+                       }
+               }
+       }
+       if(start[0]==0) {
+               start[0]='/';
+               start[1]=0;
+       }
+       return start;
+}
diff --git a/mount.c b/mount.c
index eb6091f30e3763dc7df780b6a820cb59f8ee04fc..6ffce2bf16f38a66f6885253e90d9114e2a4783b 100644 (file)
--- a/mount.c
+++ b/mount.c
@@ -395,18 +395,15 @@ extern int mount_main(int argc, char **argv)
        if (optind < argc) {
                /* if device is a filename get its real path */
                if (stat(argv[optind], &statbuf) == 0) {
-                       realpath(argv[optind], device);
+                       device = simplify_path(argv[optind]);
                } else {
                        safe_strncpy(device, argv[optind], PATH_MAX);
                }
        }
 
-       if (optind + 1 < argc) {
-               if (realpath(argv[optind + 1], directory) == NULL) {
-                       perror_msg_and_die("%s", directory);
-               }
-       }
-       
+       if (optind + 1 < argc)
+               directory = simplify_path(argv[optind + 1]);
+
        if (all == TRUE || optind + 1 == argc) {
                struct mntent *m = NULL;
                FILE *f = setmntent("/etc/fstab", "r");
index eb6091f30e3763dc7df780b6a820cb59f8ee04fc..6ffce2bf16f38a66f6885253e90d9114e2a4783b 100644 (file)
@@ -395,18 +395,15 @@ extern int mount_main(int argc, char **argv)
        if (optind < argc) {
                /* if device is a filename get its real path */
                if (stat(argv[optind], &statbuf) == 0) {
-                       realpath(argv[optind], device);
+                       device = simplify_path(argv[optind]);
                } else {
                        safe_strncpy(device, argv[optind], PATH_MAX);
                }
        }
 
-       if (optind + 1 < argc) {
-               if (realpath(argv[optind + 1], directory) == NULL) {
-                       perror_msg_and_die("%s", directory);
-               }
-       }
-       
+       if (optind + 1 < argc)
+               directory = simplify_path(argv[optind + 1]);
+
        if (all == TRUE || optind + 1 == argc) {
                struct mntent *m = NULL;
                FILE *f = setmntent("/etc/fstab", "r");