Import version 1.8.3
[s390-tools.git] / cpuplugd / getopt.c
blob86cec2145efff44d7d221802ab8888237939761d
1 /*
2 * Copyright IBM Corp 2007
3 * Author: Hans-Joachim Picht <hans@linux.vnet.ibm.com>
5 * Linux for System z Hotplug Daemon
7 * This file is used to parse the command line arguments
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <signal.h>
29 #include <syslog.h>
30 #include <limits.h>
31 #include "cpuplugd.h"
33 void print_usage(int is_error, char program_name[]);
34 void print_version();
35 int foreground;
36 int debug;
37 char *configfile;
38 int cpu_idle_limit;
40 void parse_options(int argc, char **argv)
42 int config_file_specified = -1;
43 const struct option long_options[] = {
44 { "help", no_argument, NULL, 'h'},
45 { "foreground", no_argument, NULL, 'f' },
46 { "config", required_argument, NULL, 'c' },
47 { "version", no_argument, NULL, 'v' },
48 { "verbose", no_argument, NULL, 'V' },
49 { NULL, 0, NULL, 0}
52 /* dont run without any argument */
53 if (argc == 0 || argc == 1)
54 print_usage(0, argv[0]);
55 while (optind < argc) {
56 int index = -1;
57 struct option *opt = 0;
58 int result = getopt_long(argc, argv, "hfc:vVm",
59 long_options, &index);
60 if (result == -1)
61 break; /* end of list */
62 switch (result) {
63 case 'h':
64 print_usage(0, argv[0]);
65 break;
66 case 'f':
67 foreground = 1;
68 break;
69 case 'c':
71 * this prevents -cbla and enforces the
72 * user to specify -c bla
74 if (strcmp(argv[optind-1], optarg) == 0) {
75 configfile = optarg;
76 config_file_specified = 1;
77 } else {
78 printf("Unrecognized option: %s\n", optarg);
79 exit(1);
81 break;
82 case 'v':
83 print_version();
84 break;
85 case 'V':
86 debug = 1;
87 break;
88 case 0:
89 /* all parameter that do not
90 appear in the optstring */
91 opt = (struct option *)&(long_options[index]);
92 printf("'%s' was specified.",
93 opt->name);
94 if (opt->has_arg == required_argument)
95 printf("Arg: <%s>", optarg);
96 printf("\n");
97 break;
98 case '?':
99 printf("Try '%s' --help' for more information.\n",
100 argv[0]);
101 exit(1);
102 break;
103 case -1:
104 /* we also run in this case if no argument was s
105 * pecified */
106 break;
107 default:
108 print_usage(0, argv[0]);
111 if (config_file_specified == -1) {
112 printf("You have to specify a configuration file!\n");
113 printf("Try '%s' --help' for more information.\n", argv[0]);
114 exit(1);