1*b725ae77Skettenis /* 2*b725ae77Skettenis * This simple classical example of recursion is useful for 3*b725ae77Skettenis * testing stack backtraces and such. 4*b725ae77Skettenis */ 5*b725ae77Skettenis 6*b725ae77Skettenis #ifdef vxworks 7*b725ae77Skettenis 8*b725ae77Skettenis # include <stdio.h> 9*b725ae77Skettenis 10*b725ae77Skettenis /* VxWorks does not supply atoi. */ 11*b725ae77Skettenis static int atoi(char * z)12*b725ae77Skettenisatoi (char *z) 13*b725ae77Skettenis /* char *z;*/ 14*b725ae77Skettenis { 15*b725ae77Skettenis int i = 0; 16*b725ae77Skettenis 17*b725ae77Skettenis while (*z >= '0' && *z <= '9') 18*b725ae77Skettenis i = i * 10 + (*z++ - '0'); 19*b725ae77Skettenis return i; 20*b725ae77Skettenis } 21*b725ae77Skettenis 22*b725ae77Skettenis /* I don't know of any way to pass an array to VxWorks. This function 23*b725ae77Skettenis can be called directly from gdb. */ 24*b725ae77Skettenis vxmain(char * arg)25*b725ae77Skettenisvoid vxmain (char *arg) 26*b725ae77Skettenis /*char *arg;*/ 27*b725ae77Skettenis { 28*b725ae77Skettenis char *argv[2]; 29*b725ae77Skettenis 30*b725ae77Skettenis argv[0] = ""; 31*b725ae77Skettenis argv[1] = arg; 32*b725ae77Skettenis main (2, argv, (char **) 0); 33*b725ae77Skettenis } 34*b725ae77Skettenis 35*b725ae77Skettenis #else /* ! vxworks */ 36*b725ae77Skettenis # include <stdio.h> 37*b725ae77Skettenis # include <stdlib.h> 38*b725ae77Skettenis #endif /* ! vxworks */ 39*b725ae77Skettenis main(int argc,char * argv[],char ** envp)40*b725ae77Skettenisint main (int argc, char *argv[], char **envp) 41*b725ae77Skettenis /*int argc; 42*b725ae77Skettenis char *argv[], **envp;*/ 43*b725ae77Skettenis { 44*b725ae77Skettenis int factorial (int); 45*b725ae77Skettenis #ifdef usestubs 46*b725ae77Skettenis set_debug_traps(); 47*b725ae77Skettenis breakpoint(); 48*b725ae77Skettenis #endif 49*b725ae77Skettenis #ifdef FAKEARGV 50*b725ae77Skettenis printf ("%d\n", factorial (1)); 51*b725ae77Skettenis #else 52*b725ae77Skettenis if (argc != 2) { 53*b725ae77Skettenis printf ("usage: factorial <number>\n"); 54*b725ae77Skettenis return 1; 55*b725ae77Skettenis } else { 56*b725ae77Skettenis printf ("%d\n", factorial (atoi (argv[1]))); 57*b725ae77Skettenis } 58*b725ae77Skettenis #endif 59*b725ae77Skettenis return 0; 60*b725ae77Skettenis } 61*b725ae77Skettenis factorial(int value)62*b725ae77Skettenisint factorial (int value) 63*b725ae77Skettenis /*int value;*/ 64*b725ae77Skettenis { 65*b725ae77Skettenis int local_var; 66*b725ae77Skettenis 67*b725ae77Skettenis if (value > 1) { 68*b725ae77Skettenis value *= factorial (value - 1); 69*b725ae77Skettenis } 70*b725ae77Skettenis local_var = value; 71*b725ae77Skettenis return (value); 72*b725ae77Skettenis } 73