File: getopt.c

package info (click to toggle)
s390-tools 2.35.0-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 12,248 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (100 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * cpuplugd - Linux for System z Hotplug Daemon
 *
 * Command line parsing
 *
 * Copyright IBM Corp. 2007, 2017
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include <getopt.h>
#include "cpuplugd.h"

void print_usage(int is_error, char program_name[]);
void print_version();
int foreground;
int debug;
char *configfile;
int cpu_idle_limit;

void parse_options(int argc, char **argv)
{
	int config_file_specified = -1;
	const struct option long_options[] = {
		{ "help", no_argument,       NULL, 'h'},
		{ "foreground", no_argument, NULL, 'f' },
		{ "config", required_argument, NULL, 'c' },
		{ "version", no_argument, NULL, 'v' },
		{ "verbose", no_argument, NULL, 'V'   },
		{ NULL, 0, NULL, 0}
	};

	/* dont run without any argument */
	if (argc == 0 || argc == 1)
		print_usage(0, argv[0]);
	while (optind < argc) {
		int index = -1;
		struct option *opt = 0;
		int result = getopt_long(argc, argv, "hfc:vVm",
			long_options, &index);
		if (result == -1)
			break;		/* end of list */
		switch (result) {
		case 'h':
			print_usage(0, argv[0]);
			break;
		case 'f':
			foreground = 1;
			break;
		case 'c':
			/*
			 * This prevents -cbla and enforces the
			 * user to specify -c bla
			 */
			if (strcmp(argv[optind-1], optarg) == 0) {
				configfile = optarg;
				config_file_specified = 1;
			} else {
				cpuplugd_error("Unrecognized option: %s\n",
					       optarg);
				exit(1);
			}
			break;
		case 'v':
			print_version();
			break;
		case 'V':
			debug = 1;
			break;
		case 0:
			/* all parameter that do not appear in the optstring */
			opt = (struct option *)&(long_options[index]);
			printf("'%s' was specified.",
			       opt->name);
			if (opt->has_arg == required_argument)
				printf("Arg: <%s>", optarg);
			printf("\n");
			break;
		case '?':
			printf("Try '%s' --help' for more information.\n",
				argv[0]);
			exit(1);
			break;
		case -1:
			/*
			 * We also run in this case if no argument was
			 * specified
			 */
			break;
		default:
			print_usage(0, argv[0]);
		}
	}
	if (config_file_specified == -1) {
		printf("You have to specify a configuration file!\n");
		printf("Try '%s' --help' for more information.\n", argv[0]);
		exit(1);
	}
}