Commit Vladimir's simplify_path.

This commit is contained in:
Matt Kraai 2001-08-10 15:05:27 +00:00
parent 4fb0b51703
commit a7cecbc917
6 changed files with 90 additions and 15 deletions

View File

@ -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 \ 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 \ 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 \ 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)) LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
ifeq ($(strip $(BB_SRC_DIR)),) ifeq ($(strip $(BB_SRC_DIR)),)
LIBBB_CFLAGS += -I$(LIBBB) LIBBB_CFLAGS += -I$(LIBBB)

View File

@ -264,6 +264,7 @@ char *dirname (const char *path);
int make_directory (char *path, long mode, int flags); int make_directory (char *path, long mode, int flags);
const char *u_signal_names(const char *str_sig, int *signo, int startnum); 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_AUTO 0
#define CT_UNIX2DOS 1 #define CT_UNIX2DOS 1

View File

@ -264,6 +264,7 @@ char *dirname (const char *path);
int make_directory (char *path, long mode, int flags); int make_directory (char *path, long mode, int flags);
const char *u_signal_names(const char *str_sig, int *signo, int startnum); 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_AUTO 0
#define CT_UNIX2DOS 1 #define CT_UNIX2DOS 1

78
libbb/simplify_path.c Normal file
View File

@ -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;
}

11
mount.c
View File

@ -395,18 +395,15 @@ extern int mount_main(int argc, char **argv)
if (optind < argc) { if (optind < argc) {
/* if device is a filename get its real path */ /* if device is a filename get its real path */
if (stat(argv[optind], &statbuf) == 0) { if (stat(argv[optind], &statbuf) == 0) {
realpath(argv[optind], device); device = simplify_path(argv[optind]);
} else { } else {
safe_strncpy(device, argv[optind], PATH_MAX); safe_strncpy(device, argv[optind], PATH_MAX);
} }
} }
if (optind + 1 < argc) { if (optind + 1 < argc)
if (realpath(argv[optind + 1], directory) == NULL) { directory = simplify_path(argv[optind + 1]);
perror_msg_and_die("%s", directory);
}
}
if (all == TRUE || optind + 1 == argc) { if (all == TRUE || optind + 1 == argc) {
struct mntent *m = NULL; struct mntent *m = NULL;
FILE *f = setmntent("/etc/fstab", "r"); FILE *f = setmntent("/etc/fstab", "r");

View File

@ -395,18 +395,15 @@ extern int mount_main(int argc, char **argv)
if (optind < argc) { if (optind < argc) {
/* if device is a filename get its real path */ /* if device is a filename get its real path */
if (stat(argv[optind], &statbuf) == 0) { if (stat(argv[optind], &statbuf) == 0) {
realpath(argv[optind], device); device = simplify_path(argv[optind]);
} else { } else {
safe_strncpy(device, argv[optind], PATH_MAX); safe_strncpy(device, argv[optind], PATH_MAX);
} }
} }
if (optind + 1 < argc) { if (optind + 1 < argc)
if (realpath(argv[optind + 1], directory) == NULL) { directory = simplify_path(argv[optind + 1]);
perror_msg_and_die("%s", directory);
}
}
if (all == TRUE || optind + 1 == argc) { if (all == TRUE || optind + 1 == argc) {
struct mntent *m = NULL; struct mntent *m = NULL;
FILE *f = setmntent("/etc/fstab", "r"); FILE *f = setmntent("/etc/fstab", "r");