-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_var.c
72 lines (68 loc) · 1.34 KB
/
env_var.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
#include "main.h"
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<errno.h>
#include<string.h>
int set_pwd() {
if ((int)strlen(PWD) != 0)
strcpy(OLDPWD, PWD);
if (getcwd(PWD, PATH_MAX) == NULL) {
// perror("setting pwd error");
perror("sash");
// printf("setting pwd errord");
// print_errno();
return 1;
}
}
int set_home() {
if (getcwd(HOME, PATH_MAX) == NULL) {
perror("sash");
// printf("setting pwd errord");
print_errno();
return 1;
}
}
int set_rwd() {
int n = strlen(HOME);
int m = strlen(PWD);
// printf("%d %d", n, m);
if (n > m) {
strcpy(RWD, PWD);
// printf("fgdgdfhgfdjnbj");
return 0;
}
int i;
for(i=0;i<n;i++)
{
if (PWD[i] != HOME[i]) break;
}
if (i >= n)
{
if (n == m)
{
strcpy(RWD,"~");
return 0;
}
if (n < m)
{
if (PWD[i] == '/')
{
RWD[0] = '~';
strcpy(&RWD[1], &PWD[i]);
return 0;
}
else {
strcpy(RWD, PWD);
return 0;
}
}
}
else {
strcpy(RWD, PWD);
return 0;
}
}