BOOT2 - adjust hammerread code to understand the new directory localization cap.
[dragonfly.git] / contrib / opie / libopie / open.c
blobc0c76c20fab352d07ae935e3e844fd6fc1906338
1 /* open.c: The __opieopen() library function.
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you didn't get a copy, you may request one from <license@inner.net>.
9 History:
11 Modified by cmetz for OPIE 2.4. More portable way to get the mode
12 string for fopen.
13 Created by cmetz for OPIE 2.3.
15 #include "opie_cfg.h"
17 #include <stdio.h>
18 #include <sys/types.h>
19 #if HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif /* HAVE_UNISTD_H */
22 #include <sys/stat.h>
23 #include <errno.h>
25 #include "opie.h"
27 #if !HAVE_LSTAT
28 #define lstat(x, y) stat(x, y)
29 #endif /* !HAVE_LSTAT */
31 FILE *__opieopen FUNCTION((file, rw, mode), char *file AND int rw AND int mode)
33 FILE *f;
34 struct stat st;
36 if (lstat(file, &st)) {
37 if (errno != ENOENT)
38 return NULL;
40 if (!(f = fopen(file, "w")))
41 return NULL;
43 fclose(f);
45 if (chmod(file, mode))
46 return NULL;
48 if (lstat(file, &st))
49 return NULL;
52 if (!S_ISREG(st.st_mode))
53 return NULL;
56 char *fmode;
58 switch(rw) {
59 case 0:
60 fmode = "r";
61 break;
62 case 1:
63 fmode = "r+";
64 break;
65 case 2:
66 fmode = "a";
67 break;
68 default:
69 return NULL;
72 if (!(f = fopen(file, fmode)))
73 return NULL;
76 return f;