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)
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
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
--- /dev/null
+/* 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;
+}
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");
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");