maint: update copyright
[gnulib.git] / tests / test-c-stack.c
1 /* Test of c-stack module.
2    Copyright (C) 2002, 2004, 2006, 2008-2014 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 #if HAVE_SETRLIMIT
24 /* At least FreeBSD 5.0 needs extra headers before <sys/resource.h>
25    will compile.  */
26 # include <sys/types.h>
27 # include <sys/time.h>
28 # include <sys/resource.h>
29 #endif
30
31 #include "macros.h"
32
33 char *program_name;
34
35 static volatile int *
36 recurse_1 (volatile int n, volatile int *p)
37 {
38   if (n >= 0)
39     *recurse_1 (n + 1, p) += n;
40   return p;
41 }
42
43 static int
44 recurse (volatile int n)
45 {
46   int sum = 0;
47   return *recurse_1 (n, &sum);
48 }
49
50 int
51 main (int argc, char **argv)
52 {
53 #if HAVE_SETRLIMIT && defined RLIMIT_STACK
54   /* Before starting the endless recursion, try to be friendly to the
55      user's machine.  On some Linux 2.2.x systems, there is no stack
56      limit for user processes at all.  We don't want to kill such
57      systems.  */
58   struct rlimit rl;
59   rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
60   setrlimit (RLIMIT_STACK, &rl);
61 #endif
62
63   program_name = argv[0];
64   if (c_stack_action (NULL) == 0)
65     {
66       if (1 < argc)
67         {
68           exit_failure = 77;
69           ++*argv[argc]; /* Intentionally dereference NULL.  */
70         }
71       return recurse (0);
72     }
73   fputs ("skipping test: ", stderr);
74   perror ("c_stack_action");
75   return 77;
76 }