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