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