home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / libcs / pathof.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.8 KB  |  88 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. /*
  29.  * pathof - find path of a component needed by a program
  30.  *
  31.  * char *pathof(component)
  32.  * char *component;
  33.  *
  34.  * Where component is of the form "<prog>.<subprog>" or "<prog>.<subdir>"
  35.  *
  36.  * Examples of component are:
  37.  *      "sup.libpath"
  38.  *      "csh.profiledir"
  39.  *      "login.mkplan"
  40.  *
  41.  * If component is not found, pathof() returns NULL.  If component
  42.  * is found, a copy of the path for that component is salloc()ed and
  43.  * a pointer to the copy is returned.
  44.  *
  45.  **********************************************************************
  46.  * HISTORY
  47.  * $Log:    pathof.c,v $
  48.  * Revision 1.2  90/12/11  17:57:44  mja
  49.  *     Add copyright/disclaimer for distribution.
  50.  * 
  51.  * 30-Apr-88  Glenn Marcy (gm0w) at Carnegie-Mellon University
  52.  *    Created.
  53.  *
  54.  **********************************************************************
  55.  */
  56. #include <stdio.h>
  57.  
  58. char *pathof(component)
  59. char *component;
  60. {
  61.     FILE *f;
  62.     char *p, *q, buf[BUFSIZ];
  63.     char *salloc();
  64.     int len;
  65.  
  66.     if ((f = fopen("/usr/lib/paths", "r")) == NULL)
  67.     return(NULL);
  68.     len = strlen(component);
  69.     while ((p = fgets(buf, BUFSIZ, f)) != NULL) {
  70.     if (index("#;:\n", buf[0]) != NULL)
  71.         continue;
  72.     if (buf[len] != ' ' && buf[len] != '\t')
  73.         continue;
  74.     if (strncasecmp(buf, component, len) != 0)
  75.         continue;
  76.     p = buf + len + 1;
  77.     while (*p == ' ' || *p == '\t')
  78.         p++;
  79.     q = p;
  80.     while (*q != ' ' && *q != '\t' && *q != '\n' && *q != '\0')
  81.         q++;
  82.     *q = '\0';
  83.     break;
  84.     }
  85.     (void) fclose(f);
  86.     return(p ? salloc(p) : NULL);
  87. }
  88.