122347Spst/* open.c: The __opieopen() library function. 222347Spst 329964Sache%%% copyright-cmetz-96 492906SmarkmThis software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. 592906SmarkmThe 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 1192906Smarkm Modified by cmetz for OPIE 2.4. More portable way to get the mode 1292906Smarkm 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 { 5692906Smarkm char *fmode; 5722347Spst 5892906Smarkm switch(rw) { 5992906Smarkm case 0: 6092906Smarkm fmode = "r"; 6192906Smarkm break; 6292906Smarkm case 1: 6392906Smarkm fmode = "r+"; 6492906Smarkm break; 6592906Smarkm case 2: 6692906Smarkm fmode = "a"; 6792906Smarkm break; 6892906Smarkm default: 6992906Smarkm return NULL; 7092906Smarkm }; 7192906Smarkm 7292906Smarkm if (!(f = fopen(file, fmode))) 7322347Spst return NULL; 7422347Spst } 7522347Spst 7622347Spst return f; 7722347Spst} 78