Back to index

glibc  2.9
testopt.c
Go to the documentation of this file.
00001 /*@group*/
00002 #include <ctype.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <unistd.h>
00006 
00007 int
00008 main (int argc, char **argv)
00009 {
00010   int aflag = 0;
00011   int bflag = 0;
00012   char *cvalue = NULL;
00013   int index;
00014   int c;
00015 
00016   opterr = 0;
00017 /*@end group*/
00018 
00019 /*@group*/
00020   while ((c = getopt (argc, argv, "abc:")) != -1)
00021     switch (c)
00022       {
00023       case 'a':
00024         aflag = 1;
00025         break;
00026       case 'b':
00027         bflag = 1;
00028         break;
00029       case 'c':
00030         cvalue = optarg;
00031         break;
00032       case '?':
00033         if (optopt == 'c')
00034           fprintf (stderr, "Option -%c requires an argument.\n", optopt);
00035         else if (isprint (optopt))
00036           fprintf (stderr, "Unknown option `-%c'.\n", optopt);
00037         else
00038           fprintf (stderr,
00039                    "Unknown option character `\\x%x'.\n",
00040                    optopt);
00041         return 1;
00042       default:
00043         abort ();
00044       }
00045 /*@end group*/
00046 
00047 /*@group*/
00048   printf ("aflag = %d, bflag = %d, cvalue = %s\n",
00049          aflag, bflag, cvalue);
00050 
00051   for (index = optind; index < argc; index++)
00052     printf ("Non-option argument %s\n", argv[index]);
00053   return 0;
00054 }
00055 /*@end group*/