xref: /xnu-10063.121.3/SETUP/config/searchp.c (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7*2c2f96dcSApple OSS Distributions  * Reserved.  This file contains Original Code and/or Modifications of
8*2c2f96dcSApple OSS Distributions  * Original Code as defined in and that are subject to the Apple Public
9*2c2f96dcSApple OSS Distributions  * Source License Version 1.0 (the 'License').  You may not use this file
10*2c2f96dcSApple OSS Distributions  * except in compliance with the License.  Please obtain a copy of the
11*2c2f96dcSApple OSS Distributions  * License at http://www.apple.com/publicsource and read it before using
12*2c2f96dcSApple OSS Distributions  * this file.
13*2c2f96dcSApple OSS Distributions  *
14*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
15*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19*2c2f96dcSApple OSS Distributions  * License for the specific language governing rights and limitations
20*2c2f96dcSApple OSS Distributions  * under the License."
21*2c2f96dcSApple OSS Distributions  *
22*2c2f96dcSApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
23*2c2f96dcSApple OSS Distributions  */
24*2c2f96dcSApple OSS Distributions /*  searchp  --  search through pathlist for file
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  *  Usage:  p = searchp (path,file,fullname,func);
27*2c2f96dcSApple OSS Distributions  *	char *p, *path, *file, *fullname;
28*2c2f96dcSApple OSS Distributions  *	int (*func)();
29*2c2f96dcSApple OSS Distributions  *
30*2c2f96dcSApple OSS Distributions  *  Searchp will parse "path", a list of pathnames separated
31*2c2f96dcSApple OSS Distributions  *  by colons, prepending each pathname to "file".  The resulting
32*2c2f96dcSApple OSS Distributions  *  filename will be passed to "func", a function provided by the
33*2c2f96dcSApple OSS Distributions  *  user.  This function must return zero if the search is
34*2c2f96dcSApple OSS Distributions  *  successful (i.e. ended), and non-zero if the search must
35*2c2f96dcSApple OSS Distributions  *  continue.  If the function returns zero (success), then
36*2c2f96dcSApple OSS Distributions  *  searching stops, the full filename is placed into "fullname",
37*2c2f96dcSApple OSS Distributions  *  and searchp returns 0.  If the pathnames are all unsuccessfully
38*2c2f96dcSApple OSS Distributions  *  examined, then searchp returns -1.
39*2c2f96dcSApple OSS Distributions  *  If "file" begins with a slash, it is assumed to be an
40*2c2f96dcSApple OSS Distributions  *  absolute pathname and the "path" list is not used.  Note
41*2c2f96dcSApple OSS Distributions  *  that this rule is used by Bell's cc also; whereas Bell's
42*2c2f96dcSApple OSS Distributions  *  sh uses the rule that any filename which CONTAINS a slash
43*2c2f96dcSApple OSS Distributions  *  is assumed to be absolute.  The execlp and execvp procedures
44*2c2f96dcSApple OSS Distributions  *  also use this latter rule.  In my opinion, this is bogosity.
45*2c2f96dcSApple OSS Distributions  *
46*2c2f96dcSApple OSS Distributions  *  HISTORY
47*2c2f96dcSApple OSS Distributions  * 01-Apr-86  Rudy Nedved (ern) at Carnegie-Mellon University
48*2c2f96dcSApple OSS Distributions  *	4.1BSD system ignores trailing slashes. 4.2BSD does not.
49*2c2f96dcSApple OSS Distributions  *	Therefore don't add a seperating slash if there is a null
50*2c2f96dcSApple OSS Distributions  *	filename.
51*2c2f96dcSApple OSS Distributions  *
52*2c2f96dcSApple OSS Distributions  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
53*2c2f96dcSApple OSS Distributions  *	Fixed two bugs: (1) calling function as "func" instead of
54*2c2f96dcSApple OSS Distributions  *	"(*func)", (2) omitting trailing null name implied by trailing
55*2c2f96dcSApple OSS Distributions  *	colon in path.  Latter bug fixed by introducing "lastchar" and
56*2c2f96dcSApple OSS Distributions  *	changing final loop test to look for "*lastchar" instead of
57*2c2f96dcSApple OSS Distributions  *	"*nextpath".
58*2c2f96dcSApple OSS Distributions  *
59*2c2f96dcSApple OSS Distributions  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
60*2c2f96dcSApple OSS Distributions  *	Created for VAX.  If you're thinking of using this, you probably
61*2c2f96dcSApple OSS Distributions  *	should look at openp() and fopenp() (or the "want..." routines)
62*2c2f96dcSApple OSS Distributions  *	instead.
63*2c2f96dcSApple OSS Distributions  *
64*2c2f96dcSApple OSS Distributions  */
65*2c2f96dcSApple OSS Distributions #include "config.h"
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions int
searchp(const char * spath,char * file,char * fullname,int (* func)(char *))68*2c2f96dcSApple OSS Distributions searchp(const char *spath, char *file, char *fullname, int (*func)(char *))
69*2c2f96dcSApple OSS Distributions {
70*2c2f96dcSApple OSS Distributions 	const char *nextpath, *nextchar, *lastchar;
71*2c2f96dcSApple OSS Distributions 	char *fname;
72*2c2f96dcSApple OSS Distributions 	int failure;
73*2c2f96dcSApple OSS Distributions 
74*2c2f96dcSApple OSS Distributions 	nextpath = ((*file == '/') ? "" : spath);
75*2c2f96dcSApple OSS Distributions 	do {
76*2c2f96dcSApple OSS Distributions 		fname = fullname;
77*2c2f96dcSApple OSS Distributions 		nextchar = nextpath;
78*2c2f96dcSApple OSS Distributions 		while (*nextchar && (*nextchar != ':')) {
79*2c2f96dcSApple OSS Distributions 			*fname++ = *nextchar++;
80*2c2f96dcSApple OSS Distributions 		}
81*2c2f96dcSApple OSS Distributions 		if (nextchar != nextpath && *file) {
82*2c2f96dcSApple OSS Distributions 			*fname++ = '/';
83*2c2f96dcSApple OSS Distributions 		}
84*2c2f96dcSApple OSS Distributions 		lastchar = nextchar;
85*2c2f96dcSApple OSS Distributions 		nextpath = ((*nextchar) ? nextchar + 1 : nextchar);
86*2c2f96dcSApple OSS Distributions 		nextchar = file;        /* append file */
87*2c2f96dcSApple OSS Distributions 		while (*nextchar) {
88*2c2f96dcSApple OSS Distributions 			*fname++ = *nextchar++;
89*2c2f96dcSApple OSS Distributions 		}
90*2c2f96dcSApple OSS Distributions 		*fname = '\0';
91*2c2f96dcSApple OSS Distributions 		failure = (*func)(fullname);
92*2c2f96dcSApple OSS Distributions 	}while (failure && (*lastchar));
93*2c2f96dcSApple OSS Distributions 	return failure ? -1 : 0;
94*2c2f96dcSApple OSS Distributions }
95