From: Eric Blake Date: Wed, 9 Sep 2009 12:24:28 +0000 (-0600) Subject: dirname: add library-safe mdir_name X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6dc1761b3e928d2de0a6116cd933b3147ffd7d8;p=pspp dirname: add library-safe mdir_name A library-safe dir_name is nice, especially alongside mfile_name_concat. Someday, we should rearrange the .o files so that linking in mdir_name does not suck in xalloc_die, but for now, the only planned client of mdir_name (at-func2) is already using xalloc_die. * lib/dirname.h (mdir_name): New prototype. * lib/dirname.c (dir_name): Move guts... (mdir_name): ...to new function that avoids xalloc_die. Signed-off-by: Eric Blake --- diff --git a/ChangeLog b/ChangeLog index ee180ac44e..0ba1bbe5f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2009-09-23 Eric Blake + dirname: add library-safe mdir_name + * lib/dirname.h (mdir_name): New prototype. + * lib/dirname.c (dir_name): Move guts... + (mdir_name): ...to new function that avoids xalloc_die. + fchdir: another mingw fix * modules/fchdir (Depends-on): Drop canonicalize-lgpl. * lib/fchdir.c (get_name): New helper method; skips canonicalize diff --git a/lib/dirname.c b/lib/dirname.c index c27e5b5d8b..20dcaf5b87 100644 --- a/lib/dirname.c +++ b/lib/dirname.c @@ -1,7 +1,7 @@ /* dirname.c -- return all but the last element in a file name - Copyright (C) 1990, 1998, 2000, 2001, 2003, 2004, 2005, 2006 Free Software - Foundation, Inc. + Copyright (C) 1990, 1998, 2000, 2001, 2003, 2004, 2005, 2006, 2009 + Free Software Foundation, Inc. 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 @@ -20,6 +20,7 @@ #include "dirname.h" +#include #include #include "xalloc.h" @@ -57,9 +58,9 @@ dir_len (char const *file) since it has different meanings in different environments. In some environments the builtin `dirname' modifies its argument. - Return the leading directories part of FILE, allocated with xmalloc. + Return the leading directories part of FILE, allocated with malloc. Works properly even if there are trailing slashes (by effectively - ignoring them). Unlike POSIX dirname(), FILE cannot be NULL. + ignoring them). Return NULL on failure. If lstat (FILE) would succeed, then { chdir (dir_name (FILE)); lstat (base_name (FILE)); } will access the same file. Likewise, @@ -68,17 +69,32 @@ dir_len (char const *file) to "foo" in the same directory FILE was in. */ char * -dir_name (char const *file) +mdir_name (char const *file) { size_t length = dir_len (file); bool append_dot = (length == 0 || (FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE && length == FILE_SYSTEM_PREFIX_LEN (file) && file[2] != '\0' && ! ISSLASH (file[2]))); - char *dir = xmalloc (length + append_dot + 1); + char *dir = malloc (length + append_dot + 1); + if (!dir) + return NULL; memcpy (dir, file, length); if (append_dot) dir[length++] = '.'; dir[length] = '\0'; return dir; } + +/* Just like mdir_name, above, except, rather than + returning NULL upon malloc failure, here, we report the + "memory exhausted" condition and exit. */ + +char * +dir_name (char const *file) +{ + char *result = mdir_name (file); + if (!result) + xalloc_die (); + return result; +} diff --git a/lib/dirname.h b/lib/dirname.h index f592350b81..90a1f0c2c1 100644 --- a/lib/dirname.h +++ b/lib/dirname.h @@ -1,6 +1,6 @@ /* Take file names apart into directory and base names. - Copyright (C) 1998, 2001, 2003-2006 Free Software Foundation, Inc. + Copyright (C) 1998, 2001, 2003-2006, 2009 Free Software Foundation, Inc. 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 @@ -59,6 +59,7 @@ # define IS_RELATIVE_FILE_NAME(F) (! IS_ABSOLUTE_FILE_NAME (F)) char *base_name (char const *file); +char *mdir_name (char const *file); char *dir_name (char const *file); size_t base_len (char const *file); size_t dir_len (char const *file);