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
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-03-27 06:59:07 +00:00
|
|
|
#if ACKCONF_WANT_O_TEXT_O_BINARY
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
1989-12-18 14:40:54 +00:00
|
|
|
|
2019-06-15 11:07:10 +00:00
|
|
|
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE
|
2018-06-23 16:54:40 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
FILE* fdopen(int fd, const char* mode)
|
1989-12-18 14:40:54 +00:00
|
|
|
{
|
|
|
|
register int i;
|
2018-06-21 20:33:47 +00:00
|
|
|
FILE* stream;
|
1989-12-18 14:40:54 +00:00
|
|
|
int flags = 0;
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return (FILE*)NULL;
|
|
|
|
for (i = 0; __iotab[i] != 0; i++)
|
|
|
|
if (i >= FOPEN_MAX - 1)
|
|
|
|
return (FILE*)NULL;
|
1989-12-18 14:40:54 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
flags |= _IOREAD | _IOREADING;
|
1990-04-24 09:40:47 +00:00
|
|
|
break;
|
2018-06-21 20:33:47 +00:00
|
|
|
case 'a':
|
|
|
|
flags |= _IOAPPEND;
|
|
|
|
case 'w':
|
|
|
|
flags |= _IOWRITE | _IOWRITING;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (FILE*)NULL;
|
|
|
|
}
|
|
|
|
while (*mode)
|
|
|
|
{
|
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'b':
|
2021-03-27 06:59:07 +00:00
|
|
|
#if ACKCONF_WANT_O_TEXT_O_BINARY
|
|
|
|
flags |= _IOBINARY;
|
|
|
|
#endif
|
2018-06-21 20:33:47 +00:00
|
|
|
continue;
|
|
|
|
case '+':
|
|
|
|
flags |= _IOREAD | _IOWRITE;
|
|
|
|
continue;
|
|
|
|
/* The sequence may be followed by aditional characters */
|
|
|
|
default:
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if ((stream = (FILE*)malloc(sizeof(FILE))) == NULL)
|
|
|
|
{
|
|
|
|
return (FILE*)NULL;
|
1989-12-18 14:40:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & _IOREAD) && (flags & _IOWRITE))
|
|
|
|
flags &= ~(_IOREADING | _IOWRITING);
|
|
|
|
|
2021-03-27 06:59:07 +00:00
|
|
|
#if ACKCONF_WANT_O_TEXT_O_BINARY
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* FIXME: this assumes that any platform that has O_TEXT and
|
|
|
|
* O_BINARY will also have a _setmode() function.
|
|
|
|
*/
|
|
|
|
extern int _setmode(int, int);
|
|
|
|
_setmode(fd, (flags & _IOBINARY) ? O_BINARY : O_TEXT);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1989-12-18 14:40:54 +00:00
|
|
|
stream->_count = 0;
|
|
|
|
stream->_fd = fd;
|
|
|
|
stream->_flags = flags;
|
|
|
|
stream->_buf = NULL;
|
|
|
|
__iotab[i] = stream;
|
|
|
|
return stream;
|
|
|
|
}
|
2018-06-23 16:54:40 +00:00
|
|
|
|
|
|
|
#endif
|