home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / searchp.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  4KB  |  98 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  searchp  --  search through pathlist for file
  29.  *
  30.  *  Usage:  p = searchp (path,file,fullname,func);
  31.  *    char *p, *path, *file, *fullname;
  32.  *    int (*func)();
  33.  *
  34.  *  Searchp will parse "path", a list of pathnames separated
  35.  *  by colons, prepending each pathname to "file".  The resulting
  36.  *  filename will be passed to "func", a function provided by the
  37.  *  user.  This function must return zero if the search is
  38.  *  successful (i.e. ended), and non-zero if the search must
  39.  *  continue.  If the function returns zero (success), then
  40.  *  searching stops, the full filename is placed into "fullname",
  41.  *  and searchp returns 0.  If the pathnames are all unsuccessfully
  42.  *  examined, then searchp returns -1.
  43.  *  If "file" begins with a slash, it is assumed to be an
  44.  *  absolute pathname and the "path" list is not used.  Note
  45.  *  that this rule is used by Bell's cc also; whereas Bell's
  46.  *  sh uses the rule that any filename which CONTAINS a slash
  47.  *  is assumed to be absolute.  The execlp and execvp procedures
  48.  *  also use this latter rule.  In my opinion, this is bogosity.
  49.  *
  50.  *  HISTORY
  51.  * $Log:    searchp.c,v $
  52.  * Revision 1.2  90/12/11  17:58:36  mja
  53.  *     Add copyright/disclaimer for distribution.
  54.  * 
  55.  * 01-Apr-86  Rudy Nedved (ern) at Carnegie-Mellon University
  56.  *    4.1BSD system ignores trailing slashes. 4.2BSD does not. 
  57.  *    Therefore don't add a seperating slash if there is a null
  58.  *    filename.
  59.  *
  60.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  61.  *    Fixed two bugs: (1) calling function as "func" instead of
  62.  *    "(*func)", (2) omitting trailing null name implied by trailing
  63.  *    colon in path.  Latter bug fixed by introducing "lastchar" and
  64.  *    changing final loop test to look for "*lastchar" instead of
  65.  *    "*nextpath".
  66.  *
  67.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  68.  *    Created for VAX.  If you're thinking of using this, you probably
  69.  *    should look at openp() and fopenp() (or the "want..." routines)
  70.  *    instead.
  71.  *
  72.  */
  73.  
  74. int searchp (path,file,fullname,func)
  75. char *path,*file,*fullname;
  76. int (*func)();
  77. {
  78.     register char *nextpath,*nextchar,*fname,*lastchar;
  79.     int failure;
  80.  
  81.     nextpath = ((*file == '/') ? "" : path);
  82.     do {
  83.         fname = fullname;
  84.         nextchar = nextpath;
  85.         while (*nextchar && (*nextchar != ':'))
  86.             *fname++ = *nextchar++;
  87.         if (nextchar != nextpath && *file) *fname++ = '/';
  88.         lastchar = nextchar;
  89.         nextpath = ((*nextchar) ? nextchar + 1 : nextchar);
  90.         nextchar = file;    /* append file */
  91.         while (*nextchar)  *fname++ = *nextchar++;
  92.         *fname = '\0';
  93.         failure = (*func) (fullname);
  94.     } 
  95.     while (failure && (*lastchar));
  96.     return (failure ? -1 : 0);
  97. }
  98.