File: getopt_ex.c $ getopt_ex -db -f abc -c -g hij -d file1 file2 1 argc equals 10 2 getopt_ex: illegal option -- b 3 invalid option is b 4 getopt_ex: illegal option -- c 5 invalid option is c 6 dflg equals 2 7 f_ptr points to abc 8 g_ptr points to hij 9 invalid equals 2 10 optind equals 8 11 next parameter = file1 1 #include <stdlib.h> 2 #include <stdio.h> 3 4 main(int argc, char *argv[]) 5 { 6 char options[ ] = "f:dg:"; /* valid options */ 7 int c, invalid = 0, dflg = 0, fflg = 0, gflg = 0; 8 char *f_ptr, *g_ptr; 9 10 printf("argc equals %d\n", argc); 11 while ((c = getopt(argc, argv, options)) != EOF) { 12 switch (c) { 13 case 'd': 14 dflg++; 15 break; 16 case 'f': 17 fflg++; 18 f_ptr = optarg; 19 break; 20 case 'g': 21 gflg++; 22 g_ptr = optarg; 23 break; 24 case '?': 25 printf("invalid option is %c\n", optopt); 26 invalid++; 27 } 28 } 29 printf("dflg equals %d\n", dflg); 30 if(fflg) 31 printf("f_ptr points to %s\n", f_ptr); 32 if(gflg) 33 printf("g_ptr points to %s\n", g_ptr); 34 printf("invalid equals %d\n", invalid); 35 printf("optind equals %d\n", optind); 36 if(optind < argc) 37 printf("next parameter = %s\n", argv[optind]); 38 }