hschumann2/TempleOS-Source-Code
0847
1 2#help_index "File/Cmd Line (Typically);Cmd Line (Typically)"3public U8 *DBlk(I64 blk,Bool write=FALSE)4{//Dump disk block. Optionally, write.5//If you set write to TRUE, the block will6 //be written when you press <ESC>.7 //See ::/Demo/Dsk/DskRaw.HC.8 U8 *buf=MAlloc(BLK_SIZE);9 10 BlkRead(Fs->cur_dv,buf,blk,1);11 DocD(buf,BLK_SIZE);12 if (write) {13 "Edit and press <ESC> to write or <SHIFT-ESC>\n";14 if (View) {15 "Write\n";16 BlkWrite(Fs->cur_dv,buf,blk,1);17 }18 }19 return buf;20}21 22public U8 *DClus(I64 c,Bool write=FALSE,I64 num=0)23{//Dump disk clus. Optionally, write.24//If you set write to TRUE, the clus will25 //be written when you press <ESC>.26 //See ::/Demo/Dsk/DskRaw.HC.27 //Do Dir("*",TRUE); to get clus numbers of files.28 U8 *buf=MAlloc(Fs->cur_dv->spc<<BLK_SIZE_BITS);29 c=ClusNumNext(Fs->cur_dv,c,num);30 ClusRead(Fs->cur_dv,buf,c,1);31 "Clus:%X\n",c;32 DocD(buf,Fs->cur_dv->spc<<BLK_SIZE_BITS);33 if (write) {34 "Edit and press <ESC> to write or <SHIFT-ESC>\n";35 if (View) {36 "Write\n";37 ClusWrite(Fs->cur_dv,buf,c,1);38 }39 }40 return buf;41}42 43public U8 *Dump(U8 *filename,Bool write=FALSE)44{//Dump file. Optionally, write.45//If you set write to TRUE, the file will46 //be written when you press <ESC>.47 U8 *buf;48 I64 size;49 if (buf=FileRead(filename,&size)) {50 DocD(buf,size);51 if (write) {52 "Edit and press <ESC> to write or <SHIFT-ESC>\n";53 if (View) {54 "Write\n";55 FileWrite(filename,buf,size);56 }57 }58 }59 return buf;60}61 62public Bool Copy(U8 *src_files_find_mask,U8 *dst_files_find_mask=".")63{//Copy files.64//If the name ends in ".Z", it will65 //be stored compressed. If not ".Z"66 //it will be stored uncompressed.67 Bool res=TRUE;68 CDirContext *dirc;69 CDirEntry *tmpde,*tmpde1;70 U8 *st;71 if (!(tmpde1=FilesFind(src_files_find_mask,FUF_CLUS_ORDER)))72 return FALSE;73 if (IsDir(dst_files_find_mask)) {74 if (dirc=DirContextNew(dst_files_find_mask,TRUE)) {75 tmpde=tmpde1;76 while (tmpde) {77 if (!(tmpde->attr & RS_ATTR_DIR)) {78 st=FileNameAbs(tmpde->name);79 if (!CopySingle(tmpde->full_name,st))80 res=FALSE;81 Free(st);82 }83 tmpde=tmpde->next;84 }85 DirContextDel(dirc);86 }87 DirTreeDel(tmpde1);88 return res;89 } else {90 DirTreeDel(tmpde1);91 return CopySingle(src_files_find_mask,dst_files_find_mask);92 }93}94 95public Bool Move(U8 *f1,U8 *f2)96{//Move files from one location to another or rename.97 if (Copy(f1,f2)) {98 Del(f1);99 return TRUE;100 }101 return FALSE;102}103 104I64 CopyTree2(CDirEntry *tmpde,I64 src_dir_len,I64 dst_dir_len,U8 *dst_dir)105{106 U8 *st;107 I64 res=1;108 while (tmpde) {109 st=MAlloc(StrLen(tmpde->full_name)+dst_dir_len+2);110 MemCpy(st,dst_dir,dst_dir_len);111 StrCpy(st+dst_dir_len,tmpde->full_name+src_dir_len);112 if (tmpde->attr & RS_ATTR_DIR) {113 DirMk(st,LinkedLstCnt(tmpde->sub));114 res+=CopyTree2(tmpde->sub,src_dir_len,dst_dir_len,dst_dir);115 } else116 if (CopySingle(tmpde->full_name,st))117 res++;118 Free(st);119 tmpde=tmpde->next;120 }121 return res;122}123public I64 CopyTree(U8 *src_files_find_mask,U8 *dst_files_find_mask,124 Bool no_mask=TRUE)125{//Copy directory tree.126//Returns the count of copied files (not dirs).127 CDirContext *dirc;128 CDirEntry *tmpde=NULL;129 I64 res=0,i1,i2;130 U8 *st1,*st2;131 132 st1=DirNameAbs(src_files_find_mask);133 st2=DirNameAbs(dst_files_find_mask);134 i1=StrLen(st1);135 if (!StrNCmp(st1,st2,i1) && (st2[i1]=='/' || !st2[i1]) ) {136 Free(st1);137 Free(st2);138 return 0;139 }140 Free(st1);141 Free(st2);142 if (dirc=DirContextNew(src_files_find_mask,TRUE,,no_mask)) {143 tmpde=FilesFind(dirc->mask,FUF_RECURSE);144 st1=DirCur;145 DirContextDel(dirc);146 i1=StrLen(st1);147 if (i1==3) i1--;148 if (dirc=DirContextNew(dst_files_find_mask,TRUE,TRUE)) {149 st2=DirCur;150 i2=StrLen(st2);151 if (i2==3) i2--;152 res=CopyTree2(tmpde,i1,i2,st2);153 DirContextDel(dirc);154 Free(st2);155 }156 DirTreeDel(tmpde);157 Free(st1);158 }159 return res;160}161 162I64 DelTreeDirs(CDirEntry *tmpde1)163{164 I64 res=0;165 CDirEntry *tmpde2;166 while (tmpde1) {167 tmpde2=tmpde1->next;168 if (tmpde1->attr & RS_ATTR_DIR) {169 if (tmpde1->sub)170 res+=DelTreeDirs(tmpde1->sub);171 res+=Del(tmpde1->full_name,TRUE,TRUE);172 }173 DirEntryDel(tmpde1);174 tmpde1=tmpde2;175 }176 return res;177}178I64 DelTreeFiles(CDirEntry *tmpde1)179{180 I64 res=0;181 CDirEntry *tmpde2;182 while (tmpde1) {183 tmpde2=tmpde1->next;184 if (tmpde1->attr & RS_ATTR_DIR) {185 if (tmpde1->sub)186 res+=DelTreeFiles(tmpde1->sub);187 } else188 res+=Del(tmpde1->full_name,FALSE,TRUE);189 DirEntryDel(tmpde1);190 tmpde1=tmpde2;191 }192 return res;193}194public I64 DelTree(U8 *files_find_mask,U8 *fu_flags=NULL)195{//Delete directory tree.196 I64 res=0,fuf_flags=0;197 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+r");198 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);199 if (IsDir(files_find_mask)) {200 res=DelTreeDirs(FilesFind(files_find_mask,fuf_flags));201 res+=Del(files_find_mask,TRUE,TRUE);202 res+=Del(files_find_mask,FALSE,TRUE);203 } else204 res=DelTreeFiles(FilesFind(files_find_mask,fuf_flags));205 return res;206}207 208U0 TouchFile(U8 *filename,U8 *attr,CDate cdt=I64_MIN)209{210 CDrv *dv=Let2Drv(*filename);211 CDirEntry de;212 U8 *cur_dir=StrNew(filename),buf[STR_LEN];213 if (FileFind(filename,&de,FUF_JUST_FILES)) {214 Free(de.full_name);215 if (!StrCmp(attr,"+?"))216 "%-48ts %s\n",filename,StrPrintFlags(buf,Define("ST_FILE_ATTRS"),de.attr);217 else {218 StrFirstRem(cur_dir,":");219 StrLastRem(cur_dir,"/");220 if (!*cur_dir)221 StrCpy(cur_dir,"/");222 ScanFlags(&de.attr,Define("ST_FILE_ATTRS"),attr);223 if (cdt==I64_MIN)224 de.datetime=Now;225 else226 de.datetime=cdt;227 DirNew(dv,cur_dir,&de,FALSE);228 }229 } else230 PrintErr("File not found: \"%s\".\n",filename);231 Free(cur_dir);232}233public U0 Touch(U8 *files_find_mask="*",U8 *attr="+?",234 U8 *fu_flags=NULL,CDate cdt=I64_MIN)235{/*Touch file attributes and DateTime.236Default lists attributes.237attr: "+?" =show current238"+T" =resident239RS_ATTR_READ_ONLY ST_FILE_ATTRS240To Set DateL:241Touch(filename,"",,datetime);242*/243 I64 fuf_flags=0;244 CDirEntry *tmpde,*tmpde1;245 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+f+F");246 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);247 tmpde=tmpde1=FilesFind(files_find_mask,fuf_flags);248 while (tmpde) {249 TouchFile(tmpde->full_name,attr,cdt);250 tmpde=tmpde->next;251 }252 DirTreeDel(tmpde1);253}254 