-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_prompt.c
85 lines (74 loc) · 1.87 KB
/
get_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
79
80
81
82
83
84
#include "headers.h"
// this file is used to get the username and the systemname
// the given functions return char pointers that lead to te appropriate name
char* find_username() {
// this prints the username
char *buf;
buf= (char *)malloc(100*sizeof(char));
buf= getlogin();
printf("%s",buf);
// free(buf);
return buf;
}
// this printst he systemname
int find_systemname(){
struct utsname buffer;
// char *buf;
// buf= (char *)malloc(100*sizeof(char));
// gethostname(buf,100);
int k = uname(&buffer);
// gethostname(buf, sizeof(buf));
// buf = buffer.sysname;
// printf("%s",buf);
printf("%s",buffer.nodename);
return 1;
}
// this implements pwd command
int print_pwd(char* starting_working_directory){
char cwd[1024];
// gets the current directory
char* bruh = getcwd(cwd, sizeof(cwd));
if(bruh==NULL){
perror("pwd");
return 1;
}
// gets the sizes
int size1 = strlen(cwd);
int size2 = strlen(starting_working_directory);
if(size1<size2){
printf(" %s",cwd);
}
else{
int contains =1;
for(int i=0;i<size2;i++){
if(cwd[i]!=starting_working_directory[i]){
contains = 0;
break;
}
}
// if the cwd contains ~
if(contains){
if(size1==size2){
printf("~");
}
else{
printf(" ~%s",cwd+size2);
}
}
else{
printf(" %s",cwd);
}
}
return 0;
}
// this uses the above functions to print the prompt
void print_prompt(char* starting_working_directory){
printf("<");
find_username();
printf("@");
find_systemname();
printf(":");
print_pwd(starting_working_directory);
// printf(" %s",starting_working_directory);
printf(">\n");
}