X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsavedir.c;h=c92e62eadccdf3e6e4f5a62b414efdf1c0e8c300;hb=7cf6336f44640e772242831f5d2ef0247215886b;hp=5b7a6dd7f6787665d08e8f1c94dd19c14443578d;hpb=49c86fdb3d58fc15ae14f804e0f69b681ff4fb70;p=gnulib.git diff --git a/lib/savedir.c b/lib/savedir.c index 5b7a6dd7f..c92e62ead 100644 --- a/lib/savedir.c +++ b/lib/savedir.c @@ -1,5 +1,7 @@ /* savedir.c -- save the list of files in a directory in a string - Copyright (C) 1990, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. + + Copyright 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004 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 @@ -21,12 +23,11 @@ # include #endif +#include "savedir.h" + #include #include -#ifndef errno -extern int errno; -#endif #if HAVE_DIRENT_H # include @@ -50,23 +51,15 @@ extern int errno; # define CLOSEDIR(d) closedir (d) #endif -#ifdef STDC_HEADERS -# include -# include -#endif -#ifndef NULL -# define NULL 0 -#endif +#include +#include +#include -#include "savedir.h" #include "xalloc.h" /* Return a freshly allocated string containing the filenames in directory DIR, separated by '\0' characters; the end is marked by two '\0' characters in a row. - NAME_SIZE is the number of bytes to initially allocate - for the string; it will be enlarged as needed. - Use NAME_SIZE == -1 if you do not know the size. Return NULL (setting errno) if DIR cannot be opened, read, or closed. */ #ifndef NAME_SIZE_DEFAULT @@ -74,12 +67,12 @@ extern int errno; #endif char * -savedir (const char *dir, off_t name_size) +savedir (const char *dir) { DIR *dirp; struct dirent *dp; char *name_space; - size_t allocated = name_size; /* Overflow is checked indirectly below. */ + size_t allocated = NAME_SIZE_DEFAULT; size_t used = 0; int save_errno; @@ -87,20 +80,13 @@ savedir (const char *dir, off_t name_size) if (dirp == NULL) return NULL; - /* Use the default if the size is not known. Be sure "allocated" - is at least `1' so there's room for the final NUL byte. - Do not simply test name_size <= 0, because the initialization - of "allocated" might have overflowed. */ - if (name_size < 0 || allocated == 0) - allocated = NAME_SIZE_DEFAULT; - name_space = xmalloc (allocated); errno = 0; while ((dp = readdir (dirp)) != NULL) { /* Skip "", ".", and "..". "" is returned by at least one buggy - implementation: Solaris 2.4 readdir on NFS filesystems. */ + implementation: Solaris 2.4 readdir on NFS file systems. */ char const *entry = dp->d_name; if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0') {