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