ack/lang/cem/libcc.ansi/sys/stdio/freopen.c

99 lines
1.7 KiB
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* freopen.c - open a file and associate a stream with it
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-30 13:34:25 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "loc_incl.h"
1989-05-30 13:34:25 +00:00
2018-06-23 16:54:40 +00:00
#if ACKCONF_WANT_STDIO
2018-06-21 20:33:47 +00:00
#define PMODE 0666
1989-12-18 15:04:14 +00:00
/* Do not "optimize" this file to use the open with O_CREAT if the file
* does not exist. The reason is given in fopen.c.
*/
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
FILE* freopen(const char* name, const char* mode, FILE* stream)
1989-05-30 13:34:25 +00:00
{
register int i;
int rwmode = 0, rwflags = 0;
1989-12-18 15:04:14 +00:00
int fd, flags = stream->_flags & (_IONBF | _IOFBF | _IOLBF | _IOMYBUF);
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
(void)fflush(stream); /* ignore errors */
(void)close(fileno(stream));
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
switch (*mode++)
{
case 'r':
flags |= _IOREAD;
rwmode = O_RDONLY;
break;
case 'w':
flags |= _IOWRITE;
rwmode = O_WRONLY;
rwflags = O_CREAT | O_TRUNC;
break;
case 'a':
flags |= _IOWRITE | _IOAPPEND;
rwmode = O_WRONLY;
rwflags |= O_APPEND | O_CREAT;
break;
default:
return (FILE*)NULL;
1989-05-30 13:34:25 +00:00
}
2018-06-21 20:33:47 +00:00
while (*mode)
{
switch (*mode++)
{
case 'b':
continue;
case '+':
rwmode = O_RDWR;
flags |= _IOREAD | _IOWRITE;
continue;
/* The sequence may be followed by aditional characters */
default:
break;
1989-05-30 13:34:25 +00:00
}
break;
1989-05-30 13:34:25 +00:00
}
1989-12-18 15:04:14 +00:00
if ((rwflags & O_TRUNC)
|| (((fd = open(name, rwmode)) < 0)
2018-06-21 20:33:47 +00:00
&& (rwflags & O_CREAT)))
{
if (((fd = creat(name, PMODE)) < 0) && flags | _IOREAD)
{
(void)close(fd);
fd = open(name, rwmode);
1990-04-09 15:21:43 +00:00
}
}
1989-05-30 13:34:25 +00:00
2018-06-21 20:33:47 +00:00
if (fd < 0)
{
for (i = 0; i < FOPEN_MAX; i++)
{
if (stream == __iotab[i])
{
1989-12-18 15:04:14 +00:00
__iotab[i] = 0;
1989-05-30 13:34:25 +00:00
break;
}
}
if (stream != stdin && stream != stdout && stream != stderr)
2018-06-21 20:33:47 +00:00
free((void*)stream);
return (FILE*)NULL;
1989-05-30 13:34:25 +00:00
}
stream->_count = 0;
stream->_fd = fd;
stream->_flags = flags;
1989-06-26 10:37:05 +00:00
return stream;
1989-05-30 13:34:25 +00:00
}
2018-06-23 16:54:40 +00:00
#endif