tronayne |
04-01-2011 07:09 AM |
Here's a prototype C program. It doesn't actually do anything until you add code (it will compile and execute, though). Further down is how to use argument values.
Hope this helps some.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
int main (int argc, char *argv [])
{
int c; /* general-purpose */
int error = FALSE; /* error flag */
int vopt = FALSE; /* verbose option */
time_t t0 = (time_t) 0; /* start time */
time_t t1 = (time_t) 0; /* finish time */
FILE *in;
/* process the command line arguments */
while ((c = getopt (argc, argv, "?v")) != EOF) {
switch (c) {
case '?':
error = TRUE;
break;
case 'v':
vopt = TRUE;
break;
default:
(void) fprintf (stderr, "getopt() bug\n");
exit (EXIT_FAILURE);
}
}
/* any errors in the arguments, or a '?' entered...*/
if (error) {
(void) fprintf (stderr, "usage: %s [-v] argument...\n",
argv [0]);
exit (EXIT_FAILURE);
}
/* get a start time */
if (time (&t0) < (time_t) 0)
(void) fprintf (stderr,
"%s:\tcan't read system clock\n", argv [0]);
/* now process any arguments supplied... */
while (optind != argc) {
(void) fprintf (stderr, "Processing %s...\n", argv [optind]);
/* open the input file */
if ((in = fopen (argv [optind], "r")) == (FILE *) NULL) {
(void) fprintf (stderr,
"%s:\tcan't open %s\n",
argv [0], argv [optind]);
exit (EXIT_FAILURE);
}
/* close the input file */
if (fclose (in))
(void) fprintf (stderr,
"%s:\tcan't close %s\n",
argv [0], argv [optind]);
optind++;
}
/* get a finish time */
if (time (&t1) < (time_t) 0)
(void) fprintf (stderr,
"%s:\tcan't read system clock\n", argv [0]);
if (vopt)
(void) fprintf (stderr,
"%s duration %g seconds\n",
argv [0], difftime (t1, t0));
exit (EXIT_SUCCESS);
}
Here's a fragment showing how to use integer arguments
Code:
int main (int argc, char *argv [])
{
int c; /* general-purpose */
int error = FALSE; /* error flag */
int mm = 0, dd = 0, yy = 0; /* month, day, year */
int iopt = FALSE; /* interval option */
int vopt = FALSE; /* verbose option */
time_t t0 = (time_t) 0; /* today julian date */
time_t t1 = (time_t) 0; /* unknown julian date */
struct tm *tm; /* system time structure */
/* process the command line arguments */
while ((c = getopt (argc, argv, "?id:m:y:v")) != EOF) {
switch (c) {
case '?':
error = TRUE;
break;
case 'd':
dd = (int) strtol (optarg, (char **) NULL, 10);
break;
case 'i':
iopt = TRUE;
break;
case 'm':
mm = (int) strtol (optarg, (char **) NULL, 10);
break;
case 'y':
yy = (int) strtol (optarg, (char **) NULL, 10);
break;
case 'v':
vopt = TRUE;
break;
default:
(void) fprintf (stderr, "getopt() bug\n");
exit (EXIT_FAILURE);
}
}
/* any errors in the arguments, or a '?' entered...*/
And, when dealing with string argument, you need to copy them:
Code:
void main (int argc, char *argv [])
{
int err = FALSE;
int i, j;
int topt = FALSE; /* function type option */
int ropt = FALSE; /* return type option */
char rtyp [10]; /* return type description */
char ttyp [10]; /* function type description */
time_t now; /* for time and date */
struct tm *tm; /* system time */
/* process the command line arguments */
while ((i = getopt (argc, argv, "?r:t:")) != EOF) {
switch (i) {
case 'r':
ropt = TRUE;
(void) strcpy (rtyp, optarg);
break;
case 't':
topt = TRUE;
(void) strcpy (ttyp, optarg);
break;
case '?':
err = TRUE;
break;
default:
err = TRUE;
break;
}
}
Hope this helps some.
|