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