1 /* $Id: reservedfd.c 6135 2003-01-19 01:15:40Z rra $
12 static FILE **Reserved_fd = NULL;
13 static int Maxfd = -1;
18 static int allocated = 0;
19 int i, start = allocated;
22 if (Reserved_fd != NULL) {
23 for (i = 0 ; i < Maxfd ; i++) {
24 fclose(Reserved_fd[i]);
33 if (Reserved_fd == NULL) {
34 Reserved_fd = xmalloc(fdnum * sizeof(FILE *));
37 if (allocated < fdnum) {
38 Reserved_fd = xrealloc(Reserved_fd, fdnum * sizeof(FILE *));
40 } else if (Maxfd > fdnum) {
41 for (i = fdnum ; i < Maxfd ; i++) {
42 fclose(Reserved_fd[i]);
46 for (i = start ; i < fdnum ; i++) {
47 if (((Reserved_fd[i] = fopen("/dev/null", "r")) == NULL)){
48 for (--i ; i >= 0 ; i--)
49 fclose(Reserved_fd[i]);
62 Fopen(const char *p, const char *type, int xindex)
65 if (p == NULL || *p == '\0')
67 if (xindex < 0 || xindex > Maxfd || Reserved_fd[xindex] == NULL)
68 return fopen(p, type);
69 if ((nfp = freopen(p, type, Reserved_fd[xindex])) == NULL) {
70 Reserved_fd[xindex] = freopen("/dev/null", "r", Reserved_fd[xindex]);
73 return (Reserved_fd[xindex] = nfp);
83 for (i = 0 ; i < Maxfd ; i++) {
84 if (Reserved_fd[i] == fp)
89 Reserved_fd[i] = freopen("/dev/null", "r", Reserved_fd[i]);