home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / openp.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  103 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. /*  openp, fopenp  --  search pathlist and open file
  29.  *
  30.  *  Usage:
  31.  *    i = openp (path,file,complete,flags,mode)
  32.  *    f = fopenp (path,file,complete,type)
  33.  *    int i,flags,mode;
  34.  *    FILE *f;
  35.  *    char *path,*file,*complete,*type;
  36.  *
  37.  *  Openp searches for "file" in the pathlist "path";
  38.  *  when the file is found and can be opened by open()
  39.  *  with the specified "flags" and "mode", then the full filename
  40.  *  is copied into "complete" and openp returns the file
  41.  *  descriptor.  If no such file is found, openp returns -1.
  42.  *  Fopenp performs the same function, using fopen() instead
  43.  *  of open() and type instead of flags/mode; it returns 0 if no
  44.  *  file is found.
  45.  *
  46.  *  HISTORY
  47.  * $Log:    openp.c,v $
  48.  * Revision 1.2  90/12/11  17:57:28  mja
  49.  *     Add copyright/disclaimer for distribution.
  50.  * 
  51.  * 30-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  52.  *    Adapted for 4.2 BSD UNIX.  Added new parameter to openp.c;
  53.  *    changed names of flags, mode, and type parameters to reflect
  54.  *    current manual entries for open and fopen.
  55.  *
  56.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  57.  *    Created for VAX.
  58.  *
  59.  */
  60.  
  61. #include <stdio.h>
  62.  
  63. int open();
  64. int searchp();
  65.  
  66. static int flgs,mod,value;
  67. static char *ftyp;
  68. static FILE *fvalue;
  69.  
  70. static int func (fnam)
  71. char *fnam;
  72. {
  73.     value = open (fnam,flgs,mod);
  74.     return (value < 0);
  75. }
  76.  
  77. static int ffunc (fnam)
  78. char *fnam;
  79. {
  80.     fvalue = fopen (fnam,ftyp);
  81.     return (fvalue == 0);
  82. }
  83.  
  84. int openp (path,file,complete,flags,mode)
  85. char *path, *file, *complete;
  86. int flags, mode;
  87. {
  88.     register char *p;
  89.     flgs = flags;
  90.     mod = mode;
  91.     if (searchp(path,file,complete,func) < 0)  return (-1);
  92.     return (value);
  93. }
  94.  
  95. FILE *fopenp (path,file,complete,ftype)
  96. char *path, *file, *complete, *ftype;
  97. {
  98.     register char *p;
  99.     ftyp = ftype;
  100.     if (searchp(path,file,complete,ffunc) < 0)  return (0);
  101.     return (fvalue);
  102. }
  103.