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