Repository navigation
Expand file tree
/
Copy pathprompt.c
More file actions
74 lines (62 loc) · 1.71 KB
/
Copy pathprompt.c
File metadata and controls
74 lines (62 loc) · 1.71 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
70
71
72
73
74
#include "headers.h"
char *getUsername()
{
struct passwd *pw;
uid_t uid = getuid();
pw = getpwuid(uid);
if (!pw)
{
printError("Cannot get username");
exit(EXIT_FAILURE);
}
return pw->pw_name;
}
void getHostname(char *hostname)
{
if (gethostname(hostname, DEF_SIZE))
{
printError("Cannot get hostname");
exit(EXIT_FAILURE);
}
}
void getCurrentDirectory(char *cwd)
{
if (!getcwd(cwd, DEF_SIZE))
{
printError("Cannot get current working directory");
}
}
void printPrompt(const char *username, const char *hostname, const char *cwd, int backgroundProcessStatus)
{
printf(BOLD_CYAN_COLOR);
// if cwd is @home
if (strcmp(cwd, global_home) == 0)
{
printf("<%s%s@%s%s:~%s", BOLD_YELLOW_COLOR, username, BOLD_MAGENTA_COLOR, hostname, RESET_COLOR);
}
// if cwd is @home/..
else if (strstr(cwd, global_home) == cwd)
{
printf("<%s%s@%s%s:~%s%s", BOLD_YELLOW_COLOR, username, BOLD_MAGENTA_COLOR, hostname, BOLD_GREY_COLOR, cwd + strlen(global_home));
}
// if cwd is not @home
else
{
printf("<%s%s@%s%s:%s%s", BOLD_YELLOW_COLOR, username, BOLD_MAGENTA_COLOR, hostname, BOLD_GREY_COLOR, cwd);
}
// Print background process status and time taken
if (backgroundProcessStatus > 0)
{
printf(BOLD_CYAN_COLOR " (Process %d)", backgroundProcessStatus);
}
printf(BOLD_CYAN_COLOR ">%s ", RESET_COLOR);
}
void prompt(int backgroundProcessStatus)
{
char cwd[DEF_SIZE];
getCurrentDirectory(cwd);
char *username = getUsername();
char hostname[DEF_SIZE];
getHostname(hostname);
printPrompt(username, hostname, cwd, backgroundProcessStatus);
}