-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt.c
More file actions
59 lines (46 loc) · 1.18 KB
/
prompt.c
File metadata and controls
59 lines (46 loc) · 1.18 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include "styles.h"
char HOME[1000];
int bg_procs[1024];
char *bg_procs_name[1024];
void init() {
getcwd(HOME, sizeof(HOME));
int i = 0;
for(i=0; i<1024; ++i)
{
bg_procs[i] = -1;
bg_procs_name[i] = "Process";
}
return;
}
void display_prompt() {
// Get username
uid_t uid = getuid();
struct passwd *pw = getpwuid(uid);
char *user = pw->pw_name;
// Get system name
struct utsname sysinfo;
uname(&sysinfo);
char *sysname = sysinfo.nodename;
// Get current working dir
char cur_dir[1000];
getcwd(cur_dir, sizeof(cur_dir));
// Get current dir in relation to home
int home_len = strlen(HOME);
int i = 0;
for(i=0; i<home_len; ++i) if(HOME[i]!=cur_dir[i]) break;
// Print command prompt
printf(BOLD WHT "<" RESET);
printf(BOLD GRN "%s@%s" RESET, user, sysname);
printf(BOLD WHT ":" RESET);
if(i!=home_len) printf(BOLD CYN "%s" RESET, cur_dir);
else printf(BOLD CYN "~%s" RESET, &cur_dir[i]);
printf(BOLD WHT "> " RESET);
return;
}