1 | #include "sysincludes.h"
|
---|
2 | #include "msdos.h"
|
---|
3 | #include "stream.h"
|
---|
4 | #include "file.h"
|
---|
5 | #include "mtoolsDirent.h"
|
---|
6 |
|
---|
7 | void initializeDirentry(direntry_t *entry, Stream_t *Dir)
|
---|
8 | {
|
---|
9 | entry->entry = -1;
|
---|
10 | /* entry->parent = getDirentry(Dir);*/
|
---|
11 | entry->Dir = Dir;
|
---|
12 | entry->beginSlot = 0;
|
---|
13 | entry->endSlot = 0;
|
---|
14 | }
|
---|
15 |
|
---|
16 | int isNotFound(direntry_t *entry)
|
---|
17 | {
|
---|
18 | return entry->entry == -2;
|
---|
19 | }
|
---|
20 |
|
---|
21 | void rewindEntry(direntry_t *entry)
|
---|
22 | {
|
---|
23 | entry->entry = -1;
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | direntry_t *getParent(direntry_t *entry)
|
---|
28 | {
|
---|
29 | return getDirentry(entry->Dir);
|
---|
30 | }
|
---|
31 |
|
---|
32 |
|
---|
33 | static int getPathLen(direntry_t *entry)
|
---|
34 | {
|
---|
35 | int length=0;
|
---|
36 |
|
---|
37 | while(1) {
|
---|
38 | if(entry->entry == -3) /* rootDir */
|
---|
39 | return strlen(getDrive(entry->Dir)) + 1 + length + 1;
|
---|
40 |
|
---|
41 | length += 1 + strlen(entry->name);
|
---|
42 | entry = getDirentry(entry->Dir);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | static char *sprintPwd(direntry_t *entry, char *ptr)
|
---|
47 | {
|
---|
48 | if(entry->entry == -3) {
|
---|
49 | strcpy(ptr, getDrive(entry->Dir));
|
---|
50 | strcat(ptr, ":/");
|
---|
51 | ptr = strchr(ptr, 0);
|
---|
52 | } else {
|
---|
53 | ptr = sprintPwd(getDirentry(entry->Dir), ptr);
|
---|
54 | if(ptr[-1] != '/')
|
---|
55 | *ptr++ = '/';
|
---|
56 | strcpy(ptr, entry->name);
|
---|
57 | ptr += strlen(entry->name);
|
---|
58 | }
|
---|
59 | return ptr;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | #define NEED_ESCAPE "\"$\\"
|
---|
64 |
|
---|
65 | static void _fprintPwd(FILE *f, direntry_t *entry, int recurs, int escape)
|
---|
66 | {
|
---|
67 | if(entry->entry == -3) {
|
---|
68 | fputs(getDrive(entry->Dir), f);
|
---|
69 | putc(':', f);
|
---|
70 | if(!recurs)
|
---|
71 | putc('/', f);
|
---|
72 | } else {
|
---|
73 | _fprintPwd(f, getDirentry(entry->Dir), 1, escape);
|
---|
74 | if (escape && strpbrk(entry->name, NEED_ESCAPE)) {
|
---|
75 | char *ptr;
|
---|
76 | for(ptr = entry->name; *ptr; ptr++) {
|
---|
77 | if (strchr(NEED_ESCAPE, *ptr))
|
---|
78 | putc('\\', f);
|
---|
79 | putc(*ptr, f);
|
---|
80 | }
|
---|
81 | } else {
|
---|
82 | fprintf(f, "/%s", entry->name);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void fprintPwd(FILE *f, direntry_t *entry, int escape)
|
---|
88 | {
|
---|
89 | if (escape)
|
---|
90 | putc('"', f);
|
---|
91 | _fprintPwd(f, entry, 0, escape);
|
---|
92 | if(escape)
|
---|
93 | putc('"', f);
|
---|
94 | }
|
---|
95 |
|
---|
96 | char *getPwd(direntry_t *entry)
|
---|
97 | {
|
---|
98 | int size;
|
---|
99 | char *ret;
|
---|
100 |
|
---|
101 | size = getPathLen(entry);
|
---|
102 | ret = malloc(size+1);
|
---|
103 | if(!ret)
|
---|
104 | return 0;
|
---|
105 | sprintPwd(entry, ret);
|
---|
106 | return ret;
|
---|
107 | }
|
---|
108 |
|
---|
109 | int isSubdirOf(Stream_t *inside, Stream_t *outside)
|
---|
110 | {
|
---|
111 | while(1) {
|
---|
112 | if(inside == outside) /* both are the same */
|
---|
113 | return 1;
|
---|
114 | if(getDirentry(inside)->entry == -3) /* root directory */
|
---|
115 | return 0;
|
---|
116 | /* look further up */
|
---|
117 | inside = getDirentry(inside)->Dir;
|
---|
118 | }
|
---|
119 | }
|
---|