open.c revision 92906
122347Spst/* open.c: The __opieopen() library function. 222347Spst 322347Spst%%% copyright-cmetz-96 422347SpstThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. 522347SpstThe Inner Net License Version 3 applies to this software. 622347SpstYou should have received a copy of the license with this software. If 722347Spstyou didn't get a copy, you may request one from <license@inner.net>. 822347Spst 922347Spst History: 1022347Spst 1122347Spst Modified by cmetz for OPIE 2.4. More portable way to get the mode 1222347Spst string for fopen. 1322347Spst Created by cmetz for OPIE 2.3. 1422347Spst*/ 1522347Spst#include "opie_cfg.h" 1622347Spst 1722347Spst#include <stdio.h> 1822347Spst#include <sys/types.h> 1922347Spst#if HAVE_UNISTD_H 2022347Spst#include <unistd.h> 2122347Spst#endif /* HAVE_UNISTD_H */ 2222347Spst#include <sys/stat.h> 2322347Spst#include <errno.h> 2422347Spst 2522347Spst#include "opie.h" 2622347Spst 2722347Spst#if !HAVE_LSTAT 2822347Spst#define lstat(x, y) stat(x, y) 2922347Spst#endif /* !HAVE_LSTAT */ 3022347Spst 3122347SpstFILE *__opieopen FUNCTION((file, rw, mode), char *file AND int rw AND int mode) 3222347Spst{ 3322347Spst FILE *f; 3422347Spst struct stat st; 3522347Spst 3622347Spst if (lstat(file, &st)) { 3722347Spst if (errno != ENOENT) 3822347Spst return NULL; 3922347Spst 4022347Spst if (!(f = fopen(file, "w"))) 4122347Spst return NULL; 4222347Spst 4322347Spst fclose(f); 4422347Spst 4522347Spst if (chmod(file, mode)) 4622347Spst return NULL; 4722347Spst 4822347Spst if (lstat(file, &st)) 4922347Spst return NULL; 5022347Spst } 5122347Spst 5222347Spst if (!S_ISREG(st.st_mode)) 5322347Spst return NULL; 5422347Spst 5522347Spst { 5622347Spst char *fmode; 5722347Spst 5822347Spst switch(rw) { 5922347Spst case 0: 6022347Spst fmode = "r"; 6122347Spst break; 62 case 1: 63 fmode = "r+"; 64 break; 65 case 2: 66 fmode = "a"; 67 break; 68 default: 69 return NULL; 70 }; 71 72 if (!(f = fopen(file, fmode))) 73 return NULL; 74 } 75 76 return f; 77} 78