aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/libbb.h1
-rw-r--r--libbb/simplify_path.c78
-rw-r--r--mount.c11
-rw-r--r--util-linux/mount.c11
6 files changed, 90 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index 44d5b5e93..242dac4b9 100644
--- 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)
diff --git a/include/libbb.h b/include/libbb.h
index 04ed2ae82..a7e38325b 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -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/libbb.h b/libbb/libbb.h
index 04ed2ae82..a7e38325b 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -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
index 000000000..4641bae88
--- /dev/null
+++ b/libbb/simplify_path.c
@@ -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 eb6091f30..6ffce2bf1 100644
--- 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");
diff --git a/util-linux/mount.c b/util-linux/mount.c
index eb6091f30..6ffce2bf1 100644
--- a/util-linux/mount.c
+++ b/util-linux/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");