1: /* @(#)endopen.c 2.1 SCCS id keyword */ 2: #include <stdio.h> 3: #include <errno.h> 4: 5: FILE * 6: _endopen(file, mode, iop) 7: char *file, *mode; 8: register FILE *iop; 9: { 10: extern int errno; 11: register int rw, f; 12: 13: if (iop == NULL) 14: return(NULL); 15: 16: rw = mode[1] == '+'; 17: 18: switch (*mode) { 19: 20: case 'w': 21: f = create(file, rw); 22: break; 23: 24: case 'a': 25: if ((f = open(file, rw? 2: 1)) < 0) { 26: if (errno == ENOENT) 27: f = create(file, rw); 28: } 29: lseek(f, 0L, 2); 30: break; 31: 32: case 'r': 33: f = open(file, rw? 2: 0); 34: break; 35: 36: default: 37: return(NULL); 38: } 39: 40: if (f < 0) 41: return(NULL); 42: 43: iop->_cnt = 0; 44: iop->_file = f; 45: 46: if (rw) 47: iop->_flag |= _IORW; 48: else if(*mode == 'r') 49: iop->_flag |= _IOREAD; 50: else 51: iop->_flag |= _IOWRT; 52: 53: return(iop); 54: } 55: 56: static 57: create(file, rw) 58: register char *file; 59: int rw; 60: { 61: register int f; 62: 63: f = creat(file, 0666); 64: if (rw && f>=0) { 65: close(f); 66: f = open(file, 2); 67: } 68: return(f); 69: }