|
|
1.1 root 1: /* @(#)fopen.c 1.8 */
2: /*LINTLIBRARY*/
3: #include "stdio.h"
4: #include <fcntl.h>
5:
6: extern int open(), fclose();
7: extern FILE *_findiop(), *_endopen();
8:
9: FILE *
10: fopen(file, mode)
11: char *file, *mode;
12: {
13: return (_endopen(file, mode, _findiop()));
14: }
15:
16: FILE *
17: freopen(file, mode, iop)
18: char *file, *mode;
19: register FILE *iop;
20: {
21: (void) fclose(iop); /* doesn't matter if this fails */
22: return (_endopen(file, mode, iop));
23: }
24:
25: static FILE *
26: _endopen(file, mode, iop)
27: char *file, *mode;
28: register FILE *iop;
29: {
30: register int plus, oflag, fd;
31:
32: if (iop == NULL || file == NULL || file[0] == '\0')
33: return (NULL);
34: plus = (mode[1] == '+');
35: switch (mode[0]) {
36: case 'w':
37: oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
38: (void) close(creat(file, 0666));
39: break;
40: case 'a':
41: oflag = (plus ? O_RDWR : O_WRONLY) | O_APPEND | O_CREAT;
42: break;
43: case 'r':
44: oflag = plus ? O_RDWR : O_RDONLY;
45: break;
46: default:
47: return (NULL);
48: }
49: if ((fd = open(file, oflag, 0666)) < 0)
50: return (NULL);
51: iop->_cnt = 0;
52: iop->_file = fd;
53: iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
54: if (mode[0] == 'a') {
55: if (!plus) {
56: /* if update only mode, move file pointer to the end
57: of the file */
58: if ((lseek(fd,0L,2)) < 0) {
59: return NULL;
60: }
61: }
62: }
63: _bufend(iop) = iop->_base = iop->_ptr = NULL;
64: return (iop);
65: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.