Line | |
---|
1 | /*
|
---|
2 | * fdopen - convert a (UNIX) file descriptor into a FILE pointer
|
---|
3 | */
|
---|
4 | /* $Header: /cvsup/minix/src/lib/other/fdopen.c,v 1.2 2005/09/06 10:15:57 beng Exp $ */
|
---|
5 |
|
---|
6 | #include <stdlib.h>
|
---|
7 | #include "../stdio/loc_incl.h"
|
---|
8 | #include <stdio.h>
|
---|
9 | #include <sys/stat.h>
|
---|
10 |
|
---|
11 | FILE *
|
---|
12 | fdopen(fd, mode)
|
---|
13 | int fd;
|
---|
14 | _CONST char *mode;
|
---|
15 | {
|
---|
16 | register int i;
|
---|
17 | struct stat st;
|
---|
18 | FILE *stream;
|
---|
19 | int flags = 0;
|
---|
20 |
|
---|
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;
|
---|
25 |
|
---|
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;
|
---|
37 | }
|
---|
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;
|
---|
48 | }
|
---|
49 | break;
|
---|
50 | }
|
---|
51 |
|
---|
52 | if ( fstat( fd, &st ) < 0 ) {
|
---|
53 | return (FILE *)NULL;
|
---|
54 | }
|
---|
55 |
|
---|
56 | if ( st.st_mode & S_IFIFO ) {
|
---|
57 | flags |= _IOFIFO;
|
---|
58 | }
|
---|
59 |
|
---|
60 | if ((stream = (FILE *) malloc(sizeof(FILE))) == NULL) {
|
---|
61 | return (FILE *)NULL;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if ((flags & _IOREAD) && (flags & _IOWRITE))
|
---|
65 | flags &= ~(_IOREADING | _IOWRITING);
|
---|
66 |
|
---|
67 | stream->_count = 0;
|
---|
68 | stream->_fd = fd;
|
---|
69 | stream->_flags = flags;
|
---|
70 | stream->_buf = NULL;
|
---|
71 | __iotab[i] = stream;
|
---|
72 | return stream;
|
---|
73 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.