-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrompt.c
51 lines (42 loc) · 1.11 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
#include "myshell.h"
Prompt* createPrompt(){
Prompt* prompt = malloc(sizeof(Prompt));
initializePrompt(prompt);
return prompt;
}
void checkHostName(int hostName){
if(hostName == -1){
perror("Issue with retrieving hostname:");
exit(EXIT_FAILURE);
}
}
char* getMyHostName(){
char* buffer= malloc((sizeof(char))*BUFFERSIZE);
//retrives host id
int hostName = gethostname(buffer, sizeof(buffer));
checkHostName(hostName);
return buffer;
}
char* getCurrentWorkingDirectory(){
char* s = getenv("PWD");
if(s==NULL){
perror("Retreiving present working directory failed:");
exit(EXIT_FAILURE);
}
return s;
}
char* printPrompt(Prompt* prompt){
char* promptString = malloc(sizeof(char)*BUFFERSIZE);
sprintf(promptString, "%s@%s:%s$ ",prompt->username,prompt->hostname,prompt->pwd);
return promptString;
}
void initializePrompt(Prompt* prompt){
prompt->username = getlogin();
prompt->hostname = getMyHostName();
prompt->pwd = getCurrentWorkingDirectory();
}
void setCurrentWorkingDirectory(char* updatedPath){
prompt->pwd = malloc(PATH_MAX);
strcpy(prompt->pwd,updatedPath);
setenv("PWD",prompt->pwd,1);
}