-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.c
78 lines (61 loc) · 1.44 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "prompt.h"
#include "headers.h"
#include "sighandle.h"
#include "main.h"
#include "list.h"
void init()
{
printf("\e[1;1H\e[2J"); // for clearing screen using regex
printf("Welcome to Dayitva's shell!\n\n");
int i;
ssize_t len = readlink("/proc/self/exe", HOME, 1024);
for(i = len; HOME[i] != '/'; i--);
HOME[i] = 0;
procList = 0;
signal(SIGINT, ctrlC);
signal(SIGTSTP, ctrlZ);
signal(SIGCHLD, bgProcessExit);
}
char *tilda(char *cwd)
{
unsigned long lencwd = strlen(cwd), lenHome = strlen(HOME);
int offset = -1;
if(lencwd >= lenHome)
{
if(!strncmp(cwd, HOME, lenHome))
{
offset = lenHome;
}
}
char * dir = (char*) malloc (1024);
if(cwd[offset] && cwd[offset] != '/')
{
strcpy(dir, cwd);
return dir;
}
if(offset > 0)
{
dir[0] = '~';
strcpy(dir + 1, cwd + offset);
}
else
{
strcpy(dir, cwd);
}
return dir;
}
char *create_prompt()
{
char hostname[1024];
gethostname(hostname, 1024);
char *directory = (char *) malloc (1024);
directory = getcwd(directory, 1024);
directory = tilda(directory);
char *shellprompt = (char *) malloc (1024);
sprintf(shellprompt, COL_FG_GRN "<%s@%s:" COL_FG_BLU "%s" COL_FG_GRN "> " COL_RST, getlogin(), hostname, directory);
return shellprompt;
}
void ask_prompt()
{
printf("%s", create_prompt());
}