Uninitialized vector entry?
[minix3.git] / lib / other / fdopen.c
blob3a72099e8521892cf322994098dc2e8c51fad9fc
1 /*
2 * fdopen - convert a (UNIX) file descriptor into a FILE pointer
3 */
4 /* $Header$ */
6 #include <stdlib.h>
7 #include "../stdio/loc_incl.h"
8 #include <stdio.h>
9 #include <sys/stat.h>
11 FILE *
12 fdopen(fd, mode)
13 int fd;
14 _CONST char *mode;
16 register int i;
17 struct stat st;
18 FILE *stream;
19 int flags = 0;
21 if (fd < 0) return (FILE *)NULL;
22 for (i = 0; __iotab[i] != 0 ; i++)
23 if (i >= FOPEN_MAX-1)
24 return (FILE *)NULL;
26 switch(*mode++) {
27 case 'r':
28 flags |= _IOREAD | _IOREADING;
29 break;
30 case 'a':
31 flags |= _IOAPPEND;
32 case 'w':
33 flags |= _IOWRITE | _IOWRITING;
34 break;
35 default:
36 return (FILE *)NULL;
38 while(*mode) {
39 switch(*mode++) {
40 case 'b':
41 continue;
42 case '+':
43 flags |= _IOREAD | _IOWRITE;
44 continue;
45 /* The sequence may be followed by aditional characters */
46 default:
47 break;
49 break;
52 if ( fstat( fd, &st ) < 0 ) {
53 return (FILE *)NULL;
56 if ( st.st_mode & S_IFIFO ) {
57 flags |= _IOFIFO;
60 if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) {
61 return (FILE *)NULL;
64 if ((flags & _IOREAD) && (flags & _IOWRITE))
65 flags &= ~(_IOREADING | _IOWRITING);
67 stream->_count = 0;
68 stream->_fd = fd;
69 stream->_flags = flags;
70 stream->_buf = NULL;
71 __iotab[i] = stream;
72 return stream;