1989-12-18 14:40:54 +00:00
|
|
|
/*
|
|
|
|
* fdopen - convert a (UNIX) file descriptor into a FILE pointer
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-12-18 14:40:54 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "../stdio/loc_incl.h"
|
|
|
|
|
|
|
|
FILE *
|
|
|
|
fdopen(int fd, const char *mode)
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
FILE *stream;
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (fd < 0) return (FILE *)NULL;
|
|
|
|
for (i = 0; __iotab[i] != 0 ; i++)
|
1991-02-22 16:32:05 +00:00
|
|
|
if (i >= FOPEN_MAX-1)
|
1989-12-18 14:40:54 +00:00
|
|
|
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':
|
1990-04-24 09:40:47 +00:00
|
|
|
continue;
|
1989-12-18 14:40:54 +00:00
|
|
|
case '+':
|
|
|
|
flags |= _IOREAD | _IOWRITE;
|
1990-04-24 09:40:47 +00:00
|
|
|
continue;
|
|
|
|
/* The sequence may be followed by aditional characters */
|
1989-12-18 14:40:54 +00:00
|
|
|
default:
|
1990-04-24 09:40:47 +00:00
|
|
|
break;
|
1989-12-18 14:40:54 +00:00
|
|
|
}
|
1990-04-24 09:40:47 +00:00
|
|
|
break;
|
1989-12-18 14:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|