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