2 * Copyright 2005 Timo Hirvonen
13 char *program_name
= NULL
;
15 void warn(const char *format
, ...)
19 fprintf(stderr
, "%s: ", program_name
);
21 vfprintf(stderr
, format
, ap
);
25 void warn_errno(const char *format
, ...)
30 fprintf(stderr
, "%s: ", program_name
);
32 vfprintf(stderr
, format
, ap
);
34 fprintf(stderr
, ": %s\n", strerror(e
));
37 void __NORETURN
die(const char *format
, ...)
41 fprintf(stderr
, "%s: ", program_name
);
43 vfprintf(stderr
, format
, ap
);
48 void __NORETURN
die_errno(const char *format
, ...)
53 fprintf(stderr
, "%s: ", program_name
);
55 vfprintf(stderr
, format
, ap
);
57 fprintf(stderr
, ": %s\n", strerror(e
));
61 static int short_option(int ch
, const struct option
*options
)
66 if (!options
[i
].short_opt
) {
67 if (!options
[i
].long_opt
)
68 die("unrecognized option `-%c'\n", ch
);
71 if (options
[i
].short_opt
!= ch
)
77 static int long_option(const char *opt
, const struct option
*options
)
84 for (i
= 0; options
[i
].short_opt
|| options
[i
].long_opt
; i
++) {
85 if (options
[i
].long_opt
&& strncmp(opt
, options
[i
].long_opt
, len
) == 0) {
88 if (options
[i
].long_opt
[len
] == 0) {
96 die("option `--%s' is ambiguous\n", opt
);
98 die("unrecognized option `--%s'\n", opt
);
102 int get_option(char **argvp
[], const struct option
*options
, char **arg
)
104 char **argv
= *argvp
;
105 const char *opt
= *argv
;
109 if (opt
== NULL
|| opt
[0] != '-' || opt
[1] == 0)
114 /* '--' => no more options */
118 i
= long_option(opt
+ 2, options
);
122 i
= short_option(opt
[1], options
);
125 if (options
[i
].has_arg
) {
127 die("option `%s' requires an argument\n", opt
);