-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (157 loc) · 3.73 KB
/
main.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void * memcopy(char * dest, const char * src, size_t n){
/* char * cdest = (char *)dest; */
/* char * csrc = (char *)src; */
while (n-- && (*dest++ = *src++)) ;
return dest;
}
char * strcopy(char * dst, const char * src, size_t n){
while (--n && (*dst++ = *src++))
;
if (!n) {
*dst++ = '\0';
return *src ? NULL : dst;
} else {
return dst;
}
}
// usually faster than realpath, except with epically gigantic pathnames approaching the 4096 char limit on Linux.
char* fullpath(char* filename, char* cwd){
int bufferlen;
int filelen;
int cwdlen;
char* buffer;
cwdlen = strlen(cwd);
filelen = strlen(filename);
bufferlen = cwdlen + filelen + 1;
if (filename[0] != '/'){
// combine cwd with relative path
bufferlen += 1;
buffer = malloc(bufferlen + 1);
if (cwd[1] == '\0') {
buffer[0] = '/';
memcpy(buffer+1, filename, filelen);
/* strcpy(buffer+1, filename); */
} else {
memcpy(buffer, cwd, cwdlen);
/* strcpy(buffer, cwd); */
buffer[cwdlen] = '/';
memcpy(buffer+(cwdlen+1), filename, filelen);
/* strcpy(buffer+(cwdlen+1), filename); */
}
// printf("concatted: %s\n", buffer);
} else {
buffer = malloc(bufferlen + 1);
strcpy(buffer, filename);
}
// look for '.' and '/' chars and manipulate
char c;
for (int i = 0; i < bufferlen; i++){
// printf("%c\n", buffer[i]);
// remove duplicate '/'
if (buffer[i] == '/' && buffer[i+1] == '/'){
// printf("removing 1 slash.\n");
bufferlen -= 1;
for (int ii=i+1; ii <= bufferlen; ii++){
// printf("hi\n");
buffer[ii] = buffer[ii+1];
}
i--;
continue;
}
if (buffer[i] == '/'){
i++;
// resolve ".." directories
if (buffer[i] == '.'){
// printf("i1: %c, i2: %c\n", buffer[i+1], buffer[i+2]);
if (buffer[i+1] == '.' && ((c=buffer[i+2]) == '/' || c == '\0')){
// printf("removing 2 dots.\n");
// ii must start at the previous directory
int p=i-2; // char before the last '/'
for (; buffer[p] != '/'; p--){ }
int delen = 3 + (i - p - 1);
bufferlen -= delen;
int ii=p;
for (; ii <= bufferlen; ii++){
// printf("Moving ii+delen=%d=%c to ii=%d=%c\n", ii + delen, buffer[ii + delen], ii, buffer[ii]);
buffer[ii] = buffer[ii + delen];
}
i -= delen-1;
continue;
}
// resolve "." directories
if ((c=buffer[i+1]) == '/' || c == '\0'){
// printf("removing 1 dot.\n");
int delen = 2;
bufferlen -= delen;
for (int ii=i-1; ii <= bufferlen; ii++){
// printf("Moving ii+delen=%d=%c to ii=%d=%c\n", ii + delen, buffer[ii + delen], ii, buffer[ii]);
buffer[ii] = buffer[ii + delen];
}
i -= delen-1;
continue;
}
}
}
}
if (bufferlen == 0){
buffer[0] = '/'; buffer[1] = '\0';
bufferlen++;
}
else if (buffer[bufferlen-1] == '/'){
buffer[bufferlen-1] = '\0';
}
return buffer;
}
char escaper(char*);
int main(int argc, char** argv){
argc--;
int i = 1;
char d ='\n';
if (strcmp(argv[1], "-d") == 0){
d = escaper(argv[2]);
i += 2;
}
if (argc == 0)
exit(1);
char* cwd = alloca(PATH_MAX);
if ((cwd = getenv("PWD")) == NULL) {
printf("%s\n", cwd);
perror("getenv() error");
return 1;
}
for (; i <= argc; i++){
printf("%s%c", fullpath(argv[i], cwd), d);
}
}
char escaper(char* escape){
if (escape[0] == '\\' && escape[1] != '\0' && escape[2] == '\0'){
char c1 = escape[1];
switch (c1) {
case '0':
return '\0';
case 'a':
return '\a';
case 'b':
return '\b';
case 'f':
return '\f';
case 'n':
return '\n';
case 'r':
return '\r';
case 't':
return '\t';
case 'v':
return '\v';
case '\\':
return '\\';
}
}
return escape[0];
}