Listing 10 Template for a file printer program

/* pr.c: Skeleton of a file printer program */

# include <stdio.h>
#include <stdlib. h>
#include <ctype. h>

static int
    Copies = 1,  /* Default to one copy */
    Number = O;  /* Don't print with line numbers */
static char
    Queue = 'S'; /* Default to standard printer */

static void process(char *);

main(int argc, char *argv[])
{
    int i;
    char *s;

    /* Process each argument immediately */
    for (i = 1; i < argc; ++i)
    {
       if (argv[i][O] == '-')
          for (s = argv[i]+l; *s; ++s)
             switch (toupper (*s))
             {
             case 'C':   /* Build number for copies */
                if (isdigit (s[1]))
                   for (Copies = O; isdigit(s[1]); ++s)
                       Copies = Copies*10 + (s[1] - '0');
                break;
             case 'N':   /* Toggle line numbering */
                Number = !Number;
                break;
             case 'Q':   /* Select print queue */
                ++s;
                Queue = toupper(*s);
                break;
             default:
                fprintf(stderr,"pr: Bad option: -%c\n",*s);
                return EXIT_FAILURE;
             }

       else
          process (argv [i] );
    }
    return EXIT_SUCCESS;
}

static void process(char *s)
{
    printf ("Processing %s... \n" ,s);
    printf("\tCopies: %d, Number: %d, Queue: %c\n",
      Copies,Number,Queue);
}

/* The output after executing the command line above is

Processing file1.c...
       Copies: 10, Number: 1, Queue: S
Processing file2.c...
       Copies: 1, Number: O, Queue: L
Processing file3.c...
       Copies: 1, Number: O, Queue: L

*/

/* End of File */