/* nfiles.c (emx+gcc) */

#include <sys/emx.h>
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>

static int verbose = 0;

static int nstreams (void)
{
  FILE *f;
  int i;

  f = fopen ("nul", "r");
  if (f == NULL)
    {
      if (verbose)
        putchar ('\n');
      return (0);
    }
  if (verbose)
    printf ("%d ", fileno (f));
  i = nstreams () + 1;
  fclose (f);
  return (i);
}


static int nhandles (void)
{
  int h, i;

  h = open ("nul", O_RDONLY);
  if (h < 0)
    {
      if (verbose)
        putchar ('\n');
      return (0);
    }
  if (verbose)
    printf ("%d ", h);
  i = nhandles () + 1;
  close (h);
  return (i);
}


int main (int argc, char *argv[])
{
  if (argc == 2 && strcmp (argv[1], "-v") == 0)
    verbose = 1;
  printf ("_nfiles=%d\n", _nfiles);
  printf ("%d handles\n", nhandles ());
  printf ("%d streams\n", nstreams ());
  return (0);
}
