summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/favail.c
blob: 78a7c0d1c8dc9089e97b700b46b1eac36a5334e7 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
**  Find out how many more files can be fopen'ed
**
**  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

int favail(void)
{
      int i, count;

      for (i = count = 0; i < FCNT; ++i)
      {
            if (0 == STREAM_BUF[i].FLAG)
                  ++count;
      }
      return count;
}

#ifdef TEST

void main(void)
{
      char *fname = "A$$$$$$$.$$$";
      FILE *fp;

      do
      {
            int i = favail();

            printf("You can fopen %d new file%s\n", i, &"s"[i == 1]);
            fp = fopen(fname, "w");
            *fname += 1;
      } while (fp);

      do
      {
            printf("removing %s\n", fname);
            remove(fname);
      } while ('A' <= --(*fname)) ;
}

#endif /*TEST */