X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Futil.c;h=65cb36087dc71e752e68d4cde17698aae940870a;hb=a39a859a3066bb637de16edd4f26eab77fb594a6;hp=c9ef8b1592dafa186aa3900b097fe832714637c1;hpb=f38b84ea2b6b61d309c604faedd41ab3b0fcf16b;p=openvswitch diff --git a/lib/util.c b/lib/util.c index c9ef8b15..65cb3608 100644 --- a/lib/util.c +++ b/lib/util.c @@ -168,8 +168,10 @@ ovs_error(int err_no, const char *format, ...) va_start(args, format); vfprintf(stderr, format, args); va_end(args); - if (err_no != 0) - fprintf(stderr, " (%s)", strerror(err_no)); + if (err_no != 0) { + fprintf(stderr, " (%s)", + err_no == EOF ? "end of file" : strerror(err_no)); + } putc('\n', stderr); errno = save_errno; @@ -355,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); + } +}