/************************************************************************
 *                                                                      *
 *                            Using the Command Line                    *
 *                            ======================                    *
 *                                                                      *
 *          Copyright © 1993 by Mark Little, All Rights Reserved        *
 *                                                                      *
 * This source and program are provided as part of the OZAmiga Cover    *
 * Disk and may not be used in any other publication or training course *
 * without the written permission of the Editor.                        *
 *                                                                      *
 ************************************************************************
 *                                                                      *
 * Function                                                             *
 * ========                                                             *
 *                                                                      *
 * This program demonstrates how to use the information supplied from   *
 * the Command Line. It is designed to run from the CLI.                *
 * Some of the statements in this progam have not been explained in the *
 * article, but will be explained in later articles. They are included  *
 * for you to experiment with.                                          *
 *                                                                      *
 ************************************************************************
 *                                                                      *
 * Source File:            FromCLI.c                                    *
 * Source Version:         1.0                                          *
 * Destination File:       FromCLI                                      *
 *                                                                      *
 * Source Computer:        Amiga 3000                                   *
 * Operating System:       AmigaDOS                                     *
 * O/S Version:            2.04                                         *
 * Compiler:               SAS C                                        *
 * Version:                6.0                                          *
 * Destination Computer:   Any Amiga                                    *
 *                                                                      *
 ************************************************************************
 *                                                                      *
 * Revision History                                                     *
 * ================                                                     *
 *                                                                      *
 * 11-Feb-93                                                            *
 * ---------                                                            *
 *                                                                      *
 *    Original Version                                                  *
 *                                                                      *
 ************************************************************************/

/************************************************************************
 *                         INCLUDE Files                                *
 ************************************************************************
 These files usually define external functions which will be called by the
 program. They can be functions supplied with the compiler, or they can
 be functions you have written. File names enclosed in angle brackets will
 be looked for along the search path, while file names enclosed in double
 quotes (") will be looked for in the current directory only.
 ************************************************************************/

#include    <stdio.h>

/************************************************************************
 *                         GLOBAL Variables                             *
 ************************************************************************
 Variables defined outside of the program modules can be accessed by all
 program modules in this file and by other modules which are linked to
 this file. Variables defined inside the opening braces of a program module
 are only accessable by that module.
 ************************************************************************/

/*======================================================================*
 *                         Force a Larger WorkBench Window              *
 *======================================================================*
 *** CAUTION *** This following statements are designed for the SAS C
 compiler version 6.0 and defines the type of window to be opened if this
 program is run from WorkBench instead of the CLI. To stop this from being
 compiled by other compilers, it is enclosed in a conditional clause.
 My compiler defines __SASC_60 which means this statement will be compiled
 into the program.
 You should also remember that some compilers may produce programs which
 may crash if they are run from the WorkBench when they should be run from
 the CLI - Rebuild this program and see what happens!
 *======================================================================*/

#ifdef __SASC_60
                  char        __stdiowin[] =
                                          "CON:0/0/640/200/Learning C - ";
#endif

/************************************************************************
 *                         MAIN PROGRAM                                 *
 ************************************************************************
 The module named "main" is the starting point of execution. Your compiler
 may open resources such as DOS, EXEC and other libraries before main is
 called. Consult your compiler documentation for details of what it can
 do. This program uses the argc and argv parameters which define the data
 which was input from the command line. The argc parameter holds the
 number of data items that are available, and the *argv[] points to the
 text on the line. To help you when you are writing programs such as a copy
 program, each word on the command line (or text enclosed by double quotes
 is put into a separate string. The "*" in front of the argv indicates that
 POINTERS are being used and the square brackets means that it is an ARRAY
 of POINTERS (This will be explained in the later articles). To allow you
 to get the complete command line, a pointer to the name of the program is
 also provided.
 ************************************************************************/

void main(int argc, char *argv[])
{

/*======================================================================*
 *                         Check if This was Started from Workbench     *
 *======================================================================*
 ** CAUTION ** This code is NOT ANSI standard and may not work with your
 compiler. To prevent any problems, it is enclosed in conditional clauses
 so that it will only be used with my compiler. You could remove the lines
 starting with "#" and see if it works with your compiler. There are two
 statements which will be explained in later articles:- "if" and "return".
 See if you can figure out what they are doing. P.S. You must use "=="
 when testing as will be explained later.
 *======================================================================*/

#ifdef __SASC_60

   if (argc == 0)             /* Set by SAS C to show a Workbench start */
   {
      printf("Please run this program from the CLI\n");
      printf("Usage: FromCLI YourName\n");
      return;
   }

#endif

/*======================================================================*
 *                         Was Your Name Given                          *
 *======================================================================*
 If you entered your name, the number in argc will be two. If it isn't
 two then let's remind you how to run this program. See how we test to see
 if it is not two.
 *======================================================================*/
 
   if (argc != 2)
   {
      printf("%s - Usage: %s YourName\n",argv[0],argv[0]);
      return;
   }

/*======================================================================*
 *                         Send a Message to the Console                *
 *======================================================================*
 Use the "printf" function defined in <stdio.h> to send text to the
 display. Rename this program and run it again - See what the difference
 is.
 *======================================================================*/

   printf("Hello %s, I'm glad you ran the '%s' program today\n",
          argv[1],argv[0]);
}
