xref: /titanic_44/usr/src/lib/libtecla/common/direader.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * All rights reserved.
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * Permission is hereby granted, free of charge, to any person obtaining a
7*7c478bd9Sstevel@tonic-gate  * copy of this software and associated documentation files (the
8*7c478bd9Sstevel@tonic-gate  * "Software"), to deal in the Software without restriction, including
9*7c478bd9Sstevel@tonic-gate  * without limitation the rights to use, copy, modify, merge, publish,
10*7c478bd9Sstevel@tonic-gate  * distribute, and/or sell copies of the Software, and to permit persons
11*7c478bd9Sstevel@tonic-gate  * to whom the Software is furnished to do so, provided that the above
12*7c478bd9Sstevel@tonic-gate  * copyright notice(s) and this permission notice appear in all copies of
13*7c478bd9Sstevel@tonic-gate  * the Software and that both the above copyright notice(s) and this
14*7c478bd9Sstevel@tonic-gate  * permission notice appear in supporting documentation.
15*7c478bd9Sstevel@tonic-gate  *
16*7c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*7c478bd9Sstevel@tonic-gate  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*7c478bd9Sstevel@tonic-gate  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19*7c478bd9Sstevel@tonic-gate  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20*7c478bd9Sstevel@tonic-gate  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21*7c478bd9Sstevel@tonic-gate  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22*7c478bd9Sstevel@tonic-gate  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23*7c478bd9Sstevel@tonic-gate  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24*7c478bd9Sstevel@tonic-gate  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  * Except as contained in this notice, the name of a copyright holder
27*7c478bd9Sstevel@tonic-gate  * shall not be used in advertising or otherwise to promote the sale, use
28*7c478bd9Sstevel@tonic-gate  * or other dealings in this Software without prior written authorization
29*7c478bd9Sstevel@tonic-gate  * of the copyright holder.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * If file-system access is to be excluded, this module has no function,
36*7c478bd9Sstevel@tonic-gate  * so all of its code should be excluded.
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate #ifndef WITHOUT_FILE_SYSTEM
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * Standard includes.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
45*7c478bd9Sstevel@tonic-gate #include <string.h>
46*7c478bd9Sstevel@tonic-gate #include <errno.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * Operating system includes.
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate #include <unistd.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
54*7c478bd9Sstevel@tonic-gate #include <dirent.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include "direader.h"
57*7c478bd9Sstevel@tonic-gate #include "errmsg.h"
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * Use the reentrant POSIX threads version of readdir()?
61*7c478bd9Sstevel@tonic-gate  */
62*7c478bd9Sstevel@tonic-gate #if defined(PREFER_REENTRANT) && defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199506L
63*7c478bd9Sstevel@tonic-gate #define USE_READDIR_R 1
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * Objects of the following type are used to maintain the resources
68*7c478bd9Sstevel@tonic-gate  * needed to read directories.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate struct DirReader {
71*7c478bd9Sstevel@tonic-gate   ErrMsg *err;             /* The error reporting buffer */
72*7c478bd9Sstevel@tonic-gate   DIR *dir;                /* The directory stream (if open, NULL otherwise) */
73*7c478bd9Sstevel@tonic-gate   struct dirent *file;     /* The latest directory entry */
74*7c478bd9Sstevel@tonic-gate #ifdef USE_READDIR_R
75*7c478bd9Sstevel@tonic-gate   struct dirent *buffer;   /* A buffer used by the threaded version of */
76*7c478bd9Sstevel@tonic-gate                            /*  readdir() */
77*7c478bd9Sstevel@tonic-gate   int buffer_dim;          /* The allocated size of buffer[] */
78*7c478bd9Sstevel@tonic-gate #endif
79*7c478bd9Sstevel@tonic-gate };
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static int _dr_path_is_dir(const char *pathname);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*.......................................................................
84*7c478bd9Sstevel@tonic-gate  * Create a new DirReader object.
85*7c478bd9Sstevel@tonic-gate  *
86*7c478bd9Sstevel@tonic-gate  * Output:
87*7c478bd9Sstevel@tonic-gate  *  return  DirReader *  The new object, or NULL on error.
88*7c478bd9Sstevel@tonic-gate  */
_new_DirReader(void)89*7c478bd9Sstevel@tonic-gate DirReader *_new_DirReader(void)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate   DirReader *dr;  /* The object to be returned */
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * Allocate the container.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate   dr = (DirReader *) malloc(sizeof(DirReader));
96*7c478bd9Sstevel@tonic-gate   if(!dr) {
97*7c478bd9Sstevel@tonic-gate     errno = ENOMEM;
98*7c478bd9Sstevel@tonic-gate     return NULL;
99*7c478bd9Sstevel@tonic-gate   };
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * Before attempting any operation that might fail, initialize the
102*7c478bd9Sstevel@tonic-gate  * container at least up to the point at which it can safely be passed
103*7c478bd9Sstevel@tonic-gate  * to _del_DirReader().
104*7c478bd9Sstevel@tonic-gate  */
105*7c478bd9Sstevel@tonic-gate   dr->err = NULL;
106*7c478bd9Sstevel@tonic-gate   dr->dir = NULL;
107*7c478bd9Sstevel@tonic-gate   dr->file = NULL;
108*7c478bd9Sstevel@tonic-gate #ifdef USE_READDIR_R
109*7c478bd9Sstevel@tonic-gate   dr->buffer = NULL;
110*7c478bd9Sstevel@tonic-gate   dr->buffer_dim = 0;
111*7c478bd9Sstevel@tonic-gate #endif
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * Allocate a place to record error messages.
114*7c478bd9Sstevel@tonic-gate  */
115*7c478bd9Sstevel@tonic-gate   dr->err = _new_ErrMsg();
116*7c478bd9Sstevel@tonic-gate   if(!dr->err)
117*7c478bd9Sstevel@tonic-gate     return _del_DirReader(dr);
118*7c478bd9Sstevel@tonic-gate   return dr;
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*.......................................................................
122*7c478bd9Sstevel@tonic-gate  * Delete a DirReader object.
123*7c478bd9Sstevel@tonic-gate  *
124*7c478bd9Sstevel@tonic-gate  * Input:
125*7c478bd9Sstevel@tonic-gate  *  dr     DirReader *  The object to be deleted.
126*7c478bd9Sstevel@tonic-gate  * Output:
127*7c478bd9Sstevel@tonic-gate  *  return DirReader *  The deleted object (always NULL).
128*7c478bd9Sstevel@tonic-gate  */
_del_DirReader(DirReader * dr)129*7c478bd9Sstevel@tonic-gate DirReader *_del_DirReader(DirReader *dr)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate   if(dr) {
132*7c478bd9Sstevel@tonic-gate     _dr_close_dir(dr);
133*7c478bd9Sstevel@tonic-gate #ifdef USE_READDIR_R
134*7c478bd9Sstevel@tonic-gate     free(dr->buffer);
135*7c478bd9Sstevel@tonic-gate #endif
136*7c478bd9Sstevel@tonic-gate     dr->err = _del_ErrMsg(dr->err);
137*7c478bd9Sstevel@tonic-gate     free(dr);
138*7c478bd9Sstevel@tonic-gate   };
139*7c478bd9Sstevel@tonic-gate   return NULL;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*.......................................................................
143*7c478bd9Sstevel@tonic-gate  * Open a new directory.
144*7c478bd9Sstevel@tonic-gate  *
145*7c478bd9Sstevel@tonic-gate  * Input:
146*7c478bd9Sstevel@tonic-gate  *  dr      DirReader *   The directory reader resource object.
147*7c478bd9Sstevel@tonic-gate  *  path   const char *   The directory to be opened.
148*7c478bd9Sstevel@tonic-gate  * Input/Output:
149*7c478bd9Sstevel@tonic-gate  *  errmsg       char **  If an error occurs and errmsg isn't NULL, a
150*7c478bd9Sstevel@tonic-gate  *                        pointer to an error description will be assigned
151*7c478bd9Sstevel@tonic-gate  *                        to *errmsg.
152*7c478bd9Sstevel@tonic-gate  * Output:
153*7c478bd9Sstevel@tonic-gate  *  return        int     0 - OK.
154*7c478bd9Sstevel@tonic-gate  *                        1 - Error (see *errmsg for a description).
155*7c478bd9Sstevel@tonic-gate  */
_dr_open_dir(DirReader * dr,const char * path,char ** errmsg)156*7c478bd9Sstevel@tonic-gate int _dr_open_dir(DirReader *dr, const char *path, char **errmsg)
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate   DIR *dir = NULL;   /* The directory stream */
159*7c478bd9Sstevel@tonic-gate /*
160*7c478bd9Sstevel@tonic-gate  * If a directory is already open, close it first.
161*7c478bd9Sstevel@tonic-gate  */
162*7c478bd9Sstevel@tonic-gate   (void) _dr_close_dir(dr);
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate  * Is the path a directory?
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate   if(!_dr_path_is_dir(path)) {
167*7c478bd9Sstevel@tonic-gate     if(errmsg) {
168*7c478bd9Sstevel@tonic-gate       _err_record_msg(dr->err, "Can't open directory: ", path, END_ERR_MSG);
169*7c478bd9Sstevel@tonic-gate       *errmsg = _err_get_msg(dr->err);
170*7c478bd9Sstevel@tonic-gate     };
171*7c478bd9Sstevel@tonic-gate     return 1;
172*7c478bd9Sstevel@tonic-gate   };
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * Attempt to open the directory.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate   dir = opendir(path);
177*7c478bd9Sstevel@tonic-gate   if(!dir) {
178*7c478bd9Sstevel@tonic-gate     if(errmsg) {
179*7c478bd9Sstevel@tonic-gate       _err_record_msg(dr->err, "Can't open directory: ", path, END_ERR_MSG);
180*7c478bd9Sstevel@tonic-gate       *errmsg = _err_get_msg(dr->err);
181*7c478bd9Sstevel@tonic-gate     };
182*7c478bd9Sstevel@tonic-gate     return 1;
183*7c478bd9Sstevel@tonic-gate   };
184*7c478bd9Sstevel@tonic-gate /*
185*7c478bd9Sstevel@tonic-gate  * If using POSIX threads, allocate a buffer for readdir_r().
186*7c478bd9Sstevel@tonic-gate  */
187*7c478bd9Sstevel@tonic-gate #ifdef USE_READDIR_R
188*7c478bd9Sstevel@tonic-gate   {
189*7c478bd9Sstevel@tonic-gate     size_t size;
190*7c478bd9Sstevel@tonic-gate     int name_max = pathconf(path, _PC_NAME_MAX);
191*7c478bd9Sstevel@tonic-gate #ifdef NAME_MAX
192*7c478bd9Sstevel@tonic-gate     if(name_max < 0)
193*7c478bd9Sstevel@tonic-gate       name_max = NAME_MAX;
194*7c478bd9Sstevel@tonic-gate #endif
195*7c478bd9Sstevel@tonic-gate     if(name_max < 0) {
196*7c478bd9Sstevel@tonic-gate       if(errmsg) {
197*7c478bd9Sstevel@tonic-gate 	_err_record_msg(dr->err, "Unable to deduce readdir() buffer size.",
198*7c478bd9Sstevel@tonic-gate 			END_ERR_MSG);
199*7c478bd9Sstevel@tonic-gate 	*errmsg = _err_get_msg(dr->err);
200*7c478bd9Sstevel@tonic-gate       };
201*7c478bd9Sstevel@tonic-gate       closedir(dir);
202*7c478bd9Sstevel@tonic-gate       return 1;
203*7c478bd9Sstevel@tonic-gate     };
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * How big a buffer do we need to allocate?
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate     size = sizeof(struct dirent) + name_max;
208*7c478bd9Sstevel@tonic-gate /*
209*7c478bd9Sstevel@tonic-gate  * Extend the buffer?
210*7c478bd9Sstevel@tonic-gate  */
211*7c478bd9Sstevel@tonic-gate     if(size > dr->buffer_dim || !dr->buffer) {
212*7c478bd9Sstevel@tonic-gate       struct dirent *buffer = (struct dirent *) (dr->buffer ?
213*7c478bd9Sstevel@tonic-gate 						 realloc(dr->buffer, size) :
214*7c478bd9Sstevel@tonic-gate 						 malloc(size));
215*7c478bd9Sstevel@tonic-gate       if(!buffer) {
216*7c478bd9Sstevel@tonic-gate 	if(errmsg) {
217*7c478bd9Sstevel@tonic-gate 	  _err_record_msg(dr->err, "Insufficient memory for readdir() buffer.",
218*7c478bd9Sstevel@tonic-gate 			  END_ERR_MSG);
219*7c478bd9Sstevel@tonic-gate 	  *errmsg = _err_get_msg(dr->err);
220*7c478bd9Sstevel@tonic-gate 	};
221*7c478bd9Sstevel@tonic-gate 	closedir(dir);
222*7c478bd9Sstevel@tonic-gate 	errno = ENOMEM;
223*7c478bd9Sstevel@tonic-gate 	return 1;
224*7c478bd9Sstevel@tonic-gate       };
225*7c478bd9Sstevel@tonic-gate       dr->buffer = buffer;
226*7c478bd9Sstevel@tonic-gate       dr->buffer_dim = size;
227*7c478bd9Sstevel@tonic-gate     };
228*7c478bd9Sstevel@tonic-gate   };
229*7c478bd9Sstevel@tonic-gate #endif
230*7c478bd9Sstevel@tonic-gate /*
231*7c478bd9Sstevel@tonic-gate  * Record the successfully opened directory.
232*7c478bd9Sstevel@tonic-gate  */
233*7c478bd9Sstevel@tonic-gate   dr->dir = dir;
234*7c478bd9Sstevel@tonic-gate   return 0;
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate /*.......................................................................
238*7c478bd9Sstevel@tonic-gate  * If the DirReader object is currently contains an open directory,
239*7c478bd9Sstevel@tonic-gate  * close it.
240*7c478bd9Sstevel@tonic-gate  *
241*7c478bd9Sstevel@tonic-gate  * Input:
242*7c478bd9Sstevel@tonic-gate  *  dr    DirReader *   The directory reader resource object.
243*7c478bd9Sstevel@tonic-gate  */
_dr_close_dir(DirReader * dr)244*7c478bd9Sstevel@tonic-gate void _dr_close_dir(DirReader *dr)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate   if(dr && dr->dir) {
247*7c478bd9Sstevel@tonic-gate     closedir(dr->dir);
248*7c478bd9Sstevel@tonic-gate     dr->dir = NULL;
249*7c478bd9Sstevel@tonic-gate     dr->file = NULL;
250*7c478bd9Sstevel@tonic-gate     _err_clear_msg(dr->err);
251*7c478bd9Sstevel@tonic-gate   };
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate /*.......................................................................
255*7c478bd9Sstevel@tonic-gate  * Read the next file from the directory opened with _dr_open_dir().
256*7c478bd9Sstevel@tonic-gate  *
257*7c478bd9Sstevel@tonic-gate  * Input:
258*7c478bd9Sstevel@tonic-gate  *  dr    DirReader *  The directory reader resource object.
259*7c478bd9Sstevel@tonic-gate  * Output:
260*7c478bd9Sstevel@tonic-gate  *  return     char *  The name of the new file, or NULL if we reached
261*7c478bd9Sstevel@tonic-gate  *                     the end of the directory.
262*7c478bd9Sstevel@tonic-gate  */
_dr_next_file(DirReader * dr)263*7c478bd9Sstevel@tonic-gate char *_dr_next_file(DirReader *dr)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * Are we currently reading a directory?
267*7c478bd9Sstevel@tonic-gate  */
268*7c478bd9Sstevel@tonic-gate   if(dr->dir) {
269*7c478bd9Sstevel@tonic-gate /*
270*7c478bd9Sstevel@tonic-gate  * Read the next directory entry.
271*7c478bd9Sstevel@tonic-gate  */
272*7c478bd9Sstevel@tonic-gate #ifdef USE_READDIR_R
273*7c478bd9Sstevel@tonic-gate     if(readdir_r(dr->dir, dr->buffer, &dr->file) == 0 && dr->file)
274*7c478bd9Sstevel@tonic-gate       return dr->file->d_name;
275*7c478bd9Sstevel@tonic-gate #else
276*7c478bd9Sstevel@tonic-gate     dr->file = readdir(dr->dir);
277*7c478bd9Sstevel@tonic-gate     if(dr->file)
278*7c478bd9Sstevel@tonic-gate       return dr->file->d_name;
279*7c478bd9Sstevel@tonic-gate #endif
280*7c478bd9Sstevel@tonic-gate   };
281*7c478bd9Sstevel@tonic-gate /*
282*7c478bd9Sstevel@tonic-gate  * When the end of a directory is reached, close it.
283*7c478bd9Sstevel@tonic-gate  */
284*7c478bd9Sstevel@tonic-gate   _dr_close_dir(dr);
285*7c478bd9Sstevel@tonic-gate   return NULL;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /*.......................................................................
289*7c478bd9Sstevel@tonic-gate  * Return 1 if the specified pathname refers to a directory.
290*7c478bd9Sstevel@tonic-gate  *
291*7c478bd9Sstevel@tonic-gate  * Input:
292*7c478bd9Sstevel@tonic-gate  *  pathname  const char *  The path to test.
293*7c478bd9Sstevel@tonic-gate  * Output:
294*7c478bd9Sstevel@tonic-gate  *  return           int    0 - Not a directory.
295*7c478bd9Sstevel@tonic-gate  *                          1 - pathname[] refers to a directory.
296*7c478bd9Sstevel@tonic-gate  */
_dr_path_is_dir(const char * pathname)297*7c478bd9Sstevel@tonic-gate static int _dr_path_is_dir(const char *pathname)
298*7c478bd9Sstevel@tonic-gate {
299*7c478bd9Sstevel@tonic-gate   struct stat statbuf;    /* The file-statistics return buffer */
300*7c478bd9Sstevel@tonic-gate /*
301*7c478bd9Sstevel@tonic-gate  * Look up the file attributes.
302*7c478bd9Sstevel@tonic-gate  */
303*7c478bd9Sstevel@tonic-gate   if(stat(pathname, &statbuf) < 0)
304*7c478bd9Sstevel@tonic-gate     return 0;
305*7c478bd9Sstevel@tonic-gate /*
306*7c478bd9Sstevel@tonic-gate  * Is the file a directory?
307*7c478bd9Sstevel@tonic-gate  */
308*7c478bd9Sstevel@tonic-gate   return S_ISDIR(statbuf.st_mode) != 0;
309*7c478bd9Sstevel@tonic-gate }
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate #endif  /* ifndef WITHOUT_FILE_SYSTEM */
312