/* ---------------------------------------------------------------- */
/* PROGRAM  local-1.c:                                              */
/*   This program uses setjmp()/longjmp() to simulate goto.         */
/* ---------------------------------------------------------------- */

#include  <stdio.h>
#include  <setjmp.h>

#define   DEFAULT_n   10

void  main(int argc, char *argv[])
{
     jmp_buf   Buffer;
     long      fact, i, number;
     
     if (argc != 2) {
          printf("Use %s n\n", argv[0]);
          printf("n is set to %d\n", DEFAULT_n);
          number = DEFAULT_n;
     }
     else 
          number = atoi(argv[1]);
          
     printf("Factorial Computation:\n");
     fact   = i = 1;                    /* initialization           */
     setjmp(Buffer);                    /* this is a goto label     */
     fact *= i;                         /* compute product          */
     printf("%d! = %d\n", i, fact);
     i++;
     if (i <= number)                   /* is the end of the loop?  */
          longjmp(Buffer, 1);           /* no, go back              */
}