]> Dogcows Code - chaz/tar/blob - lib/savedir.c
(decode_header): Initialize st_atime and st_ctime.
[chaz/tar] / lib / savedir.c
1 /* savedir.c -- save the list of files in a directory in a string
2
3 Copyright 1990, 1997, 1998, 1999, 2000, 2001 Free Software
4 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <sys/types.h>
27
28 #include <errno.h>
29 #ifndef errno
30 extern int errno;
31 #endif
32
33 #if HAVE_DIRENT_H
34 # include <dirent.h>
35 #else
36 # define dirent direct
37 # if HAVE_SYS_NDIR_H
38 # include <sys/ndir.h>
39 # endif
40 # if HAVE_SYS_DIR_H
41 # include <sys/dir.h>
42 # endif
43 # if HAVE_NDIR_H
44 # include <ndir.h>
45 # endif
46 #endif
47
48 #ifdef CLOSEDIR_VOID
49 /* Fake a return value. */
50 # define CLOSEDIR(d) (closedir (d), 0)
51 #else
52 # define CLOSEDIR(d) closedir (d)
53 #endif
54
55 #ifdef STDC_HEADERS
56 # include <stdlib.h>
57 # include <string.h>
58 #endif
59 #ifndef NULL
60 # define NULL 0
61 #endif
62
63 #include "savedir.h"
64 #include "xalloc.h"
65
66 /* Return a freshly allocated string containing the filenames
67 in directory DIR, separated by '\0' characters;
68 the end is marked by two '\0' characters in a row.
69 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
70
71 #ifndef NAME_SIZE_DEFAULT
72 # define NAME_SIZE_DEFAULT 512
73 #endif
74
75 char *
76 savedir (const char *dir)
77 {
78 DIR *dirp;
79 struct dirent *dp;
80 char *name_space;
81 size_t allocated = NAME_SIZE_DEFAULT;
82 size_t used = 0;
83 int save_errno;
84
85 dirp = opendir (dir);
86 if (dirp == NULL)
87 return NULL;
88
89 name_space = xmalloc (allocated);
90
91 errno = 0;
92 while ((dp = readdir (dirp)) != NULL)
93 {
94 /* Skip "", ".", and "..". "" is returned by at least one buggy
95 implementation: Solaris 2.4 readdir on NFS filesystems. */
96 char const *entry = dp->d_name;
97 if (entry[entry[0] != '.' ? 0 : entry[1] != '.' ? 1 : 2] != '\0')
98 {
99 size_t entry_size = strlen (entry) + 1;
100 if (used + entry_size < used)
101 xalloc_die ();
102 if (allocated <= used + entry_size)
103 {
104 do
105 {
106 if (2 * allocated < allocated)
107 xalloc_die ();
108 allocated *= 2;
109 }
110 while (allocated <= used + entry_size);
111
112 name_space = xrealloc (name_space, allocated);
113 }
114 memcpy (name_space + used, entry, entry_size);
115 used += entry_size;
116 }
117 }
118 name_space[used] = '\0';
119 save_errno = errno;
120 if (CLOSEDIR (dirp) != 0)
121 save_errno = errno;
122 if (save_errno != 0)
123 {
124 free (name_space);
125 errno = save_errno;
126 return NULL;
127 }
128 return name_space;
129 }
This page took 0.042566 seconds and 4 git commands to generate.