Mercurial > hg > octave-lojdl > gnulib-hg
changeset 12067:c1ef4ce880fc
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 <ebb9@byu.net>
author | Eric Blake <ebb9@byu.net> |
---|---|
date | Wed, 09 Sep 2009 06:24:28 -0600 |
parents | 5e590c10bc80 |
children | 14cddb59cb05 |
files | ChangeLog lib/dirname.c lib/dirname.h |
diffstat | 3 files changed, 29 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2009-09-23 Eric Blake <ebb9@byu.net> + 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
--- 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 <stdlib.h> #include <string.h> #include "xalloc.h" @@ -57,9 +58,9 @@ 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 @@ 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; +}
--- 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);