test-c-stack: fix compilation failure on FreeBSD 5.0
[gnulib.git] / tests / test-c-stack.c
1 /* Test of c-stack module.
2    Copyright (C) 2002, 2004, 2006, 2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 #include "c-stack.h"
20
21 #include "exitfail.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #if HAVE_SETRLIMIT
25 /* At least FreeBSD 5.0 needs extra headers before <sys/resource.h>
26    will compile.  */
27 # include <sys/types.h>
28 # include <sys/time.h>
29 # include <sys/resource.h>
30 #endif
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 static long
45 recurse (char *p)
46 {
47   char array[500];
48   array[0] = 1;
49   return *p + recurse (array);
50 }
51
52 char *program_name;
53
54 int
55 main (int argc, char **argv)
56 {
57 #if HAVE_SETRLIMIT && defined RLIMIT_STACK
58   /* Before starting the endless recursion, try to be friendly to the
59      user's machine.  On some Linux 2.2.x systems, there is no stack
60      limit for user processes at all.  We don't want to kill such
61      systems.  */
62   struct rlimit rl;
63   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
64   setrlimit (RLIMIT_STACK, &rl);
65 #endif
66
67   program_name = argv[0];
68   if (c_stack_action (0) == 0)
69     {
70       if (1 < argc)
71         {
72           exit_failure = 77;
73           ++*argv[argc]; /* Intentionally dereference NULL.  */
74         }
75       return recurse ("\1");
76     }
77   perror ("c_stack_action");
78   return 77;
79 }