Hi, attached a alternative version of dirname() so that we don't need libgen.h for it. Tested only with dirname applet and seems to work. Needs to be tested with other code where it is called. Hints, critics and improvements are welcome.
Ciao, Tito /* vi: set sw=4 ts=4: */ /* * bb_dirname implementation for busybox * * Copyright (C) 2011 Tito Ragusa <[email protected]> * * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ #include "libbb.h" /* The following list of examples (taken from SUSv2) shows the strings * returned by dirname() for different paths: * * path dirname * "/usr/lib" "/usr" * "/usr/" "/" * "/" "/" * "usr" "." * "." "." * ".." "." * This examples are from coreutils dirname * "usr/lib "usr" * "" "." * " " "." * This is added by me * NULL "." */ char* FAST_FUNC bb_dirname(char *path) { char *p; char *last; if (path) { last = last_char_is(path, '/'); if (last && last != path) *last = '\0'; p = strrchr(path , '/'); if (p) { if (p == path) p++; *p = '\0'; return path; } } return (char *)"."; }
Alternative dirname implementation for busybox to remove libgen.h. Signed-off-by: Tito Ragusa <[email protected]> --- include/libbb.h.original 2011-08-29 01:52:50.000000000 +0200 +++ include/libbb.h 2011-08-29 01:53:37.000000000 +0200 @@ -136,9 +136,8 @@ /* This is declared here rather than #including <libgen.h> in order to avoid * confusing the two versions of basename. See the dirname/basename man page * for details. */ -#if !defined __FreeBSD__ -char *dirname(char *path); -#endif +char *bb_dirname(char *path) FAST_FUNC; +#define dirname bb_dirname #ifndef PATH_MAX # define PATH_MAX 256 #endif --- libbb/Kbuild.src.original 2011-08-29 01:51:59.000000000 +0200 +++ libbb/Kbuild.src 2011-08-29 01:33:13.000000000 +0200 @@ -29,6 +29,7 @@ lib-y += crc32.o lib-y += create_icmp6_socket.o lib-y += create_icmp_socket.o +lib-y += dirname.o lib-y += default_error_retval.o lib-y += device_open.o lib-y += dump.o --- /dev/null 2011-08-28 16:17:50.264384581 +0200 +++ libbb/dirname.c 2011-08-29 01:47:55.000000000 +0200 @@ -0,0 +1,50 @@ +/* vi: set sw=4 ts=4: */ +/* + * bb_dirname implementation for busybox + * + * Copyright (C) 2011 Tito Ragusa <[email protected]> + * + * Licensed under GPLv2 or later, see file LICENSE in this source tree. + */ + +#include "libbb.h" + +/* The following list of examples (taken from SUSv2) shows the strings + * returned by dirname() for different paths: + * + * path dirname + * "/usr/lib" "/usr" + * "/usr/" "/" + * "/" "/" + * "usr" "." + * "." "." + * ".." "." + * This examples are from coreutils dirname + * "usr/lib "usr" + * "" "." + * " " "." + * This is added by me + * NULL "." + */ + +char* FAST_FUNC bb_dirname(char *path) { + char *p; + char *last; + + if (path) { + last = last_char_is(path, '/'); + + if (last && last != path) + *last = '\0'; + + p = strrchr(path , '/'); + if (p) { + if (p == path) + p++; + *p = '\0'; + return path; + } + } + return (char *)"."; +} +
_______________________________________________ busybox mailing list [email protected] http://lists.busybox.net/mailman/listinfo/busybox
