add -Wpedantic to default CFLAGS
[baseutils.git] / getopt.c
1 /*
2 * This material, written by Henry Spencer, was released by him
3 * into the public domain and is thus not subject to any copyright.
4 */
5
6 #define _XOPEN_SOURCE 700
7
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 int
14 main(int argc, char *argv[])
15 {
16 int c;
17 int status = 0;
18
19 if (argc < 2) {
20 fprintf(stderr, "%s: no arguments given\n", argv[0]);
21 exit(2);
22 }
23
24 optind = 2; /* Past the program name and the option letters. */
25 while ((c = getopt(argc, argv, argv[1])) != -1) {
26 switch (c) {
27 case '?':
28 status = 1; /* getopt routine gave message */
29 break;
30 default:
31 if (optarg != NULL)
32 printf(" -%c %s", c, optarg);
33 else
34 printf(" -%c", c);
35 break;
36 }
37 }
38
39 printf(" --");
40 for (; optind < argc; optind++)
41 printf(" %s", argv[optind]);
42 printf("\n");
43
44 return status;
45 }