-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractCommandOptions.c
More file actions
93 lines (87 loc) · 3.4 KB
/
extractCommandOptions.c
File metadata and controls
93 lines (87 loc) · 3.4 KB
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
#include "extractCommandOptions.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
extern int *optarg;
Command *extractCommandOptions(int argc, char *argv[]){
Command *command = (Command *)malloc(sizeof(Command));
if(command == NULL){
fprintf(stderr, "extractCommandOptions - Error alocating memory.\n");
exit(EXIT_FAILURE);
}
command->background = 0;
command->port = 0;
command->logFilename = NULL;
command->statsFilename = NULL;
command->rootDir = NULL;
if(argc < 2){
printf("extractCommandOptions - Error number of arguments\n");
printf("Format: %s -p <port> -l <logfile> -s <statsfile> -r <rootdir> -b\n", argv[0]);
free(command);
exit(EXIT_FAILURE);
}
struct option long_options[] = {
{"port", required_argument, 0, 'p'},
{"log", required_argument, 0, 'l'},
{"statistics", required_argument, 0, 's'},
{"background", no_argument, 0, 'b'},
{"root", required_argument, 0, 'r'},
{0, 0, 0, 0}
};
int opt;
int option_index = 0;
while((opt = getopt_long(argc, argv, "p:l:s:r:b", long_options, &option_index)) != -1){
switch (opt){
case 'p':
command->port = atoi(optarg);
break;
case 'l':
printf("extractCommandOptions - Log file name is %s\n", optarg);
command->logFilename = (char *)malloc(strlen(optarg) + 1);
if(command->logFilename == NULL){
fprintf(stderr, "extractCommandOptions - Error allocating memory for logFilename.\n");
free(command);
exit(EXIT_FAILURE);
}
strcpy(command->logFilename, optarg);
break;
case 's':
printf("extractCommandOptions - Stats file name is %s\n", optarg);
command->statsFilename = (char *)malloc(strlen(optarg) + 1);
if(command->statsFilename == NULL){
fprintf(stderr, "extractCommandOptions - Error allocating memory for statsFilename.\n");
free(command->logFilename);
free(command);
exit(EXIT_FAILURE);
}
strcpy(command->statsFilename, optarg);
break;
case 'r':
printf("extractCommandOptions - Root directory is %s\n", optarg);
command->rootDir = (char *)malloc(strlen(optarg) + 1);
if(command->rootDir == NULL){
fprintf(stderr, "extractCommandOptions - Error allocating memory for rootDir.\n");
free(command->logFilename);
free(command->statsFilename);
free(command);
exit(EXIT_FAILURE);
}
strcpy(command->rootDir, optarg);
break;
case 'b':
printf("extractCommandOptions - Running with background flag == 1\n");
command->background = 1;
break;
default:
fprintf(stderr, "Unknown command\n");
free(command->logFilename);
free(command->statsFilename);
free(command->rootDir);
free(command);
exit(EXIT_FAILURE);
}
}
return command;
}