|
|
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: break;
39: case 'a':
40: oflag = (plus ? O_RDWR : O_WRONLY) | O_APPEND | O_CREAT;
41: break;
42: case 'r':
43: oflag = plus ? O_RDWR : O_RDONLY;
44: break;
45: default:
46: return (NULL);
47: }
48: if ((fd = open(file, oflag, 0666)) < 0)
49: return (NULL);
50: iop->_cnt = 0;
51: iop->_file = fd;
52: iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
53: if (mode[0] == 'a') {
54: if (!plus) {
55: /* if update only mode, move file pointer to the end
56: of the file */
57: if ((lseek(fd,0L,2)) < 0) {
58: return NULL;
59: }
60: }
61: }
62: _bufend(iop) = iop->_base = iop->_ptr = NULL;
63: return (iop);
64: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.