-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharmy.c
106 lines (92 loc) · 2.89 KB
/
army.c
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
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <getopt.h>
#include "army.h"
// Forward declarations
static int parse_args(int argc, char *argv[]);
static void print_usage();
int main(int argc, char *argv[])
{
int fileind = parse_args(argc, argv);
for (int i = fileind; i < argc; i++)
{
printf("File: %s\n", argv[i]);
}
return 0;
}
/**
* Parse a set of command line arguments for the program. Arguments
* that do not correspond to options/flags are placed at the
* end of argv by getopt_long. The index of the first non-option
* (i.e. a filename) argument is returned by this function so that
* the filenames can be obtained by the calling function.
*
* @param argc Number of args in argv
* @param argv Program arguments ([0] is the program name)
* @return Index of first file argument in argv
*/
static int parse_args(int argc, char *argv[])
{
static struct option long_options[] = {
{ "listing", no_argument, 0, 'l' },
{ "output", required_argument, 0, 'o' },
{ "version", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
int opt;
int long_index;
while (( opt = getopt_long(argc, argv, "lo:vh", long_options, &long_index) ) != -1)
{
switch (opt)
{
case 'l':
flag_listing = true;
break;
case 'o':
flag_output = optarg;
break;
case 'v':
printf(
"army v" ARMY_VERSION "\n"
"Copyright (c) 2016 daviga404\n"
"License GPLv3: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>\n"
"\n"
"This software is free software and was made for fun. You may\n"
"redistribute it under the terms of the GPLv3 license. This program\n"
"has absolutely no warranty.\n"
);
exit(EXIT_SUCCESS);
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
break;
default:
print_usage();
exit(EXIT_FAILURE);
}
}
if (optind >= argc)
{
printf("Error: no input files\n");
exit(EXIT_FAILURE);
}
return optind;
}
/**
* Print program usage
*/
static void print_usage()
{
printf(
"Usage: army [option...] <file>\n"
"Options:\n"
" -l,--listing Create a listing file\n"
" -o,--output [file] Output the object file to the given file instead.\n"
" Default file to output to is " DEFAULT_OUTPUT_FILE "\n"
" -v,--version Show program version info (must be only flag)\n"
" -h,--help Show this help message"
);
}