forked from DC-SWAT/DreamShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchdir.c
More file actions
136 lines (98 loc) · 2.94 KB
/
Copy pathchdir.c
File metadata and controls
136 lines (98 loc) · 2.94 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* DreamShell ##version##
* chdir.c
* (c) 2004-2014 SWAT
*/
#include "ds.h"
/* read a pathname based on the current directory and turn it into an abs one, we
don't check for validity, or really do anything except handle ..'s and .'s */
int makeabspath_wd(char *buff, char *path, char *dir, size_t size) {
int numtxts;
int i;
char *txts[32]; /* max of 32...should be nuff */
char *rslash;
numtxts = 0;
/* check if path is already absolute */
if (path[0] == '/') {
strncpy(buff, path, size);
return 0;
}
/* split the path into tokens */
for (numtxts = 0; numtxts < 32;) {
if ((txts[numtxts] = strsep(&path, "/")) == NULL)
break;
if (*txts[numtxts] != '\0')
numtxts++;
}
/* start from the current directory */
strncpy(buff, dir, size);
for (i = 0; i < numtxts; i++) {
if (strcmp(txts[i], "..") == 0) {
if ((rslash = strrchr(buff, '/')) != NULL)
*rslash = '\0';
} else if (strcmp(txts[i], ".") == 0) {
/* do nothing */
} else {
if (buff[strlen(buff) - 1] != '/')
strncat(buff, "/", size - 1 - strlen(buff));
strncat(buff, txts[i], size - 1 - strlen(buff));
}
}
/* make sure it's not empty */
if (buff[0] == '\0') {
buff[0] = '/';
buff[1] = '\0';
}
return 0;
}
// Deprecated
int makeabspath(char *buff, char *path, size_t size) {
//return makeabspath_wd(buff, path, cwd, size);
realpath(path, buff);
return 1;
}
const char *relativeFilePath(const char *rel, const char *file) {
if(file[0] == '/') return file;
char *rslash, *dir, *ret;
char fn[MAX_FN_LEN];
char buff[MAX_FN_LEN];
if((rslash = strrchr(rel, '/')) != NULL) {
strncpy(buff, file, MAX_FN_LEN);
dir = substring(rel, 0, strlen(rel) - strlen(rslash));
makeabspath_wd(fn, buff, dir, MAX_FN_LEN);
fn[strlen(fn)] = '\0';
ret = strdup(fn);
//ds_printf("Directory: '%s' File: '%s' Out: '%s'", dir, rslash, ret, fn);
free(dir);
} else {
return file;
}
return ret;
}
int relativeFilePath_wb(char *buff, const char *rel, const char *file) {
if(file[0] == '/') {
strncpy(buff, file, MAX_FN_LEN);
return 1;
}
/*
char *rslash, *dir;
if((rslash = strrchr(rel, '/')) != NULL) {
dir = substring(rel, 0, strlen(rel) - strlen(rslash));
makeabspath_wd(buff, file, dir, MAX_FN_LEN);
ds_printf("Directory: '%s' File: '%s' Out: '%s'", dir, rel, buff);
*/
makeabspath_wd(buff, (char*)file, getFilePath(rel), MAX_FN_LEN);
//ds_printf("Directory: '%s' File: '%s' Out: '%s'", path, rel, buff);
return 1;
}
char *getFilePath(const char *file) {
char *rslash;
if((rslash = strrchr(file, '/')) != NULL) {
return substring(file, 0, strlen(file) - strlen(rslash));
}
return NULL;
}
/* change the current directory (dir is an absolute path for now) */
// Deprecated
int ds_chdir(char *dir) {
return fs_chdir(dir);
}