* find/defs.h: move lstat declarations into defs.h
[findutils.git] / lib / savedir.c
blobb11211613805237335fa8c99e60c9650f055355b
1 /* savedir.c -- save the list of files in a directory in a string
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
24 #include <sys/types.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
30 #if HAVE_DIRENT_H
31 # include <dirent.h>
32 # define NAMLEN(dirent) strlen((dirent)->d_name)
33 #else
34 # define dirent direct
35 # define NAMLEN(dirent) (dirent)->d_namlen
36 # if HAVE_SYS_NDIR_H
37 # include <sys/ndir.h>
38 # endif
39 # if HAVE_SYS_DIR_H
40 # include <sys/dir.h>
41 # endif
42 # if HAVE_NDIR_H
43 # include <ndir.h>
44 # endif
45 #endif
47 #ifdef CLOSEDIR_VOID
48 /* Fake a return value. */
49 #define CLOSEDIR(d) (closedir (d), 0)
50 #else
51 #define CLOSEDIR(d) closedir (d)
52 #endif
54 #ifdef STDC_HEADERS
55 #include <stdlib.h>
56 #include <string.h>
57 #endif
58 #ifndef NULL
59 #define NULL 0
60 #endif
62 char *stpcpy ();
64 /* Return a freshly allocated string containing the filenames
65 in directory DIR, separated by '\0' characters;
66 the end is marked by two '\0' characters in a row.
67 NAME_SIZE is the number of bytes to initially allocate
68 for the string; it will be enlarged as needed.
69 Return NULL if DIR cannot be opened or if out of memory. */
71 char *
72 savedir (dir, name_size)
73 char *dir;
74 unsigned name_size;
76 DIR *dirp;
77 struct dirent *dp;
78 char *name_space;
79 char *namep;
81 dirp = opendir (dir);
82 if (dirp == NULL)
83 return NULL;
85 name_space = (char *) malloc (name_size);
86 if (name_space == NULL)
88 closedir (dirp);
89 return NULL;
91 namep = name_space;
93 while ((dp = readdir (dirp)) != NULL)
95 /* Skip "." and ".." (some NFS filesystems' directories lack them). */
96 if (dp->d_name[0] != '.'
97 || (dp->d_name[1] != '\0'
98 && (dp->d_name[1] != '.' || dp->d_name[2] != '\0')))
100 unsigned size_needed = (namep - name_space) + NAMLEN (dp) + 2;
102 if (size_needed > name_size)
104 char *new_name_space;
106 while (size_needed > name_size)
107 name_size += 1024;
109 new_name_space = realloc (name_space, name_size);
110 if (new_name_space == NULL)
112 closedir (dirp);
113 return NULL;
115 namep += new_name_space - name_space;
116 name_space = new_name_space;
118 namep = stpcpy (namep, dp->d_name) + 1;
121 *namep = '\0';
122 if (CLOSEDIR (dirp))
124 free (name_space);
125 return NULL;
127 return name_space;