Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* |
2 | * This simple classical example of recursion is useful for | |
3 | * testing stack backtraces and such. | |
4 | */ | |
5 | ||
428b16bd PA |
6 | #include <stdio.h> |
7 | #include <stdlib.h> | |
c906108c | 8 | |
dedad4e3 PA |
9 | #include "../lib/unbuffer_output.c" |
10 | ||
085dd6e6 JM |
11 | int factorial (int); |
12 | ||
13 | int | |
14 | main (int argc, char **argv, char **envp) | |
c906108c | 15 | { |
dedad4e3 PA |
16 | gdb_unbuffer_output (); |
17 | ||
c906108c | 18 | #ifdef FAKEARGV |
95e4302a | 19 | printf ("%d\n", factorial (1)); /* commands.exp: hw local_var out of scope */ |
c906108c SS |
20 | #else |
21 | if (argc != 2) { | |
22 | printf ("usage: factorial <number>\n"); | |
23 | return 1; | |
24 | } else { | |
25 | printf ("%d\n", factorial (atoi (argv[1]))); | |
26 | } | |
27 | #endif | |
28 | return 0; | |
29 | } | |
30 | ||
085dd6e6 | 31 | int factorial (int value) |
c906108c | 32 | { |
085dd6e6 JM |
33 | int local_var; |
34 | ||
c906108c SS |
35 | if (value > 1) { |
36 | value *= factorial (value - 1); | |
37 | } | |
085dd6e6 | 38 | local_var = value; |
c906108c | 39 | return (value); |
95e4302a | 40 | } /* commands.exp: local_var out of scope */ |