Debian Codegraph @

listings/chapter-2/getopt_long.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <getopt.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 /* The name of this program. */
12 const char* program_name;
13
14 /* Prints usage information for this program to STREAM (typically
15 stdout or stderr), and exit the program with EXIT_CODE. Does not
16 return. */
17
18 void print_usage (FILE* stream, int exit_code)
19 {
20 fprintf (stream, "Usage: %s options [ inputfile ... ]\n", program_name);
21 fprintf (stream,
22 " -h --help Display this usage information.\n"
23 " -o --output filename Write output to file.\n"
24 " -v --verbose Print verbose messages.\n");
25 exit (exit_code);
26 }
27
28 /* Main program entry point. ARGC conains number of argument list
29 elements; ARGV is an array of pointers to them. */
30
31 int main (int argc, char* argv[])
32 {
33 int next_option;
34
35 /* A string listing valid short options letters. */
36 const char* const short_options = "ho:v";
37 /* An array describing valid long options. */
38 const struct option long_options[] = {
39 { "help", 0, NULL, 'h' },
40 { "output", 1, NULL, 'o' },
41 { "verbose", 0, NULL, 'v' },
42 { NULL, 0, NULL, 0 } /* Required at end of array. */
43 };
44
45 /* The name of the file to receive program output, or NULL for
46 standard output. */
47 const char* output_filename = NULL;
48 /* Whether to display verbose messages. */
49 int verbose = 0;
50
51 /* Remember the name of the program, to incorporate in messages.
52 The name is stored in argv[0]. */
53 program_name = argv[0];
54
55 do {
56 next_option = getopt_long (argc, argv, short_options,
57 long_options, NULL);
58 switch (next_option)
59 {
60 case 'h': /* -h or --help */
61 /* User has requested usage information. Print it to standard
62 output, and exit with exit code zero (normal termination). */
63 print_usage (stdout, 0);
64
65 case 'o': /* -o or --output */
66 /* This option takes an argument, the name of the output file. */
67 output_filename = optarg;
68 break;
69
70 case 'v': /* -v or --verbose */
71 verbose = 1;
72 break;
73
74 case '?': /* The user specified an invalid option. */
75 /* Print usage information to standard error, and exit with exit
76 code one (indicating abonormal termination). */
77 print_usage (stderr, 1);
78
79 case -1: /* Done with options. */
80 break;
81
82 default: /* Something else: unexpected. */
83 abort ();
84 }
85 }
86 while (next_option != -1);
87
88 /* Done with options. OPTIND points to first non-option argument.
89 For demonstration purposes, print them if the verbose option was
90 specified. */
91 if (verbose) {
92 int i;
93 for (i = optind; i < argc; ++i)
94 printf ("Argument: %s\n", argv[i]);
95 }
96
97 /* The main program goes here. */
98
99 return 0;
100 }