aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-08-10 15:05:27 +0000
committerMatt Kraai <kraai@debian.org>2001-08-10 15:05:27 +0000
commita7cecbc9170eed93d365c0c38e8ba1477ab0a40c (patch)
tree3541c27248b0d143cb06b9f61472b568414cb110
parent4fb0b517035ddc189bd696e3175bf18dbc37d441 (diff)
downloadbusybox-w32-a7cecbc9170eed93d365c0c38e8ba1477ab0a40c.tar.gz
busybox-w32-a7cecbc9170eed93d365c0c38e8ba1477ab0a40c.tar.bz2
busybox-w32-a7cecbc9170eed93d365c0c38e8ba1477ab0a40c.zip
Commit Vladimir's simplify_path.
-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 \
250trim.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ 250trim.c unzip.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \
251xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ 251xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \
252copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ 252copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \
253dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c 253dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \
254simplify_path.c
254LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) 255LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
255ifeq ($(strip $(BB_SRC_DIR)),) 256ifeq ($(strip $(BB_SRC_DIR)),)
256 LIBBB_CFLAGS += -I$(LIBBB) 257 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);
264int make_directory (char *path, long mode, int flags); 264int make_directory (char *path, long mode, int flags);
265 265
266const char *u_signal_names(const char *str_sig, int *signo, int startnum); 266const char *u_signal_names(const char *str_sig, int *signo, int startnum);
267char *simplify_path(const char *path);
267 268
268#define CT_AUTO 0 269#define CT_AUTO 0
269#define CT_UNIX2DOS 1 270#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);
264int make_directory (char *path, long mode, int flags); 264int make_directory (char *path, long mode, int flags);
265 265
266const char *u_signal_names(const char *str_sig, int *signo, int startnum); 266const char *u_signal_names(const char *str_sig, int *signo, int startnum);
267char *simplify_path(const char *path);
267 268
268#define CT_AUTO 0 269#define CT_AUTO 0
269#define CT_UNIX2DOS 1 270#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 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * simplify_path implementation for busybox
4 *
5 *
6 * Copyright (C) 2001 Vladimir N. Oleynik <dzo@simtreas.ru>
7 *
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 */
24
25#include <stdlib.h>
26
27#include "libbb.h"
28
29static inline char *strcpy_overlap(char *dst, const char *src)
30{
31 char *ptr = dst;
32
33 do *dst++ = *src; while (*src++);
34 return ptr;
35}
36
37char *simplify_path(const char *path)
38{
39 char *s, *start, *next;
40
41 if (path[0] == '/')
42 start = xstrdup(path);
43 else {
44 s = xgetcwd(NULL);
45 start = concat_path_file(s, path);
46 free(s);
47 }
48 s = start;
49 /* remove . and .. */
50 while(*s) {
51 if(*s++ == '/' && (*s == '/' || *s == 0)) {
52 /* remove duplicate and trailing slashes */
53 s = strcpy_overlap(s-1, s);
54 }
55 else if(*(s-1) == '.' && *(s-2)=='/') {
56 if(*s == '/' || *s == 0) {
57 /* remove . */
58 s = strcpy_overlap(s-1, s); /* maybe set // */
59 s--;
60 } else if(*s == '.') {
61 next = s+1; /* set after ".." */
62 if(*next == '/' || *next == 0) { /* "../" */
63 if((s-=2) > start)
64 /* skip previous dir */
65 do s--; while(*s != '/');
66 /* remove previous dir */
67 strcpy_overlap(s, next);
68 }
69
70 }
71 }
72 }
73 if(start[0]==0) {
74 start[0]='/';
75 start[1]=0;
76 }
77 return start;
78}
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)
395 if (optind < argc) { 395 if (optind < argc) {
396 /* if device is a filename get its real path */ 396 /* if device is a filename get its real path */
397 if (stat(argv[optind], &statbuf) == 0) { 397 if (stat(argv[optind], &statbuf) == 0) {
398 realpath(argv[optind], device); 398 device = simplify_path(argv[optind]);
399 } else { 399 } else {
400 safe_strncpy(device, argv[optind], PATH_MAX); 400 safe_strncpy(device, argv[optind], PATH_MAX);
401 } 401 }
402 } 402 }
403 403
404 if (optind + 1 < argc) { 404 if (optind + 1 < argc)
405 if (realpath(argv[optind + 1], directory) == NULL) { 405 directory = simplify_path(argv[optind + 1]);
406 perror_msg_and_die("%s", directory); 406
407 }
408 }
409
410 if (all == TRUE || optind + 1 == argc) { 407 if (all == TRUE || optind + 1 == argc) {
411 struct mntent *m = NULL; 408 struct mntent *m = NULL;
412 FILE *f = setmntent("/etc/fstab", "r"); 409 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)
395 if (optind < argc) { 395 if (optind < argc) {
396 /* if device is a filename get its real path */ 396 /* if device is a filename get its real path */
397 if (stat(argv[optind], &statbuf) == 0) { 397 if (stat(argv[optind], &statbuf) == 0) {
398 realpath(argv[optind], device); 398 device = simplify_path(argv[optind]);
399 } else { 399 } else {
400 safe_strncpy(device, argv[optind], PATH_MAX); 400 safe_strncpy(device, argv[optind], PATH_MAX);
401 } 401 }
402 } 402 }
403 403
404 if (optind + 1 < argc) { 404 if (optind + 1 < argc)
405 if (realpath(argv[optind + 1], directory) == NULL) { 405 directory = simplify_path(argv[optind + 1]);
406 perror_msg_and_die("%s", directory); 406
407 }
408 }
409
410 if (all == TRUE || optind + 1 == argc) { 407 if (all == TRUE || optind + 1 == argc) {
411 struct mntent *m = NULL; 408 struct mntent *m = NULL;
412 FILE *f = setmntent("/etc/fstab", "r"); 409 FILE *f = setmntent("/etc/fstab", "r");