1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7*2c2f96dcSApple OSS Distributions * Reserved. This file contains Original Code and/or Modifications of
8*2c2f96dcSApple OSS Distributions * Original Code as defined in and that are subject to the Apple Public
9*2c2f96dcSApple OSS Distributions * Source License Version 1.0 (the 'License'). You may not use this file
10*2c2f96dcSApple OSS Distributions * except in compliance with the License. Please obtain a copy of the
11*2c2f96dcSApple OSS Distributions * License at http://www.apple.com/publicsource and read it before using
12*2c2f96dcSApple OSS Distributions * this file.
13*2c2f96dcSApple OSS Distributions *
14*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
15*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19*2c2f96dcSApple OSS Distributions * License for the specific language governing rights and limitations
20*2c2f96dcSApple OSS Distributions * under the License."
21*2c2f96dcSApple OSS Distributions *
22*2c2f96dcSApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
23*2c2f96dcSApple OSS Distributions */
24*2c2f96dcSApple OSS Distributions /* openp, fopenp -- search pathlist and open file
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * Usage:
27*2c2f96dcSApple OSS Distributions * i = openp (path,file,complete,flags,mode)
28*2c2f96dcSApple OSS Distributions * f = fopenp (path,file,complete,type)
29*2c2f96dcSApple OSS Distributions * int i,flags,mode;
30*2c2f96dcSApple OSS Distributions * FILE *f;
31*2c2f96dcSApple OSS Distributions * char *path,*file,*complete,*type;
32*2c2f96dcSApple OSS Distributions *
33*2c2f96dcSApple OSS Distributions * Openp searches for "file" in the pathlist "path";
34*2c2f96dcSApple OSS Distributions * when the file is found and can be opened by open()
35*2c2f96dcSApple OSS Distributions * with the specified "flags" and "mode", then the full filename
36*2c2f96dcSApple OSS Distributions * is copied into "complete" and openp returns the file
37*2c2f96dcSApple OSS Distributions * descriptor. If no such file is found, openp returns -1.
38*2c2f96dcSApple OSS Distributions * Fopenp performs the same function, using fopen() instead
39*2c2f96dcSApple OSS Distributions * of open() and type instead of flags/mode; it returns 0 if no
40*2c2f96dcSApple OSS Distributions * file is found.
41*2c2f96dcSApple OSS Distributions *
42*2c2f96dcSApple OSS Distributions * HISTORY
43*2c2f96dcSApple OSS Distributions * 30-Apr-85 Steven Shafer (sas) at Carnegie-Mellon University
44*2c2f96dcSApple OSS Distributions * Adapted for 4.2 BSD UNIX. Added new parameter to openp.c;
45*2c2f96dcSApple OSS Distributions * changed names of flags, mode, and type parameters to reflect
46*2c2f96dcSApple OSS Distributions * current manual entries for open and fopen.
47*2c2f96dcSApple OSS Distributions *
48*2c2f96dcSApple OSS Distributions * 20-Nov-79 Steven Shafer (sas) at Carnegie-Mellon University
49*2c2f96dcSApple OSS Distributions * Created for VAX.
50*2c2f96dcSApple OSS Distributions *
51*2c2f96dcSApple OSS Distributions */
52*2c2f96dcSApple OSS Distributions
53*2c2f96dcSApple OSS Distributions #include <stdio.h>
54*2c2f96dcSApple OSS Distributions #include <fcntl.h> /* open */
55*2c2f96dcSApple OSS Distributions #include "config.h"
56*2c2f96dcSApple OSS Distributions
57*2c2f96dcSApple OSS Distributions
58*2c2f96dcSApple OSS Distributions int openp(const char *fpath, char *file, char *complete, int flags, int mode);
59*2c2f96dcSApple OSS Distributions
60*2c2f96dcSApple OSS Distributions static int flgs, mod, value;
61*2c2f96dcSApple OSS Distributions static const char *ftyp;
62*2c2f96dcSApple OSS Distributions static FILE *fvalue;
63*2c2f96dcSApple OSS Distributions
64*2c2f96dcSApple OSS Distributions static int
func(char * fnam)65*2c2f96dcSApple OSS Distributions func(char *fnam)
66*2c2f96dcSApple OSS Distributions {
67*2c2f96dcSApple OSS Distributions value = open(fnam, flgs, mod);
68*2c2f96dcSApple OSS Distributions return value < 0;
69*2c2f96dcSApple OSS Distributions }
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions static int
ffunc(char * fnam)72*2c2f96dcSApple OSS Distributions ffunc(char *fnam)
73*2c2f96dcSApple OSS Distributions {
74*2c2f96dcSApple OSS Distributions fvalue = fopen(fnam, ftyp);
75*2c2f96dcSApple OSS Distributions return fvalue == 0;
76*2c2f96dcSApple OSS Distributions }
77*2c2f96dcSApple OSS Distributions
78*2c2f96dcSApple OSS Distributions int
openp(const char * fpath,char * file,char * complete,int flags,int mode)79*2c2f96dcSApple OSS Distributions openp(const char *fpath, char *file, char *complete, int flags, int mode)
80*2c2f96dcSApple OSS Distributions {
81*2c2f96dcSApple OSS Distributions flgs = flags;
82*2c2f96dcSApple OSS Distributions mod = mode;
83*2c2f96dcSApple OSS Distributions if (searchp(fpath, file, complete, func) < 0) {
84*2c2f96dcSApple OSS Distributions return -1;
85*2c2f96dcSApple OSS Distributions }
86*2c2f96dcSApple OSS Distributions return value;
87*2c2f96dcSApple OSS Distributions }
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS Distributions FILE *
fopenp(const char * fpath,char * file,char * complete,const char * ftype)90*2c2f96dcSApple OSS Distributions fopenp(const char *fpath, char *file, char *complete, const char *ftype)
91*2c2f96dcSApple OSS Distributions {
92*2c2f96dcSApple OSS Distributions ftyp = ftype;
93*2c2f96dcSApple OSS Distributions if (searchp(fpath, file, complete, ffunc) < 0) {
94*2c2f96dcSApple OSS Distributions return 0;
95*2c2f96dcSApple OSS Distributions }
96*2c2f96dcSApple OSS Distributions return fvalue;
97*2c2f96dcSApple OSS Distributions }
98