-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.c
73 lines (63 loc) · 1.32 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
#include "headers.h"
#include "prompt.h"
char home_dir[1024];
char* get_homedir()
{
//Assuming that the max length of the home dir = 1024
getcwd(home_dir, 1024);
return home_dir;
}
void prompt()
{
//Assuming that the max length of the HOSTNAME and USERNAME dir = 1024
char HOSTNAME[1024], USERNAME[1024];
gethostname(HOSTNAME, sizeof(HOSTNAME));
getlogin_r(USERNAME, sizeof(USERNAME));
color_reset();
//printing USERNAME and HOSTNAME in boldgreen colour
bold_green();
printf("<%s@", USERNAME);
printf("%s:", HOSTNAME);
color_reset();
//Finding cur dir in each prompt and comparing it with home directory
char curr_dir[1024];
getcwd(curr_dir, 1024);
bold_cyan();
int cmp = strcmp(home_dir, curr_dir);
//printf("strcmp = %d", cmp);
//cur_dir == home_dir, print ~
if (cmp == 0)
{
printf("~>");
}
//home_dir>cur_dir in length, print complete path
else if (cmp > 0)
{
printf(" %s>", curr_dir);
}
//home_dir<Cur_dir, so print~ and then further path using while loop
else if (cmp < 0)
{
printf("~");
int i = strlen(home_dir);
while (curr_dir[i] != '\0')
{
printf("%c", curr_dir[i]);
i++;
}
printf(">");
}
color_reset();
}
void bold_green () {
printf("\033[1;32m");
}
void bold_cyan() {
printf("\033[1;36m");
}
void color_reset () {
printf("\033[0m");
}
void red () {
printf("\033[0;31m");
}