-
Notifications
You must be signed in to change notification settings - Fork 0
/
userInterface.c
85 lines (70 loc) · 2.39 KB
/
userInterface.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "userInterface.h"
#include "sniffer.h"
void printWelcomeMessage() {
printf("Welcome to a simple CLI packet analyser based on the C/C++ library libpcap\n");
}
void doMainMenu(int argc, char *argv[]) {
int choice = 0;
while(choice != 4) {
printf("\nPlease type the number of your choice:\n");
printf("1. Print packet details to the console\n");
printf("2. Print packet details to a file\n");
printf("3. Do both of the above\n");
printf("4. Exit the application\n\n");
/*
Find a way to avoid a data type other than an integer being passed into choice from the user (via scanf here).
Whenever a value of a different type is entered by the user here, the program automatically executes this while loop
seemingly ad infinitum, due to a segmentation fault (in short, the program breaks/crashes ungracefully)
*/
scanf("%d", &choice);
switch(choice) {
case 1:
sniffToCli(argc, argv);
break;
case 2:
doFileMenu(argc, argv);
break;
case 3:
doFileMenu(argc, argv);
sniffToCli(argc, argv);
break;
case 4:
printf("\nBye!\n\n");
exit(0);
default:
fprintf(stderr, "\nError: Invalid option\n");
break;
}
}
}
void doFileMenu(int argc, char *argv[]) {
int subChoice = 0;
char buf[256];
char *filePath;
printf("\nPlease type the number of your choice:\n");
printf("1. Enter file path\n");
printf("2. Main menu\n\n");
/*
Find a way to avoid a data type other than an integer being passed into subchoice from the user (via scanfs here).
Same problem as in doMainMenu.
*/
scanf("%d", &subChoice);
switch(subChoice) {
case 1:
printf("\nPlease write the path of the file you wish to print to: ");
scanf("%s", &buf); // Try to understand why a compiler warning is triggered here aswell
filePath = strcpy(filePath, buf);
sniffToFile(argc, argv, filePath);
break;
case 2:
doMainMenu(argc, argv);
break;
default:
fprintf(stderr, "Error: Invalid option");
doFileMenu(argc, argv);
break;
}
}