-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
82 lines (73 loc) · 2.55 KB
/
main.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
#include "./Foxes-C/foxes.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define USAGE "Usage: %s [OPTION...] [TAG]\n\nOptions:\n -w <width>\t\tset the width of the image\n -h <height>\t\tset the height of the image\n -r <width:height>\tset the aspect ratio ex. \"1:2\"\n\nTAG:\n fox\n curious\n happy\n scarry\n sleeping\n"
int main(int argc, char *argv[]) {
int width = 0;
int height = 0;
char* aspectRatio = "";
char* tag = "";
int opt;
extern char *optarg;
extern int optind, optopt;
while ((opt = getopt(argc, argv, "w:h:r:")) != -1) {
switch (opt) {
case 'w': width = atoi(optarg); break;
case 'h': height = atoi(optarg); break;
case 'r': aspectRatio = optarg; break;
default:
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
for ( ; optind < argc; optind++) {
if (access(argv[optind], R_OK)) {
if (strcmp(tag, "") == 0 && (
strcmp(argv[optind], "fox") == 0 ||
strcmp(argv[optind], "curious") == 0 ||
strcmp(argv[optind], "happy") == 0 ||
strcmp(argv[optind], "scary") == 0 ||
strcmp(argv[optind], "sleeping") == 0)) {
tag = argv[optind];
}
else {
if (tag) {
fprintf(stderr, "%s: tag is already set, no need for %s\n", argv[0], argv[optind]);
}
else {
fprintf(stderr, "%s: invalid tag %s\n", argv[0], argv[optind]);
}
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
}
}
if (strcmp(tag, "") == 0) {
fprintf(stderr, "%s: no tag set\n", argv[0]);
fprintf(stderr, USAGE, argv[0]);
exit(EXIT_FAILURE);
}
foxes_options options = {};
if (width) options.width = width;
if (height) options.height = height;
if (strcmp(aspectRatio, "") != 0) options.aspectRatio = aspectRatio;
if (strcmp(tag, "fox") == 0) {
printf("%s\n", foxes_fox(options));
}
else if (strcmp(tag, "curious") == 0) {
printf("%s\n", foxes_curious(options));
}
else if (strcmp(tag, "happy") == 0) {
printf("%s\n", foxes_happy(options));
}
else if (strcmp(tag, "scary") == 0) {
printf("%s\n", foxes_scary(options));
}
else {
printf("%s\n", foxes_sleeping(options));
}
return 0;
}