#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#include <string.h>
#include <errno.h>
#include <sys/fcntl.h>

#define MAX 65535 /* mind, (short) -1 == (ushort) 65535 */

static int fds[MAX];

int
main( int argc, char* argv[] )
{
  int i, fd;

  memset( fds, 0, sizeof(fds) );
  setbuf( stdout, 0 );

  for ( i=0; i<MAX; i++ ) {
    if ( (fds[i]=open( argv[0], O_RDONLY )) == -1 ) {
      if ( errno != EMFILE ) perror( "cannot open" );
      break;
    } else {
      if ( (i & 15) == 0 ) printf( "\ropened file #%d", i );
    }
  }

  printf( "\nYou can open %d files simultaneously.\n", i );
  for ( i--; i>=0; i-- ) close(fds[i]);
  return 0;
}

