xref: /titanic_53/usr/src/tools/cscope-fast/dir.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *	cscope - interactive C symbol cross-reference
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  *	directory searching functions
37*7c478bd9Sstevel@tonic-gate  */
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>	/* needed by stat.h */
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>	/* stat */
41*7c478bd9Sstevel@tonic-gate #include "global.h"
42*7c478bd9Sstevel@tonic-gate #include "dirent.h"
43*7c478bd9Sstevel@tonic-gate #include "vp.h"		/* vpdirs and vpndirs */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #define	DIRSEPS	" ,:"	/* directory list separators */
46*7c478bd9Sstevel@tonic-gate #define	DIRINC	10	/* directory list size increment */
47*7c478bd9Sstevel@tonic-gate #define	HASHMOD	2003	/* must be a prime number */
48*7c478bd9Sstevel@tonic-gate #define	SRCINC	HASHMOD	/* source file list size increment */
49*7c478bd9Sstevel@tonic-gate 			/* largest known database had 22049 files */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate char	**incdirs;		/* #include directories */
52*7c478bd9Sstevel@tonic-gate char	**srcdirs;		/* source directories */
53*7c478bd9Sstevel@tonic-gate char	**srcfiles;		/* source files */
54*7c478bd9Sstevel@tonic-gate int	nincdirs;		/* number of #include directories */
55*7c478bd9Sstevel@tonic-gate int	mincdirs = DIRINC;	/* maximum number of #include directories */
56*7c478bd9Sstevel@tonic-gate int	nsrcdirs;		/* number of source directories */
57*7c478bd9Sstevel@tonic-gate int	msrcdirs = DIRINC;	/* maximum number of source directories */
58*7c478bd9Sstevel@tonic-gate int	nsrcfiles;		/* number of source files */
59*7c478bd9Sstevel@tonic-gate int	msrcfiles = SRCINC;	/* maximum number of source files */
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static	struct	listitem {	/* source file table entry */
62*7c478bd9Sstevel@tonic-gate 	char	*file;
63*7c478bd9Sstevel@tonic-gate 	struct	listitem *next;
64*7c478bd9Sstevel@tonic-gate } *srcfiletable[HASHMOD];
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate static void getsrcfiles(char *vpdir, char *dir);
68*7c478bd9Sstevel@tonic-gate static BOOL issrcfile(char *file);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate /* add a source directory to the list for each view path source directory */
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate void
73*7c478bd9Sstevel@tonic-gate sourcedir(char *dirlist)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	struct	stat	statstruct;
76*7c478bd9Sstevel@tonic-gate 	char	*dir;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	/* don't change environment variable text */
79*7c478bd9Sstevel@tonic-gate 	dirlist = stralloc(dirlist);
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	/* parse the directory list */
82*7c478bd9Sstevel@tonic-gate 	dir = strtok(dirlist, DIRSEPS);
83*7c478bd9Sstevel@tonic-gate 	while (dir != NULL) {
84*7c478bd9Sstevel@tonic-gate 		/*
85*7c478bd9Sstevel@tonic-gate 		 * make sure it is a directory (must exist in current
86*7c478bd9Sstevel@tonic-gate 		 * view path node)
87*7c478bd9Sstevel@tonic-gate 		 */
88*7c478bd9Sstevel@tonic-gate 		if (stat(compath(dir), &statstruct) == 0 &&
89*7c478bd9Sstevel@tonic-gate 		    (statstruct.st_mode & S_IFDIR)) {
90*7c478bd9Sstevel@tonic-gate 			if (srcdirs == NULL) {
91*7c478bd9Sstevel@tonic-gate 				srcdirs = mymalloc(msrcdirs * sizeof (char *));
92*7c478bd9Sstevel@tonic-gate 			} else if (nsrcdirs == msrcdirs) {
93*7c478bd9Sstevel@tonic-gate 				msrcdirs += DIRINC;
94*7c478bd9Sstevel@tonic-gate 				srcdirs = myrealloc(srcdirs,
95*7c478bd9Sstevel@tonic-gate 				    msrcdirs * sizeof (char *));
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 			srcdirs[nsrcdirs++] = stralloc(dir);
98*7c478bd9Sstevel@tonic-gate 		}
99*7c478bd9Sstevel@tonic-gate 		dir = strtok((char *)NULL, DIRSEPS);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate /* add a #include directory to the list for each view path source directory */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate void
106*7c478bd9Sstevel@tonic-gate includedir(char *dirlist)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	struct	stat	statstruct;
109*7c478bd9Sstevel@tonic-gate 	char	*dir;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	/* don't change environment variable text */
112*7c478bd9Sstevel@tonic-gate 	dirlist = stralloc(dirlist);
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	/* parse the directory list */
115*7c478bd9Sstevel@tonic-gate 	dir = strtok(dirlist, DIRSEPS);
116*7c478bd9Sstevel@tonic-gate 	while (dir != NULL) {
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 		/*
119*7c478bd9Sstevel@tonic-gate 		 * make sure it is a directory (must exist in current
120*7c478bd9Sstevel@tonic-gate 		 * view path node)
121*7c478bd9Sstevel@tonic-gate 		 */
122*7c478bd9Sstevel@tonic-gate 		if (stat(compath(dir), &statstruct) == 0 &&
123*7c478bd9Sstevel@tonic-gate 		    (statstruct.st_mode & S_IFDIR)) {
124*7c478bd9Sstevel@tonic-gate 			if (incdirs == NULL) {
125*7c478bd9Sstevel@tonic-gate 				incdirs = mymalloc(mincdirs * sizeof (char *));
126*7c478bd9Sstevel@tonic-gate 			} else if (nincdirs == mincdirs) {
127*7c478bd9Sstevel@tonic-gate 				mincdirs += DIRINC;
128*7c478bd9Sstevel@tonic-gate 				incdirs = myrealloc(incdirs,
129*7c478bd9Sstevel@tonic-gate 				    mincdirs * sizeof (char *));
130*7c478bd9Sstevel@tonic-gate 			}
131*7c478bd9Sstevel@tonic-gate 			incdirs[nincdirs++] = stralloc(dir);
132*7c478bd9Sstevel@tonic-gate 		}
133*7c478bd9Sstevel@tonic-gate 		dir = strtok((char *)NULL, DIRSEPS);
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate /* make the source file list */
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate void
140*7c478bd9Sstevel@tonic-gate makefilelist(void)
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	static	BOOL	firstbuild = YES;	/* first time through */
143*7c478bd9Sstevel@tonic-gate 	FILE	*names;			/* name file pointer */
144*7c478bd9Sstevel@tonic-gate 	char	dir[PATHLEN + 1];
145*7c478bd9Sstevel@tonic-gate 	char	path[PATHLEN + 1];
146*7c478bd9Sstevel@tonic-gate 	struct	stat	statstruct;
147*7c478bd9Sstevel@tonic-gate 	char	*file;
148*7c478bd9Sstevel@tonic-gate 	char	*s;
149*7c478bd9Sstevel@tonic-gate 	int	i, j;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	/* if there are source file arguments */
152*7c478bd9Sstevel@tonic-gate 	if (fileargc > 0) {
153*7c478bd9Sstevel@tonic-gate 		/* put them in a list that can be expanded */
154*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < fileargc; ++i) {
155*7c478bd9Sstevel@tonic-gate 			file = fileargv[i];
156*7c478bd9Sstevel@tonic-gate 			if (infilelist(file) == NO) {
157*7c478bd9Sstevel@tonic-gate 				if (vpaccess(file, READ) == 0) {
158*7c478bd9Sstevel@tonic-gate 					addsrcfile(file);
159*7c478bd9Sstevel@tonic-gate 				} else {
160*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
161*7c478bd9Sstevel@tonic-gate 					    "cscope: cannot find file %s\n",
162*7c478bd9Sstevel@tonic-gate 					    file);
163*7c478bd9Sstevel@tonic-gate 					errorsfound = YES;
164*7c478bd9Sstevel@tonic-gate 				}
165*7c478bd9Sstevel@tonic-gate 			}
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 		return;
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 	/* see if a file name file exists */
170*7c478bd9Sstevel@tonic-gate 	if (namefile == NULL && vpaccess(NAMEFILE, READ) == 0) {
171*7c478bd9Sstevel@tonic-gate 		namefile = NAMEFILE;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	/* if there is a file of source file names */
174*7c478bd9Sstevel@tonic-gate 	if (namefile != NULL) {
175*7c478bd9Sstevel@tonic-gate 		if ((names = vpfopen(namefile, "r")) == NULL) {
176*7c478bd9Sstevel@tonic-gate 			cannotopen(namefile);
177*7c478bd9Sstevel@tonic-gate 			myexit(1);
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 		/* get the names in the file */
180*7c478bd9Sstevel@tonic-gate 		while (fscanf(names, "%s", path) == 1) {
181*7c478bd9Sstevel@tonic-gate 			if (*path == '-') {	/* if an option */
182*7c478bd9Sstevel@tonic-gate 				i = path[1];
183*7c478bd9Sstevel@tonic-gate 				switch (i) {
184*7c478bd9Sstevel@tonic-gate 				case 'q':	/* quick search */
185*7c478bd9Sstevel@tonic-gate 					invertedindex = YES;
186*7c478bd9Sstevel@tonic-gate 					break;
187*7c478bd9Sstevel@tonic-gate 				case 'T':
188*7c478bd9Sstevel@tonic-gate 					/* truncate symbols to 8 characters */
189*7c478bd9Sstevel@tonic-gate 					truncatesyms = YES;
190*7c478bd9Sstevel@tonic-gate 					break;
191*7c478bd9Sstevel@tonic-gate 				case 'I':	/* #include file directory */
192*7c478bd9Sstevel@tonic-gate 				case 'p':	/* file path components to */
193*7c478bd9Sstevel@tonic-gate 						/* display */
194*7c478bd9Sstevel@tonic-gate 					s = path + 2;	  /* for "-Ipath" */
195*7c478bd9Sstevel@tonic-gate 					if (*s == '\0') { /* if "-I path" */
196*7c478bd9Sstevel@tonic-gate 						(void) fscanf(names,
197*7c478bd9Sstevel@tonic-gate 						    "%s", path);
198*7c478bd9Sstevel@tonic-gate 						s = path;
199*7c478bd9Sstevel@tonic-gate 					}
200*7c478bd9Sstevel@tonic-gate 					switch (i) {
201*7c478bd9Sstevel@tonic-gate 					case 'I': /* #include file directory */
202*7c478bd9Sstevel@tonic-gate 						if (firstbuild == YES) {
203*7c478bd9Sstevel@tonic-gate 							/* expand $ and ~ */
204*7c478bd9Sstevel@tonic-gate 							shellpath(dir,
205*7c478bd9Sstevel@tonic-gate 							    sizeof (dir), s);
206*7c478bd9Sstevel@tonic-gate 							includedir(dir);
207*7c478bd9Sstevel@tonic-gate 						}
208*7c478bd9Sstevel@tonic-gate 						break;
209*7c478bd9Sstevel@tonic-gate 					case 'p':
210*7c478bd9Sstevel@tonic-gate 						/* file path components */
211*7c478bd9Sstevel@tonic-gate 						/* to display */
212*7c478bd9Sstevel@tonic-gate 						if (*s < '0' || *s > '9') {
213*7c478bd9Sstevel@tonic-gate 							(void) fprintf(stderr,
214*7c478bd9Sstevel@tonic-gate 							    "cscope: -p option "
215*7c478bd9Sstevel@tonic-gate 							    "in file %s: "
216*7c478bd9Sstevel@tonic-gate 							    "missing or "
217*7c478bd9Sstevel@tonic-gate 							    "invalid numeric "
218*7c478bd9Sstevel@tonic-gate 							    "value\n",
219*7c478bd9Sstevel@tonic-gate 							    namefile);
220*7c478bd9Sstevel@tonic-gate 						}
221*7c478bd9Sstevel@tonic-gate 						dispcomponents = atoi(s);
222*7c478bd9Sstevel@tonic-gate 						break;
223*7c478bd9Sstevel@tonic-gate 					}
224*7c478bd9Sstevel@tonic-gate 					break;
225*7c478bd9Sstevel@tonic-gate 				default:
226*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
227*7c478bd9Sstevel@tonic-gate 					    "cscope: only -I, -p, and -T "
228*7c478bd9Sstevel@tonic-gate 					    "options can be in file %s\n",
229*7c478bd9Sstevel@tonic-gate 					    namefile);
230*7c478bd9Sstevel@tonic-gate 				}
231*7c478bd9Sstevel@tonic-gate 			} else if (vpaccess(path, READ) == 0) {
232*7c478bd9Sstevel@tonic-gate 				addsrcfile(path);
233*7c478bd9Sstevel@tonic-gate 			} else {
234*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
235*7c478bd9Sstevel@tonic-gate 				    "cscope: cannot find file %s\n",
236*7c478bd9Sstevel@tonic-gate 				    path);
237*7c478bd9Sstevel@tonic-gate 				errorsfound = YES;
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 		(void) fclose(names);
241*7c478bd9Sstevel@tonic-gate 		firstbuild = NO;
242*7c478bd9Sstevel@tonic-gate 		return;
243*7c478bd9Sstevel@tonic-gate 	}
244*7c478bd9Sstevel@tonic-gate 	/* make a list of all the source files in the directories */
245*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsrcdirs; ++i) {
246*7c478bd9Sstevel@tonic-gate 		s = srcdirs[i];
247*7c478bd9Sstevel@tonic-gate 		getsrcfiles(s, s);
248*7c478bd9Sstevel@tonic-gate 		if (*s != '/') {	/* if it isn't a full path name */
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 			/* compute its path from any higher view path nodes */
251*7c478bd9Sstevel@tonic-gate 			for (j = 1; j < vpndirs; ++j) {
252*7c478bd9Sstevel@tonic-gate 				(void) sprintf(dir, "%s/%s", vpdirs[j], s);
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate 				/* make sure it is a directory */
255*7c478bd9Sstevel@tonic-gate 				if (stat(compath(dir), &statstruct) == 0 &&
256*7c478bd9Sstevel@tonic-gate 				    (statstruct.st_mode & S_IFDIR)) {
257*7c478bd9Sstevel@tonic-gate 					getsrcfiles(dir, s);
258*7c478bd9Sstevel@tonic-gate 				}
259*7c478bd9Sstevel@tonic-gate 			}
260*7c478bd9Sstevel@tonic-gate 		}
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate /* get the source file names in this directory */
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate static void
267*7c478bd9Sstevel@tonic-gate getsrcfiles(char *vpdir, char *dir)
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	DIR	*dirfile;	/* directory file descriptor */
270*7c478bd9Sstevel@tonic-gate 	struct	dirent	*entry;	/* directory entry pointer */
271*7c478bd9Sstevel@tonic-gate 	char	path[PATHLEN + 1];
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	/* attempt to open the directory */
274*7c478bd9Sstevel@tonic-gate 	if ((dirfile = opendir(vpdir)) != NULL) {
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		/* read each entry in the directory */
277*7c478bd9Sstevel@tonic-gate 		while ((entry = readdir(dirfile)) != NULL) {
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 			/* if it is a source file not already found */
280*7c478bd9Sstevel@tonic-gate 			(void) sprintf(path, "%s/%s", dir, entry->d_name);
281*7c478bd9Sstevel@tonic-gate 			if (entry->d_ino != 0 &&
282*7c478bd9Sstevel@tonic-gate 			    issrcfile(path) && infilelist(path) == NO) {
283*7c478bd9Sstevel@tonic-gate 				addsrcfile(path);	/* add it to the list */
284*7c478bd9Sstevel@tonic-gate 			}
285*7c478bd9Sstevel@tonic-gate 		}
286*7c478bd9Sstevel@tonic-gate 		closedir(dirfile);
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate }
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate /* see if this is a source file */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate static BOOL
293*7c478bd9Sstevel@tonic-gate issrcfile(char *file)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	struct	stat	statstruct;
296*7c478bd9Sstevel@tonic-gate 	char	*s;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/* if there is a file suffix */
299*7c478bd9Sstevel@tonic-gate 	if ((s = strrchr(file, '.')) != NULL && *++s != '\0') {
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 		/* if an SCCS or versioned file */
302*7c478bd9Sstevel@tonic-gate 		if (file[1] == '.' && file + 2 != s) { /* 1 character prefix */
303*7c478bd9Sstevel@tonic-gate 			switch (*file) {
304*7c478bd9Sstevel@tonic-gate 			case 's':
305*7c478bd9Sstevel@tonic-gate 			case 'S':
306*7c478bd9Sstevel@tonic-gate 				return (NO);
307*7c478bd9Sstevel@tonic-gate 			}
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 		if (s[1] == '\0') {	/* 1 character suffix */
310*7c478bd9Sstevel@tonic-gate 			switch (*s) {
311*7c478bd9Sstevel@tonic-gate 			case 'c':
312*7c478bd9Sstevel@tonic-gate 			case 'h':
313*7c478bd9Sstevel@tonic-gate 			case 'l':
314*7c478bd9Sstevel@tonic-gate 			case 'y':
315*7c478bd9Sstevel@tonic-gate 			case 'C':
316*7c478bd9Sstevel@tonic-gate 			case 'G':
317*7c478bd9Sstevel@tonic-gate 			case 'H':
318*7c478bd9Sstevel@tonic-gate 			case 'L':
319*7c478bd9Sstevel@tonic-gate 				return (YES);
320*7c478bd9Sstevel@tonic-gate 			}
321*7c478bd9Sstevel@tonic-gate 		} else if (s[2] == '\0') {	/* 2 character suffix */
322*7c478bd9Sstevel@tonic-gate 			if (*s == 'b' && s[1] == 'p' ||	/* breakpoint listing */
323*7c478bd9Sstevel@tonic-gate 			    *s == 'q' &&
324*7c478bd9Sstevel@tonic-gate 				(s[1] == 'c' || s[1] == 'h') || /* Ingres */
325*7c478bd9Sstevel@tonic-gate 			    *s == 'p' && s[1] == 'r' ||	/* SDL */
326*7c478bd9Sstevel@tonic-gate 			    *s == 's' && s[1] == 'd') {	/* SDL */
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 				/*
329*7c478bd9Sstevel@tonic-gate 				 * some directories have 2 character
330*7c478bd9Sstevel@tonic-gate 				 * suffixes so make sure it is a file
331*7c478bd9Sstevel@tonic-gate 				 */
332*7c478bd9Sstevel@tonic-gate 				if (vpstat(file, &statstruct) == 0 &&
333*7c478bd9Sstevel@tonic-gate 				    (statstruct.st_mode & S_IFREG)) {
334*7c478bd9Sstevel@tonic-gate 					return (YES);
335*7c478bd9Sstevel@tonic-gate 				}
336*7c478bd9Sstevel@tonic-gate 			}
337*7c478bd9Sstevel@tonic-gate 		}
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 	return (NO);
340*7c478bd9Sstevel@tonic-gate }
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /* add an include file to the source file list */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate void
345*7c478bd9Sstevel@tonic-gate incfile(char *file, int type)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	char	path[PATHLEN + 1];
348*7c478bd9Sstevel@tonic-gate 	int	i;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 	/* see if the file is already in the source file list */
351*7c478bd9Sstevel@tonic-gate 	if (infilelist(file) == YES) {
352*7c478bd9Sstevel@tonic-gate 		return;
353*7c478bd9Sstevel@tonic-gate 	}
354*7c478bd9Sstevel@tonic-gate 	/* look in current directory if it was #include "file" */
355*7c478bd9Sstevel@tonic-gate 	if (type == '"' && vpaccess(file, READ) == 0) {
356*7c478bd9Sstevel@tonic-gate 		addsrcfile(file);
357*7c478bd9Sstevel@tonic-gate 	} else {
358*7c478bd9Sstevel@tonic-gate 		/* search for the file in the #include directory list */
359*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < nincdirs; ++i) {
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 			/* don't include the file from two directories */
362*7c478bd9Sstevel@tonic-gate 			(void) sprintf(path, "%s/%s", incdirs[i], file);
363*7c478bd9Sstevel@tonic-gate 			if (infilelist(path) == YES) {
364*7c478bd9Sstevel@tonic-gate 				break;
365*7c478bd9Sstevel@tonic-gate 			}
366*7c478bd9Sstevel@tonic-gate 			/* make sure it exists and is readable */
367*7c478bd9Sstevel@tonic-gate 			if (vpaccess(compath(path), READ) == 0) {
368*7c478bd9Sstevel@tonic-gate 				addsrcfile(path);
369*7c478bd9Sstevel@tonic-gate 				break;
370*7c478bd9Sstevel@tonic-gate 			}
371*7c478bd9Sstevel@tonic-gate 		}
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate }
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate /* see if the file is already in the list */
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate BOOL
378*7c478bd9Sstevel@tonic-gate infilelist(char *file)
379*7c478bd9Sstevel@tonic-gate {
380*7c478bd9Sstevel@tonic-gate 	struct	listitem *p;
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 	for (p = srcfiletable[hash(compath(file)) % HASHMOD];
383*7c478bd9Sstevel@tonic-gate 	    p != NULL; p = p->next) {
384*7c478bd9Sstevel@tonic-gate 		if (strequal(file, p->file)) {
385*7c478bd9Sstevel@tonic-gate 			return (YES);
386*7c478bd9Sstevel@tonic-gate 		}
387*7c478bd9Sstevel@tonic-gate 	}
388*7c478bd9Sstevel@tonic-gate 	return (NO);
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate /* add a source file to the list */
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate void
394*7c478bd9Sstevel@tonic-gate addsrcfile(char *path)
395*7c478bd9Sstevel@tonic-gate {
396*7c478bd9Sstevel@tonic-gate 	struct	listitem *p;
397*7c478bd9Sstevel@tonic-gate 	int	i;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 	/* make sure there is room for the file */
400*7c478bd9Sstevel@tonic-gate 	if (nsrcfiles == msrcfiles) {
401*7c478bd9Sstevel@tonic-gate 		msrcfiles += SRCINC;
402*7c478bd9Sstevel@tonic-gate 		srcfiles = myrealloc(srcfiles, msrcfiles * sizeof (char *));
403*7c478bd9Sstevel@tonic-gate 	}
404*7c478bd9Sstevel@tonic-gate 	/* add the file to the list */
405*7c478bd9Sstevel@tonic-gate 	p = (struct listitem *)mymalloc(sizeof (struct listitem));
406*7c478bd9Sstevel@tonic-gate 	p->file = stralloc(compath(path));
407*7c478bd9Sstevel@tonic-gate 	i = hash(p->file) % HASHMOD;
408*7c478bd9Sstevel@tonic-gate 	p->next = srcfiletable[i];
409*7c478bd9Sstevel@tonic-gate 	srcfiletable[i] = p;
410*7c478bd9Sstevel@tonic-gate 	srcfiles[nsrcfiles++] = p->file;
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate /* free the memory allocated for the source file list */
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate void
416*7c478bd9Sstevel@tonic-gate freefilelist(void)
417*7c478bd9Sstevel@tonic-gate {
418*7c478bd9Sstevel@tonic-gate 	struct	listitem *p, *nextp;
419*7c478bd9Sstevel@tonic-gate 	int	i;
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	while (nsrcfiles > 0) {
422*7c478bd9Sstevel@tonic-gate 		free(srcfiles[--nsrcfiles]);
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < HASHMOD; ++i) {
425*7c478bd9Sstevel@tonic-gate 		for (p = srcfiletable[i]; p != NULL; p = nextp) {
426*7c478bd9Sstevel@tonic-gate 			nextp = p->next;
427*7c478bd9Sstevel@tonic-gate 			free(p);
428*7c478bd9Sstevel@tonic-gate 		}
429*7c478bd9Sstevel@tonic-gate 		srcfiletable[i] = NULL;
430*7c478bd9Sstevel@tonic-gate 	}
431*7c478bd9Sstevel@tonic-gate }
432