-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.c
More file actions
78 lines (70 loc) · 1.95 KB
/
basic.c
File metadata and controls
78 lines (70 loc) · 1.95 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "header.h"
void get_val() // Get values of USER, SYSTEM and Root directory
{
// Might have to seperate to accomodate user change (Not sure)
system_name = malloc(100005*sizeof(char));
user_name = malloc(100005*sizeof(char));
if(system_name==NULL)
{
fprintf(stderr,"%s Error in assigning memory%s",RED,NORMAL);
exit(0);
}
if(user_name==NULL)
{
fprintf(stderr,"%s Error in assigning memory%s",RED,NORMAL);
exit(0);
}
gethostname(system_name,100005);
getlogin_r(user_name,100005);
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL)
{
perror("getcwd() error");
exit(0);
}
PREVDIR = malloc(100005*sizeof(char));
PRDIR = malloc(100005*sizeof(char));
INTDIR = malloc(100005*sizeof(char));
strcpy(INTDIR,cwd);
strcpy(PRDIR,"~");
OPRDIR = cwd;
strcpy(PREVDIR,"~");
// user_name = name of the user
// system_name = hostname
// INTDIR = Initial directory / Home directory of the shell
// PREVDIR = Previous directory, initially ~, for cd -
// PRDIR = Present directory in terms of ~
// OPRDIR = Original present directory, absolute
}
void prompt() // Print the prompt everytime
{
if(status==1)
{
printf("\n%s:') %s",GREEN,NORMAL);
}
else
{
printf("\n%s:'( %s",RED,NORMAL);
}
printf("%s%s@%s%s: %s%s%s $ ",CYAN,user_name,system_name,NORMAL,MAGENTA,PRDIR,NORMAL);
}
char* inc_tilda(char * address) // Adding ~ value to any loc (Changes absolute path to relative(~))
{
if(address[0]=='~')
{
char* new_addr;
new_addr = malloc(100000*sizeof(char*));
if(new_addr==NULL)
{
fprintf(stderr,"%s Error in assigning memory%s",RED,NORMAL);
exit(0);
}
strcpy(new_addr,INTDIR);
strcat(new_addr,address+1);
return new_addr;
}
else
{
return address;
}
}