1/* Check that TRT happens when error on too many opened files.
2#notarget: cris*-*-elf
3#sim: --sysroot=@exedir@
4*/
5#include <stddef.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <unistd.h>
9#include <errno.h>
10#include <limits.h>
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <fcntl.h>
14#include <string.h>
15
16int main (int argc, char *argv[])
17{
18  int i;
19  int filemax;
20
21#ifdef OPEN_MAX
22  filemax = OPEN_MAX;
23#else
24  filemax = sysconf (_SC_OPEN_MAX);
25#endif
26
27  char *fn = malloc (strlen (argv[0]) + 2);
28  if (fn == NULL)
29    abort ();
30  strcpy (fn, "/");
31  strcat (fn, argv[0]);
32
33  for (i = 0; i < filemax + 1; i++)
34    {
35      if (open (fn, O_RDONLY) < 0)
36	{
37	  /* Shouldn't happen too early.  */
38	  if (i < filemax - 3 - 1)
39	    {
40	      fprintf (stderr, "i: %d\n", i);
41	      abort ();
42	    }
43	  if (errno != EMFILE)
44	    {
45	      perror ("open");
46	      abort ();
47	    }
48	  goto ok;
49	}
50    }
51  abort ();
52
53ok:
54  printf ("pass\n");
55  exit (0);
56}
57