forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdopen.c
More file actions
executable file
·73 lines (64 loc) · 1.21 KB
/
Copy pathfdopen.c
File metadata and controls
executable file
·73 lines (64 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* fdopen - convert a (UNIX) file descriptor into a FILE pointer
*/
/* $Header$ */
#include <stdlib.h>
#include "../stdio/loc_incl.h"
#include <stdio.h>
#include <sys/stat.h>
FILE *
fdopen(fd, mode)
int fd;
_CONST char *mode;
{
register int i;
struct stat st;
FILE *stream;
int flags = 0;
if (fd < 0) return (FILE *)NULL;
for (i = 0; __iotab[i] != 0 ; i++)
if (i >= FOPEN_MAX-1)
return (FILE *)NULL;
switch(*mode++) {
case 'r':
flags |= _IOREAD | _IOREADING;
break;
case 'a':
flags |= _IOAPPEND;
case 'w':
flags |= _IOWRITE | _IOWRITING;
break;
default:
return (FILE *)NULL;
}
while(*mode) {
switch(*mode++) {
case 'b':
continue;
case '+':
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by aditional characters */
default:
break;
}
break;
}
if ( fstat( fd, &st ) < 0 ) {
return (FILE *)NULL;
}
if ( st.st_mode & S_IFIFO ) {
flags |= _IOFIFO;
}
if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) {
return (FILE *)NULL;
}
if ((flags & _IOREAD) && (flags & _IOWRITE))
flags &= ~(_IOREADING | _IOWRITING);
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
stream->_buf = NULL;
__iotab[i] = stream;
return stream;
}