123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <sys/types.h>
- #include <errno.h>
- #ifndef errno
- extern int errno;
- #endif
- #if HAVE_DIRENT_H
- # include <dirent.h>
- #else
- # define dirent direct
- # if HAVE_SYS_NDIR_H
- # include <sys/ndir.h>
- # endif
- # if HAVE_SYS_DIR_H
- # include <sys/dir.h>
- # endif
- # if HAVE_NDIR_H
- # include <ndir.h>
- # endif
- #endif
- #ifdef CLOSEDIR_VOID
- # define CLOSEDIR(d) (closedir (d), 0)
- #else
- # define CLOSEDIR(d) closedir (d)
- #endif
- #ifdef STDC_HEADERS
- # include <stdlib.h>
- # include <string.h>
- #endif
- #ifndef NULL
- # define NULL 0
- #endif
- #include "savedir.h"
- #include "xalloc.h"
- #ifndef NAME_SIZE_DEFAULT
- # define NAME_SIZE_DEFAULT 512
- #endif
- char *
- savedir (const char *dir)
- {
- DIR *dirp;
- struct dirent *dp;
- char *name_space;
- size_t allocated = NAME_SIZE_DEFAULT;
- size_t used = 0;
- int save_errno;
- dirp = opendir (dir);
- if (dirp == NULL)
- return NULL;
- name_space = xmalloc (allocated);
- errno = 0;
- while ((dp = readdir (dirp)) != NULL)
- {
-
- char const *entry = dp->d_name;
- if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
- {
- size_t entry_size = strlen (entry) + 1;
- if (used + entry_size < used)
- xalloc_die ();
- if (allocated <= used + entry_size)
- {
- do
- {
- if (2 * allocated < allocated)
- xalloc_die ();
- allocated *= 2;
- }
- while (allocated <= used + entry_size);
- name_space = xrealloc (name_space, allocated);
- }
- memcpy (name_space + used, entry, entry_size);
- used += entry_size;
- }
- }
- name_space[used] = '\0';
- save_errno = errno;
- if (CLOSEDIR (dirp) != 0)
- save_errno = errno;
- if (save_errno != 0)
- {
- free (name_space);
- errno = save_errno;
- return NULL;
- }
- return name_space;
- }
|