open.c revision 29964
122347Spst/* open.c: The __opieopen() library function. 222347Spst 329964Sache%%% copyright-cmetz-96 429964SacheThis software is Copyright 1996-1997 by Craig Metz, All Rights Reserved. 522347SpstThe Inner Net License Version 2 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 Created by cmetz for OPIE 2.3. 1222347Spst*/ 1322347Spst#include "opie_cfg.h" 1422347Spst 1522347Spst#include <stdio.h> 1622347Spst#include <sys/types.h> 1722347Spst#if HAVE_UNISTD_H 1822347Spst#include <unistd.h> 1922347Spst#endif /* HAVE_UNISTD_H */ 2022347Spst#include <sys/stat.h> 2122347Spst#include <errno.h> 2222347Spst 2322347Spst#include "opie.h" 2422347Spst 2522347Spst#if !HAVE_LSTAT 2622347Spst#define lstat(x, y) stat(x, y) 2722347Spst#endif /* !HAVE_LSTAT */ 2822347Spst 2922347SpstFILE *__opieopen FUNCTION((file, rw, mode), char *file AND int rw AND int mode) 3022347Spst{ 3122347Spst FILE *f; 3222347Spst struct stat st; 3322347Spst 3422347Spst if (lstat(file, &st)) { 3522347Spst if (errno != ENOENT) 3622347Spst return NULL; 3722347Spst 3822347Spst if (!(f = fopen(file, "w"))) 3922347Spst return NULL; 4022347Spst 4122347Spst fclose(f); 4222347Spst 4322347Spst if (chmod(file, mode)) 4422347Spst return NULL; 4522347Spst 4622347Spst if (lstat(file, &st)) 4722347Spst return NULL; 4822347Spst } 4922347Spst 5022347Spst if (!S_ISREG(st.st_mode)) 5122347Spst return NULL; 5222347Spst 5322347Spst { 5422347Spst char *fmodes[] = { "r", "r+", "a" }; 5522347Spst 5622347Spst if (!(f = fopen(file, fmodes[rw]))) 5722347Spst return NULL; 5822347Spst } 5922347Spst 6022347Spst return f; 6122347Spst} 62