Initial revision
This commit is contained in:
61
lang/cem/libcc.ansi/misc/fdopen.c
Normal file
61
lang/cem/libcc.ansi/misc/fdopen.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* fdopen - convert a (UNIX) file descriptor into a FILE pointer
|
||||
*/
|
||||
/* $Header$ */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../stdio/loc_incl.h"
|
||||
|
||||
int lseek(int d, int offset, int whence);
|
||||
|
||||
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++)
|
||||
if (i >= FOPEN_MAX)
|
||||
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':
|
||||
break;
|
||||
case '+':
|
||||
flags |= _IOREAD | _IOWRITE;
|
||||
break;
|
||||
default:
|
||||
return (FILE *)NULL;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user