00001 #include "sysincludes.h"
00002 #include "msdos.h"
00003 #include "stream.h"
00004 #include "file.h"
00005 #include "mtoolsDirent.h"
00006
00007 void initializeDirentry(direntry_t *entry, Stream_t *Dir)
00008 {
00009 entry->entry = -1;
00010
00011 entry->Dir = Dir;
00012 entry->beginSlot = 0;
00013 entry->endSlot = 0;
00014 }
00015
00016 int isNotFound(direntry_t *entry)
00017 {
00018 return entry->entry == -2;
00019 }
00020
00021 void rewindEntry(direntry_t *entry)
00022 {
00023 entry->entry = -1;
00024 }
00025
00026
00027 direntry_t *getParent(direntry_t *entry)
00028 {
00029 return getDirentry(entry->Dir);
00030 }
00031
00032
00033 static int getPathLen(direntry_t *entry)
00034 {
00035 int length=0;
00036
00037 while(1) {
00038 if(entry->entry == -3)
00039 return strlen(getDrive(entry->Dir)) + 1 + length + 1;
00040
00041 length += 1 + strlen(entry->name);
00042 entry = getDirentry(entry->Dir);
00043 }
00044 }
00045
00046 static char *sprintPwd(direntry_t *entry, char *ptr)
00047 {
00048 if(entry->entry == -3) {
00049 strcpy(ptr, getDrive(entry->Dir));
00050 strcat(ptr, ":/");
00051 ptr = strchr(ptr, 0);
00052 } else {
00053 ptr = sprintPwd(getDirentry(entry->Dir), ptr);
00054 if(ptr[-1] != '/')
00055 *ptr++ = '/';
00056 strcpy(ptr, entry->name);
00057 ptr += strlen(entry->name);
00058 }
00059 return ptr;
00060 }
00061
00062
00063 #define NEED_ESCAPE "\"$\\"
00064
00065 static void _fprintPwd(FILE *f, direntry_t *entry, int recurs, int escape)
00066 {
00067 if(entry->entry == -3) {
00068 fputs(getDrive(entry->Dir), f);
00069 putc(':', f);
00070 if(!recurs)
00071 putc('/', f);
00072 } else {
00073 _fprintPwd(f, getDirentry(entry->Dir), 1, escape);
00074 if (escape && strpbrk(entry->name, NEED_ESCAPE)) {
00075 char *ptr;
00076 for(ptr = entry->name; *ptr; ptr++) {
00077 if (strchr(NEED_ESCAPE, *ptr))
00078 putc('\\', f);
00079 putc(*ptr, f);
00080 }
00081 } else {
00082 fprintf(f, "/%s", entry->name);
00083 }
00084 }
00085 }
00086
00087 void fprintPwd(FILE *f, direntry_t *entry, int escape)
00088 {
00089 if (escape)
00090 putc('"', f);
00091 _fprintPwd(f, entry, 0, escape);
00092 if(escape)
00093 putc('"', f);
00094 }
00095
00096 char *getPwd(direntry_t *entry)
00097 {
00098 int size;
00099 char *ret;
00100
00101 size = getPathLen(entry);
00102 ret = malloc(size+1);
00103 if(!ret)
00104 return 0;
00105 sprintPwd(entry, ret);
00106 return ret;
00107 }
00108
00109 int isSubdirOf(Stream_t *inside, Stream_t *outside)
00110 {
00111 while(1) {
00112 if(inside == outside)
00113 return 1;
00114 if(getDirentry(inside)->entry == -3)
00115 return 0;
00116
00117 inside = getDirentry(inside)->Dir;
00118 }
00119 }