-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
69 lines (56 loc) · 1.28 KB
/
main.c
File metadata and controls
69 lines (56 loc) · 1.28 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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include "prompt.h"
#include "utils.h"
#include "builtins.h"
#include "argslinkedl.h"
#define HOSTNAMESIZE 64
#define PATHNAMESIZE 4096
#define INPUTSIZE 500
int main()
{
char hostname[HOSTNAMESIZE];
char *username;
char cwd[PATHNAMESIZE];
char input[INPUTSIZE];
char *trimmedInput;
char *command;
struct arg *arguments;
char *returnVal;
gethostname(hostname, HOSTNAMESIZE);
username = getlogin();
if (!username) {
fprintf(stderr, "Error: unable to fetch username");
return 1;
}
getcwd(cwd, PATHNAMESIZE);
struct promptInfo prompt = {hostname, username, cwd};
do {
getcwd(cwd, PATHNAMESIZE);
printf("%s@%s:[%s]$ ", username, hostname, cwd);
returnVal = fgets(input, INPUTSIZE, stdin);
if (!input)
return 0;
if (!strchr(input, '\n')) {
printf("\n");
continue;
}
input[strcspn(input, "\n")] = '\0';
trimmedInput = trimWhitespace(input);
if (trimmedInput[0] == '\0')
continue;
arguments = strsplit(input);
command = arguments->cmd;
if (redirectOutput(arguments) != -1) {
cleanArgs(&arguments);
if (arguments != NULL)
exeCmd(arguments);
}
resetDescriptorTable();
freeList(&arguments);
} while(returnVal);
return 0;
}