1 /* savedir.c -- save the list of files in a directory in a string
3 Copyright 1990, 1997, 1998, 1999, 2000, 2001, 2003, 2004 Free
4 Software Foundation, Inc.
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)
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.
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. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
27 # include <sys/stat.h>
31 # include <sys/types.h>
43 # define dirent direct
45 # include <sys/ndir.h>
56 /* Fake a return value. */
57 # define CLOSEDIR(d) (closedir (d), 0)
59 # define CLOSEDIR(d) closedir (d)
67 #include "extendbuf.h"
68 #include "savedirinfo.h"
70 #undef HAVE_STRUCT_DIRENT_D_TYPE
73 #if defined HAVE_STRUCT_DIRENT_D_TYPE
75 type_to_mode(unsigned type
)
82 case DT_FIFO
: return S_IFIFO
;
85 case DT_CHR
: return S_IFCHR
;
88 case DT_DIR
: return S_IFDIR
;
91 case DT_BLK
: return S_IFBLK
;
94 case DT_REG
: return S_IFREG
;
97 case DT_LNK
: return S_IFLNK
;
100 case DT_SOCK
: return S_IFSOCK
;
103 return 0; /* Unknown. */
110 /* Return a freshly allocated string containing the filenames
111 in directory DIR, separated by '\0' characters;
112 the end is marked by two '\0' characters in a row.
113 Return NULL (setting errno) if DIR cannot be opened, read, or closed. */
116 savedirinfo (const char *dir
, struct savedir_dirinfo
**extra
)
121 size_t namebuf_allocated
= 0u, namebuf_used
= 0u;
122 #if defined HAVE_STRUCT_DIRENT_D_TYPE
123 size_t extra_allocated
= 0u, extra_used
= 0u;
124 struct savedir_dirinfo
*info
= NULL
;
131 dirp
= opendir (dir
);
137 while ((dp
= readdir (dirp
)) != NULL
)
139 /* Skip "", ".", and "..". "" is returned by at least one buggy
140 implementation: Solaris 2.4 readdir on NFS file systems. */
141 char const *entry
= dp
->d_name
;
142 if (entry
[entry
[0] != '.' ? 0 : entry
[1] != '.' ? 1 : 2] != '\0')
144 /* Remember the name. */
145 size_t entry_size
= strlen (entry
) + 1;
146 name_space
= extendbuf(name_space
, namebuf_used
+entry_size
, &namebuf_allocated
);
147 memcpy (name_space
+ namebuf_used
, entry
, entry_size
);
148 namebuf_used
+= entry_size
;
151 #if defined HAVE_STRUCT_DIRENT_D_TYPE
152 /* Remember the type. */
155 info
= extendbuf(info
,
156 (extra_used
+1) * sizeof(struct savedir_dirinfo
),
158 info
[extra_used
].type_info
= type_to_mode(dp
->d_type
);
164 name_space
= extendbuf(name_space
, namebuf_used
+1, &namebuf_allocated
);
165 name_space
[namebuf_used
] = '\0';
168 if (CLOSEDIR (dirp
) != 0)
177 #if defined HAVE_STRUCT_DIRENT_D_TYPE