Assorted bug reports with a few fixes for busybox 1.19.0 - dirname

Tito farmatito at tiscali.it
Mon Aug 29 00:01:00 UTC 2011


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 <farmatito at tiscali.it>
 *
 * 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 *)".";
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: dirname.patch
Type: text/x-patch
Size: 2070 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20110829/8724feea/attachment.bin>


More information about the busybox mailing list