static void GetIndividualFile(char* dest, char* path, char* file) { strcpy(dest, path); GetPreviousDir(dest); addsep(dest); strcat(dest, file); }
BOOL MakeAbsolutePath(char* workingdir, char* relative, char* result) { char* rel = relative; char* temp; assert(workingdir && relative && result); if ((relative[0] == '/') || (relative[0] == '\\')) { strcpy(result, relative); /* path already absolute */ return TRUE; } strcpy(result, workingdir); if (workingdir[0] == 0) RETURN_FTEERROR(FALSE); temp = strchr(result, 0); if ((*(temp-1) != '/') && (*(temp-1) != '\\')) strcat(result, "\\"); while ((temp = SkipDots(rel)) != NULL) { if (!GetPreviousDir(result)) RETURN_FTEERROR(FALSE); rel = temp; } if ((strlen(result) + strlen(rel)) > MAXPATH) RETURN_FTEERROR(FALSE); strcat(result, rel); return TRUE; }
static int DelTree(const char* path) { int found, attrib; char temppath[MAXPATH]; char temppath1[MAXPATH]; char origpath[MAXPATH]; struct ffblk info; strcpy(temppath, path); strcpy(origpath, path); GetPreviousDir(origpath); /* Two state machine: */ /* As long as the complete tree is not deleted */ AddAllWildCard(origpath); AddAllWildCard(temppath); while (strcmp(temppath, origpath) != 0) { /* 1) as long as there are still sub directories, enter the first one */ found = findfirst(temppath, &info, FA_DIREC) == 0; while (found && (info.ff_name[0] == '.' || (info.ff_attrib & FA_DIREC) == 0)) { found = findnext(&info) == 0; } if (found) { GetIndividualFile(temppath1, temppath, info.ff_name); strcpy(temppath, temppath1); AddAllWildCard(temppath); } /* 2) if there are no more sub directories in this directory, delete all the files, leave the directory, and delete it */ else { found = findfirst(temppath, &info, FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH) == 0; while (found) { GetIndividualFile(temppath1, temppath, info.ff_name); if (chmod(temppath1, S_IREAD|S_IWRITE) == -1) return 0; if (unlink(temppath1) == -1) return 0; found = findnext(&info) == 0; } GetPreviousDir(temppath); /* Strip \*.* */ /* DRR: FreeDOS cannot set attributes of directory without * clearing directory bit (but the directory will not be * changed to a normal file) */ if ((attrib = _chmod(temppath, 0)) == -1) return 0; attrib &= ~FA_RDONLY; if (_chmod(temppath, 1, attrib & ~FA_DIREC) == -1) return 0; if (rmdir(temppath) == -1) return 0; GetPreviousDir(temppath); /* Leave directory */ AddAllWildCard(temppath); } } return 1; }
int RenameTree(const char* src_filename, const char* dest_filename) { int found, attrib; char temppath[MAXPATH]; char temppath1[MAXPATH]; char origpath[MAXPATH]; char tgtpath[MAXPATH]; char tgtpath1[MAXPATH]; struct ffblk info; char srcdrive[MAXDRIVE], srcdir[MAXDIR], srcfname[MAXFILE], srcext[MAXEXT]; char dstdrive[MAXDRIVE], dstdir[MAXDIR], dstfname[MAXFILE], dstext[MAXEXT]; /* If the target directory exists, delete it */ if (dir_exists(dest_filename)) { if (!DelTree(dest_filename)) return 0; } /* First see wether we are not renaming a directory in the same path */ SplitPath(src_filename, srcdrive, srcdir, srcfname, srcext); SplitPath(dest_filename, dstdrive, dstdir, dstfname, dstext); if ((strcmpi(srcdir, dstdir) == 0) && (!dir_exists(dest_filename))) { return rename(src_filename, dest_filename) == 0; } /* Deep renaming a directory */ strcpy(temppath, src_filename); strcpy(origpath, src_filename); GetPreviousDir(origpath); strcpy(tgtpath, dest_filename); if (mkdir(tgtpath) == -1) return 0; /* Two state machine: */ /* As long as the complete tree is not deleted */ AddAllWildCard(origpath); AddAllWildCard(temppath); while (strcmp(temppath, origpath) != 0) { /* 1) as long as there are still sub directories, enter the first one */ found = findfirst(temppath, &info, FA_DIREC) == 0; while (found && (info.ff_name[0] == '.' || (info.ff_attrib & FA_DIREC) == 0)) { found = findnext(&info) == 0; } if (found) { GetIndividualFile(temppath1, temppath, info.ff_name); strcpy(temppath, temppath1); AddAllWildCard(temppath); /* Make sure the target directory exists */ addsep(tgtpath); strcat(tgtpath, info.ff_name); if (mkdir(tgtpath) == -1) return 0; } /* 2) if there are no more sub directories in this directory, rename all the files and leave this directory, and delete this directory */ else { found = findfirst(temppath, &info, FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_ARCH) == 0; while (found) { GetIndividualFile(temppath1, temppath, info.ff_name); strcpy(tgtpath1, tgtpath); addsep(tgtpath1); strcat(tgtpath1, info.ff_name); unlink(tgtpath1); if (rename(temppath1, tgtpath1) == -1) return 0; found = findnext(&info) == 0; } GetPreviousDir(temppath); /* Strip *.* */ /* DRR: FreeDOS cannot set attributes of directory without * clearing directory bit (but the directory will not be * changed to a normal file) */ if ((attrib = _chmod(temppath, 0)) == -1) return 0; attrib &= ~FA_RDONLY; if (_chmod(temppath, 1, attrib & ~FA_DIREC) == -1) return 0; if (rmdir(temppath) == -1) return 0; GetPreviousDir(temppath); /* Leave directory */ AddAllWildCard(temppath); GetPreviousDir(tgtpath); /* Leave target directory */ } } return 1; }