-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
executable file
·62 lines (44 loc) · 1.43 KB
/
prompt.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
#include "headers.h"
#include "stringTokenize.h"
#include "history.h"
#include "prompt.h"
char **prompt(char *shell_root, int *errorCode) {
char hostname[path_len];
char username[path_len];
memset(hostname, 0, sizeof(hostname));
memset(username, 0, sizeof(username));
strcpy(username, getenv("USERNAME"));
gethostname(hostname, path_len);
char *inputBuffer = (char *) malloc(path_len);
char homeDir[path_len];
char cwd[path_len];
size_t bf_size = 1024;
memset(cwd, 0, sizeof(cwd));
memset(homeDir, 0, sizeof(homeDir));
getcwd(cwd, path_len);
bool tilda = true;
unsigned int len = strlen(shell_root);
if (strlen(cwd) >= len && strncmp(shell_root, cwd, len) == 0) {
strcpy(homeDir, cwd + len);
} else {
strcpy(homeDir, cwd);
tilda = false;
}
if (*errorCode == 1) printf(RED ":'( " RESET);
else if (*errorCode == 0) printf(GREEN ":') " RESET);
printf(MAGENTA "%s@%s" RESET, username, hostname);
printf(":");
tilda ? printf(CYAN "~%s> " RESET, homeDir) : printf(CYAN "%s> " RESET, homeDir);
*errorCode = 0;
if (getline(&inputBuffer, &bf_size, stdin) == -1) {
printf("\n");
free(inputBuffer);
exit(0);
}
char **commands = parseString(inputBuffer, CMD_DELIMITER);
if (commands[0] != NULL) {
add_to_history(inputBuffer, shell_root);
}
free(inputBuffer);
return commands;
}