/*  Squares.c  -   Last Edited: 05/8/89       Author: Michael S. Fransyshen

    This program is an example of HOW to pass arguments to a C program
    from the CLI environment.  It accepts two numbers (ints) and outputs
    the squares of the numbers in the specified range.  The program's real
    purpose is solely demonstrative, not in its utility.  Notice that 
    a large majority of the program is in the error handling for user 
    input.                              */

Bad_Input()      /* This function is called when user input does not meet
                    the required criteria                              */
{

   printf("\nSpecify LEGAL START and END values for the output range!\n");
   printf("USAGE:  Squares <Start> <End>\n");
   printf("Where START and END fall within the range: -32768 to +32767\n\n");
   exit();
}

main(argc, argv)
int  argc;
char *argv[];
{
 long   Square,Current,End,Error,Start,Temp;
 long   atol();

 if( argc != 3 )( Bad_Input() ); /* Check for correct number of arguments */
 
 Start = atol(argv[1]);
 End = atol(argv[2]);
 
 if(Start > 32767 || Start < -32678) ( Bad_Input() );
 if(End > 32767 || End < -32678) ( Bad_Input() );
 
 /* Since atol() will return a 0 value if converting letters, we must check
    to see if the user typed a 0 as an argument, or a string of other ASCII
    characters at the CLI.  In the line below, I test for the ACSII value of
    0 (Zero) pointed to by *argv[1] or *argv[2].  A more thorough explanation
    may be found in the accompanying Square.doc file  */
 
 if(*argv[1] != 48 && Start == 0L || *argv[2] != 48 && End == 0L)
   ( Bad_Input() );
 
 if(Start > End)
  {
    Temp = Start;      /* Control for input where Start > End.  */
    Start = End;       /* Swap the values, Display output from  */
    End = Temp;        /* lowest to highest.                    */
  }

 for(Current = Start; Current <= End; Current++)
  {
    Square = Current * Current;
    printf("Current Value: %ld \t  Squared Value: %ld\n",Current,Square);
  }
}
