X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Futil.c;h=65cb36087dc71e752e68d4cde17698aae940870a;hb=45a7de56bbcb2adfabf2082b1133e768777d44d6;hp=6c152e24d1231247af779557ca18de4e5b668ebe;hpb=0fec26b00be202f828193a66ed35c842389588f4;p=openvswitch diff --git a/lib/util.c b/lib/util.c index 6c152e24..65cb3608 100644 --- a/lib/util.c +++ b/lib/util.c @@ -357,3 +357,29 @@ hexit_value(int c) NOT_REACHED(); } + +/* Returns the directory name portion of 'file_name' as a malloc()'d string, + * similar to the POSIX dirname() function but thread-safe. */ +char * +dir_name(const char *file_name) +{ + size_t len = strlen(file_name); + while (len > 0 && file_name[len - 1] == '/') { + len--; + } + while (len > 0 && file_name[len - 1] != '/') { + len--; + } + while (len > 0 && file_name[len - 1] == '/') { + len--; + } + if (!len) { + return xstrdup((file_name[0] == '/' + && file_name[1] == '/' + && file_name[2] != '/') ? "//" + : file_name[0] == '/' ? "/" + : "."); + } else { + return xmemdup0(file_name, len); + } +}