X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsavedir.c;h=e969407d32ee2b9a06fff5ba60bf6b4d41e2a934;hb=5972f7a7a266fadbaa462755260767966724c72a;hp=fce61da0d6abdc84b20357bee009aebbb833cb62;hpb=d87c39464604e74f580c7fae835be31a4c125c36;p=gnulib.git diff --git a/lib/savedir.c b/lib/savedir.c index fce61da0d..e969407d3 100644 --- a/lib/savedir.c +++ b/lib/savedir.c @@ -1,5 +1,5 @@ /* savedir.c -- save the list of files in a directory in a string - Copyright (C) 1990 Free Software Foundation, Inc. + Copyright (C) 1990, 1997, 1998, 1999, 2000 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 @@ -12,51 +12,61 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* Written by David MacKenzie . */ +/* Written by David MacKenzie . */ -#include -#ifdef DIRENT -#include -#ifdef direct -#undef direct +#if HAVE_CONFIG_H +# include #endif -#define direct dirent -#define NLENGTH(direct) (strlen((direct)->d_name)) -#else -#define NLENGTH(direct) ((direct)->d_namlen) -#ifdef USG -#ifdef SYSNDIR -#include -#else -#include + +#include + +#if HAVE_UNISTD_H +# include #endif + +#if HAVE_DIRENT_H +# include +# define NAMLEN(dirent) strlen((dirent)->d_name) #else -#include -#endif +# define dirent direct +# define NAMLEN(dirent) (dirent)->d_namlen +# if HAVE_SYS_NDIR_H +# include +# endif +# if HAVE_SYS_DIR_H +# include +# endif +# if HAVE_NDIR_H +# include +# endif #endif -#ifdef VOID_CLOSEDIR +#ifdef CLOSEDIR_VOID /* Fake a return value. */ -#define CLOSEDIR(d) (closedir (d), 0) +# define CLOSEDIR(d) (closedir (d), 0) #else -#define CLOSEDIR(d) closedir (d) +# define CLOSEDIR(d) closedir (d) #endif #ifdef STDC_HEADERS -#include -#include +# include +# include #else char *malloc (); char *realloc (); -#ifndef NULL -#define NULL 0 #endif +#ifndef NULL +# define NULL 0 #endif +#ifndef stpcpy char *stpcpy (); +#endif + +#include "savedir.h" /* Return a freshly allocated string containing the filenames in directory DIR, separated by '\0' characters; @@ -66,12 +76,10 @@ char *stpcpy (); Return NULL if DIR cannot be opened or if out of memory. */ char * -savedir (dir, name_size) - char *dir; - unsigned name_size; +savedir (const char *dir, off_t name_size) { DIR *dirp; - struct direct *dp; + struct dirent *dp; char *name_space; char *namep; @@ -79,6 +87,11 @@ savedir (dir, name_size) if (dirp == NULL) return NULL; + /* Be sure name_size is at least `1' so there's room for + the final NUL byte. */ + if (name_size <= 0) + name_size = 1; + name_space = (char *) malloc (name_size); if (name_space == NULL) { @@ -94,7 +107,7 @@ savedir (dir, name_size) || (dp->d_name[1] != '\0' && (dp->d_name[1] != '.' || dp->d_name[2] != '\0'))) { - unsigned size_needed = (namep - name_space) + NLENGTH (dp) + 2; + off_t size_needed = (namep - name_space) + NAMLEN (dp) + 2; if (size_needed > name_size) {