xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/ftw.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
28*0Sstevel@tonic-gate /*        All Rights Reserved   */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 1.2 */
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate /***************************************************************
34*0Sstevel@tonic-gate  *	ftw - file tree walk
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *	int ftw (path, fn, depth)  char *path; int (*fn)(); int depth;
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  *	Given a path name, ftw starts from the file given by that path
39*0Sstevel@tonic-gate  *	name and visits each file and directory in the tree beneath
40*0Sstevel@tonic-gate  *	that file.  If a single file has multiple links within the
41*0Sstevel@tonic-gate  *	structure, it will be visited once for each such link.
42*0Sstevel@tonic-gate  *	For each object visited, fn is called with three arguments.
43*0Sstevel@tonic-gate  *	The first contains the path name of the object, the second
44*0Sstevel@tonic-gate  *	contains a pointer to a stat buffer which will usually hold
45*0Sstevel@tonic-gate  *	appropriate information for the object and the third will
46*0Sstevel@tonic-gate  *	contain an integer value giving additional information about
47*0Sstevel@tonic-gate  *
48*0Sstevel@tonic-gate  *		FTW_F	The object is a file for which stat was
49*0Sstevel@tonic-gate  *			successful.  It does not guarantee that the
50*0Sstevel@tonic-gate  *			file can actually be read.
51*0Sstevel@tonic-gate  *
52*0Sstevel@tonic-gate  *		FTW_D	The object is a directory for which stat and
53*0Sstevel@tonic-gate  *			open for read were both successful.
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *		FTW_DNR	The object is a directory for which stat
56*0Sstevel@tonic-gate  *			succeeded, but which cannot be read.  Because
57*0Sstevel@tonic-gate  *			the directory cannot be read, fn will not be
58*0Sstevel@tonic-gate  *			called for any descendants of this directory.
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  *		FTW_NS	Stat failed on the object because of lack of
61*0Sstevel@tonic-gate  *			appropriate permission, or because the object is a
62*0Sstevel@tonic-gate  *			symbolic link that points to a non-existent file.
63*0Sstevel@tonic-gate  *			This indication will be given, for example, for each
64*0Sstevel@tonic-gate  *			file in a directory with read but no execute
65*0Sstevel@tonic-gate  *			permission.   Because stat failed, it is not
66*0Sstevel@tonic-gate  *			possible to determine whether this object is a file
67*0Sstevel@tonic-gate  *			or a directory.  The stat buffer passed to fn will
68*0Sstevel@tonic-gate  *			contain garbage.  Stat failure for any reason
69*0Sstevel@tonic-gate  *			other than lack of permission will be
70*0Sstevel@tonic-gate  *			considered an error and will cause ftw to stop
71*0Sstevel@tonic-gate  *			and return -1 to its caller.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  *	If fn returns nonzero, ftw stops and returns the same value
74*0Sstevel@tonic-gate  *	to its caller.  If ftw gets into other trouble along the way,
75*0Sstevel@tonic-gate  *	it returns -1 and leaves an indication of the cause in errno.
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  *	The third argument to ftw does not limit the depth to which
78*0Sstevel@tonic-gate  *	ftw will go.  Rather, it limits the depth to which ftw will
79*0Sstevel@tonic-gate  *	go before it starts recycling file descriptors.  In general,
80*0Sstevel@tonic-gate  *	it is necessary to use a file descriptor for each level of the
81*0Sstevel@tonic-gate  *	tree, but they can be recycled for deep trees by saving the
82*0Sstevel@tonic-gate  *	position, closing, re-opening, and seeking.  It is possible
83*0Sstevel@tonic-gate  *	to start recycling file descriptors by sensing when we have
84*0Sstevel@tonic-gate  *	run out, but in general this will not be terribly useful if
85*0Sstevel@tonic-gate  *	fn expects to be able to open files.  We could also figure out
86*0Sstevel@tonic-gate  *	how many file descriptors are available and guarantee a certain
87*0Sstevel@tonic-gate  *	number to fn, but we would not know how many to guarantee,
88*0Sstevel@tonic-gate  *	and we do not want to impose the extra overhead on a caller who
89*0Sstevel@tonic-gate  *	knows how many are available without having to figure it out.
90*0Sstevel@tonic-gate  *
91*0Sstevel@tonic-gate  *	It is possible for ftw to die with a memory fault in the event
92*0Sstevel@tonic-gate  *	of a file system so deeply nested that the stack overflows.
93*0Sstevel@tonic-gate  **************************************************************/
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #include <sys/types.h>
96*0Sstevel@tonic-gate #include <sys/stat.h>
97*0Sstevel@tonic-gate #include <sys/dir.h>
98*0Sstevel@tonic-gate #include <errno.h>
99*0Sstevel@tonic-gate #include <ftw.h>
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate #define NULL 0
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate extern char *malloc(), *strcpy();
104*0Sstevel@tonic-gate extern void free();
105*0Sstevel@tonic-gate extern int errno;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate int
ftw(path,fn,depth)108*0Sstevel@tonic-gate ftw(path, fn, depth)
109*0Sstevel@tonic-gate char *path;
110*0Sstevel@tonic-gate int (*fn)();
111*0Sstevel@tonic-gate int depth;
112*0Sstevel@tonic-gate {
113*0Sstevel@tonic-gate 	int rc, n;
114*0Sstevel@tonic-gate 	DIR *dirp;
115*0Sstevel@tonic-gate 	char *subpath, *component;
116*0Sstevel@tonic-gate 	struct stat sb;
117*0Sstevel@tonic-gate 	struct direct *dp;
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	/* Try to get file status.
120*0Sstevel@tonic-gate 				If unsuccessful, errno will say why. */
121*0Sstevel@tonic-gate 	if(stat(path, &sb) < 0) {
122*0Sstevel@tonic-gate 		if (errno == EACCES) {
123*0Sstevel@tonic-gate 			return((*fn)(path, &sb, FTW_NS));
124*0Sstevel@tonic-gate 		} else if (errno == ENOENT) {
125*0Sstevel@tonic-gate 			/* Check if symbolic link points to non-existent file */
126*0Sstevel@tonic-gate 			if (lstat(path, &sb) < 0) {
127*0Sstevel@tonic-gate 				return(-1);
128*0Sstevel@tonic-gate 			}
129*0Sstevel@tonic-gate 			else if ((sb.st_mode & S_IFMT) == S_IFLNK) {
130*0Sstevel@tonic-gate 				errno = ENOENT;
131*0Sstevel@tonic-gate 				return((*fn)(path, &sb, FTW_NS));
132*0Sstevel@tonic-gate 			}
133*0Sstevel@tonic-gate 			else {
134*0Sstevel@tonic-gate 				return(-1);
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 		} else {
137*0Sstevel@tonic-gate 			return(-1);
138*0Sstevel@tonic-gate 		}
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	/*
142*0Sstevel@tonic-gate 	 *	The stat succeeded, so we know the object exists.
143*0Sstevel@tonic-gate 	 *	If not a directory, call the user function and return.
144*0Sstevel@tonic-gate 	 */
145*0Sstevel@tonic-gate 	if((sb.st_mode & S_IFMT) != S_IFDIR)
146*0Sstevel@tonic-gate 		return((*fn)(path, &sb, FTW_F));
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 *	The object was a directory.
150*0Sstevel@tonic-gate 	 *
151*0Sstevel@tonic-gate 	 *	Open a file to read the directory
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	dirp = opendir(path);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 *	Call the user function, telling it whether
157*0Sstevel@tonic-gate 	 *	the directory can be read.  If it can't be read
158*0Sstevel@tonic-gate 	 *	call the user function or indicate an error,
159*0Sstevel@tonic-gate 	 *	depending on the reason it couldn't be read.
160*0Sstevel@tonic-gate 	 */
161*0Sstevel@tonic-gate 	if(dirp == NULL)
162*0Sstevel@tonic-gate 		return(errno == EACCES? (*fn)(path, &sb, FTW_DNR): -1);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	/* We could read the directory.  Call user function. */
165*0Sstevel@tonic-gate 	rc = (*fn)(path, &sb, FTW_D);
166*0Sstevel@tonic-gate 	if(rc != 0)
167*0Sstevel@tonic-gate 		return(rc);
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 	/* Allocate a buffer to hold generated pathnames. */
170*0Sstevel@tonic-gate 	n = strlen(path);
171*0Sstevel@tonic-gate 	subpath = malloc((unsigned)(n+MAXNAMLEN+2));
172*0Sstevel@tonic-gate 	if(subpath == NULL) {
173*0Sstevel@tonic-gate 		closedir(dirp);
174*0Sstevel@tonic-gate 		errno = ENOMEM;
175*0Sstevel@tonic-gate 		return(-1);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	/* Create a prefix to which we will append component names */
179*0Sstevel@tonic-gate 	(void)strcpy(subpath, path);
180*0Sstevel@tonic-gate 	if(subpath[0] != '\0' && subpath[n-1] != '/')
181*0Sstevel@tonic-gate 		subpath[n++] = '/';
182*0Sstevel@tonic-gate 	component = &subpath[n];
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/*
185*0Sstevel@tonic-gate 	 *	Read the directory one component at a time.
186*0Sstevel@tonic-gate 	 *	We must ignore "." and "..", but other than that,
187*0Sstevel@tonic-gate 	 *	just create a path name and call self to check it out.
188*0Sstevel@tonic-gate 	 */
189*0Sstevel@tonic-gate 	while((dp = readdir(dirp)) != NULL) {
190*0Sstevel@tonic-gate 		if(strcmp(dp->d_name, ".") != 0 &&
191*0Sstevel@tonic-gate 			strcmp(dp->d_name, "..") != 0) {
192*0Sstevel@tonic-gate 				long here;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 			/* Append component name to the working path */
195*0Sstevel@tonic-gate 			(void)strcpy(component, dp->d_name);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 			/*
198*0Sstevel@tonic-gate 			 *	If we are about to exceed our depth,
199*0Sstevel@tonic-gate 			 *	remember where we are and close a file.
200*0Sstevel@tonic-gate 			 */
201*0Sstevel@tonic-gate 			if(depth <= 1) {
202*0Sstevel@tonic-gate 				here = telldir(dirp);
203*0Sstevel@tonic-gate 				closedir(dirp);
204*0Sstevel@tonic-gate 			}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 			/*
207*0Sstevel@tonic-gate 			 *	Do a recursive call to process the file.
208*0Sstevel@tonic-gate 			 *	(watch this, sports fans)
209*0Sstevel@tonic-gate 			 */
210*0Sstevel@tonic-gate 			rc = ftw(subpath, fn, depth-1);
211*0Sstevel@tonic-gate 			if(rc != 0) {
212*0Sstevel@tonic-gate 				free(subpath);
213*0Sstevel@tonic-gate 				if(depth > 1)
214*0Sstevel@tonic-gate 					closedir(dirp);
215*0Sstevel@tonic-gate 				return(rc);
216*0Sstevel@tonic-gate 			}
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 			/*
219*0Sstevel@tonic-gate 			 *	If we closed the file, try to reopen it.
220*0Sstevel@tonic-gate 			 */
221*0Sstevel@tonic-gate 			if(depth <= 1) {
222*0Sstevel@tonic-gate 				dirp = opendir(path);
223*0Sstevel@tonic-gate 				if(dirp == NULL) {
224*0Sstevel@tonic-gate 					free(subpath);
225*0Sstevel@tonic-gate 					return(-1);
226*0Sstevel@tonic-gate 				}
227*0Sstevel@tonic-gate 				seekdir(dirp, here);
228*0Sstevel@tonic-gate 			}
229*0Sstevel@tonic-gate 		}
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	/*
233*0Sstevel@tonic-gate 	 *	We got out of the subdirectory loop.  The return from
234*0Sstevel@tonic-gate 	 *	the final readdir is in dp.  Clean up.
235*0Sstevel@tonic-gate 	 */
236*0Sstevel@tonic-gate 	free(subpath);
237*0Sstevel@tonic-gate 	closedir(dirp);
238*0Sstevel@tonic-gate 	return(0);
239*0Sstevel@tonic-gate }
240