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