-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (44 loc) · 1.34 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
#include "shell.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern char** environ;
int main(int argc, char *argv[]) {
char *input;
size_t input_size = 0;
size_t input_length;
if (argc > 1) {
FILE *input_file = fopen(argv[1], "r");
if (input_file == NULL) {
perror("Error opening input file");
return EXIT_FAILURE;
}
input = NULL;
while (getline(&input, &input_size, input_file) != -1) {
input_length = strlen(input);
if (input_length > 0 && input[input_length - 1] == '\n') {
input[input_length - 1] = '\0';
}
printf("You entered: %s\n", input);
}
free(input);
fclose(input_file);
} else {
input = NULL;
while (1) {
printf("($) ");
getline(&input, &input_size, stdin);
input_length = strlen(input);
if (input_length > 0 && input[input_length - 1] == '\n') {
input[input_length - 1] = '\0';
}
if (strcmp(input, "exit") == 0) {
free(input);
exit(0);
}
printf("You entered: %s\n", input);
}
free(input);
}
return 0;
}