summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/isfopen.c
blob: 4a88f1d5b36f73b77385652a06584cd3be0334f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
**  Find out if a FILE * is valid
**
**  public domain demo by Bob Stout
*/

#include <stdio.h>

#ifdef __TURBOC__
 #define STREAM_BUF     _streams
 #define FCNT           FOPEN_MAX
 #define FLAG           flags
#else /* MSC, ZTC++ */
 #define STREAM_BUF     _iob
 #define FCNT           _NFILE
 #define FLAG           _flag
#endif

typedef enum {FALSE, TRUE} LOGICAL;

int isfopen(FILE *fp)
{
      int i;

      for (i = 0; i < FCNT; ++i)
      {
            if (0 != STREAM_BUF[i].FLAG && fp == &STREAM_BUF[i])
                  return TRUE;
      }
      return FALSE;
}

#ifdef TEST

void main(void)
{
      printf("stdout is%s valid\n", isfopen(stdout) ? "":" not");
      printf("buffer #10 is%s valid\n", isfopen(&STREAM_BUF[9]) ? "":" not");
}

#endif /*TEST */