-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.c
72 lines (52 loc) · 1.95 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
#include "headers.h"
void prompt(char* directory,int* flag,char* name,int time){
// Do not hardcode the prmopt
char* sysname=(char*)malloc(sizeof(char)*LEN);
char* usname=(char*)malloc(sizeof(char)*LEN);
char* directory_new=(char*)malloc(sizeof(char)*LEN);
char* temp=(char*)malloc(sizeof(char)*LEN);
int t=gethostname(sysname,LEN);
int p=getlogin_r(usname,LEN);
directory_new=getcwd(directory_new,LEN);
if(strcmp(directory,directory_new)==0){
temp="~";
}
else if(strstr(directory_new,directory)!=NULL){
char temporary_directory[strlen(directory_new)+1];
for(int i=0;i<strlen(directory_new);i++){
temporary_directory[i]='\0';
}
int position_of_home=0;
for(int i=0;i<strlen(directory_new);i++){
temporary_directory[i]=directory_new[i];
if(strcmp(temporary_directory,directory)==0){
position_of_home=i+1;
break;
}
}
char relative_string[strlen(directory_new)];
for(int i=0;i<strlen(directory_new);i++){
relative_string[i]='\0';
}
for(int i=position_of_home;i<strlen(directory_new);i++){
relative_string[i-position_of_home]=directory_new[i];
}
strcpy(temp,"~");
strcat(temp,relative_string);
// printf("%s\n",temp);
}
else strcpy(temp,directory_new);
// printf("Aaj kamayinga to kal baithkar khayenga\n");
printf(MAG"<"reset);
printf(ANSI_COLOR_GREEN "%s" ANSI_COLOR_RESET,usname );
printf(ANSI_COLOR_RED "@" ANSI_COLOR_RESET);
printf(ANSI_COLOR_BLUE "%s" ANSI_COLOR_RESET,sysname);
printf(":");
printf(ANSI_COLOR_RED "%s" ANSI_COLOR_RESET,temp);
if((*flag)==0){
printf(ANSI_COLOR_GREEN ">" ANSI_COLOR_RESET);}
else{
printf("%s : %d s>",name,time);
(*flag)=0;
}
}