1/* savedir.c -- save the list of files in a directory in a string
2
3   Copyright (C) 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
4   2006 Free Software Foundation, Inc.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22#include <config.h>
23
24#include "savedir.h"
25
26#include <sys/types.h>
27
28#include <errno.h>
29
30#include <dirent.h>
31#ifndef _D_EXACT_NAMLEN
32# define _D_EXACT_NAMLEN(dp)	strlen ((dp)->d_name)
33#endif
34
35#include <stddef.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include "openat.h"
40#include "xalloc.h"
41
42#ifndef NAME_SIZE_DEFAULT
43# define NAME_SIZE_DEFAULT 512
44#endif
45
46/* The results of opendir() in this file are not used with dirfd and fchdir,
47   therefore save some unnecessary work in fchdir.c.  */
48#undef opendir
49#undef closedir
50
51/* Return a freshly allocated string containing the file names
52   in directory DIRP, separated by '\0' characters;
53   the end is marked by two '\0' characters in a row.
54   Return NULL (setting errno) if DIRP cannot be read or closed.
55   If DIRP is NULL, return NULL without affecting errno.  */
56
57static char *
58savedirstream (DIR *dirp)
59{
60  char *name_space;
61  size_t allocated = NAME_SIZE_DEFAULT;
62  size_t used = 0;
63  int save_errno;
64
65  if (dirp == NULL)
66    return NULL;
67
68  name_space = xmalloc (allocated);
69
70  for (;;)
71    {
72      struct dirent const *dp;
73      char const *entry;
74
75      errno = 0;
76      dp = readdir (dirp);
77      if (! dp)
78	break;
79
80      /* Skip "", ".", and "..".  "" is returned by at least one buggy
81         implementation: Solaris 2.4 readdir on NFS file systems.  */
82      entry = dp->d_name;
83      if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
84	{
85	  size_t entry_size = _D_EXACT_NAMLEN (dp) + 1;
86	  if (used + entry_size < used)
87	    xalloc_die ();
88	  if (allocated <= used + entry_size)
89	    {
90	      do
91		{
92		  if (2 * allocated < allocated)
93		    xalloc_die ();
94		  allocated *= 2;
95		}
96	      while (allocated <= used + entry_size);
97
98	      name_space = xrealloc (name_space, allocated);
99	    }
100	  memcpy (name_space + used, entry, entry_size);
101	  used += entry_size;
102	}
103    }
104  name_space[used] = '\0';
105  save_errno = errno;
106  if (closedir (dirp) != 0)
107    save_errno = errno;
108  if (save_errno != 0)
109    {
110      free (name_space);
111      errno = save_errno;
112      return NULL;
113    }
114  return name_space;
115}
116
117/* Return a freshly allocated string containing the file names
118   in directory DIR, separated by '\0' characters;
119   the end is marked by two '\0' characters in a row.
120   Return NULL (setting errno) if DIR cannot be opened, read, or closed.  */
121
122char *
123savedir (char const *dir)
124{
125  return savedirstream (opendir (dir));
126}
127
128/* Return a freshly allocated string containing the file names
129   in directory FD, separated by '\0' characters;
130   the end is marked by two '\0' characters in a row.
131   Return NULL (setting errno) if FD cannot be read or closed.  */
132
133char *
134fdsavedir (int fd)
135{
136  return savedirstream (fdopendir (fd));
137}
138