open.c revision 29964
1/* open.c: The __opieopen() library function. 2 3%%% copyright-cmetz-96 4This software is Copyright 1996-1997 by Craig Metz, All Rights Reserved. 5The Inner Net License Version 2 applies to this software. 6You should have received a copy of the license with this software. If 7you didn't get a copy, you may request one from <license@inner.net>. 8 9 History: 10 11 Created by cmetz for OPIE 2.3. 12*/ 13#include "opie_cfg.h" 14 15#include <stdio.h> 16#include <sys/types.h> 17#if HAVE_UNISTD_H 18#include <unistd.h> 19#endif /* HAVE_UNISTD_H */ 20#include <sys/stat.h> 21#include <errno.h> 22 23#include "opie.h" 24 25#if !HAVE_LSTAT 26#define lstat(x, y) stat(x, y) 27#endif /* !HAVE_LSTAT */ 28 29FILE *__opieopen FUNCTION((file, rw, mode), char *file AND int rw AND int mode) 30{ 31 FILE *f; 32 struct stat st; 33 34 if (lstat(file, &st)) { 35 if (errno != ENOENT) 36 return NULL; 37 38 if (!(f = fopen(file, "w"))) 39 return NULL; 40 41 fclose(f); 42 43 if (chmod(file, mode)) 44 return NULL; 45 46 if (lstat(file, &st)) 47 return NULL; 48 } 49 50 if (!S_ISREG(st.st_mode)) 51 return NULL; 52 53 { 54 char *fmodes[] = { "r", "r+", "a" }; 55 56 if (!(f = fopen(file, fmodes[rw]))) 57 return NULL; 58 } 59 60 return f; 61} 62