-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
94 lines (81 loc) · 1.39 KB
/
init.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
85
86
87
88
89
90
91
92
93
94
#include "maindef.h"
// Function to reset variables
void clear_variables1()
{
input_buffer[0] = '\0';
no_cmds = 0;
cwd[0] = '\0';
}
// Function to reset variables
void clear_variables2()
{
no_args = 0;
execFlag = 0;
is_bg = 0;
}
// Function to generate relative path
void cust_pwd()
{
char *token1[512];
char *token2[512];
char temp[1024];
char pth[1024];
int cnt;
int no_toks1 = 0;
int no_toks2 = 0;
int i;
cnt = 1;
strcpy(temp, current_directory);
token1[0] = strtok(temp, "/");
no_toks1++;
while((token1[cnt] = strtok(NULL, "/")) != NULL)
{
cnt++;
no_toks1++;
}
token1[cnt] = NULL;
cnt = 1;
getcwd(pth, sizeof(pth));
strcpy(temp, pth);
token2[0] = strtok(temp, "/");
no_toks2++;
while((token2[cnt] = strtok(NULL, "/")) != NULL)
{
cnt++;
no_toks2++;
}
token2[cnt] = NULL;
if(no_toks1 == no_toks2)
{
strcpy(cwd, "~");
}
else
{
strcat(cwd, "~");
for(i = no_toks1; i < no_toks2; i++)
{
strcat(cwd, "/");
strcat(cwd, token2[i]);
}
}
}
void prompt()
{
char shell[1000];
char *buff;
char hostname[1000];
buff = (char *) malloc(10 * sizeof(char));
buff = getlogin();
gethostname(hostname, 1000);
// Printing the relative path of the file
cust_pwd();
// Printing the prompt
strcpy(shell, "<");
strcat(shell, buff);
strcat(shell, "@");
strcat(shell, hostname);
strcat(shell, ":");
strcat(shell, cwd);
strcat(shell, ">");
printf("%s", shell);
}