source: trunk/dll/copyf.c@ 29

Last change on this file since 29 was 29, checked in by root, 23 years ago

Drop obsolete debug code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.1 KB
Line 
1
2/***********************************************************************
3
4 $Id: copyf.c 29 2002-10-16 23:25:28Z root $
5
6 Copy functions
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2001, 2002 Steven H.Levine
10
11 Revisions 14 Sep 02 SHL - Drop obsolete debug code
12 14 Oct 02 SHL - Drop obsolete debug code
13
14***********************************************************************/
15
16#define INCL_DOS
17#define INCL_DOSERRORS
18#define INCL_WIN
19
20#include <os2.h>
21#include <io.h>
22#include <string.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <stdarg.h>
26#include <ctype.h>
27#include <time.h>
28#include "fm3dll.h"
29
30#ifndef WinMoveObject
31 HOBJECT APIENTRY WinMoveObject(HOBJECT hObjectofObject,
32 HOBJECT hObjectofDest,
33 ULONG ulReserved);
34#endif
35#ifndef WinCopyObject
36 HOBJECT APIENTRY WinCopyObject(HOBJECT hObjectofObject,
37 HOBJECT hObjectofDest,
38 ULONG ulReserved);
39#endif
40
41#pragma alloc_text(LONGNAMES,TruncName,GetLongName,WriteLongName)
42#pragma alloc_text(LONGNAMES,ZapLongName,AdjustWildcardName)
43#pragma alloc_text(COPYF,default_disk,docopyf)
44#pragma alloc_text(UNLINKF,unlinkf,unlink_allf,make_deleteable,wipeallf)
45
46
47char *MakeTempName (char *buffer) {
48
49 FILESTATUS3 fs3;
50 APIRET rc;
51 char *p,*o;
52
53 p = o = buffer + strlen(buffer);
54 sprintf(p,"%08lx.%03lx",clock(),mypid);
55 p = buffer + (strlen(buffer) - 1);
56 for(;;) {
57 DosError(FERR_DISABLEHARDERR);
58 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
59 (ULONG)sizeof(fs3));
60 if(rc == ERROR_DISK_CHANGE) {
61 DosError(FERR_ENABLEHARDERR);
62 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
63 (ULONG)sizeof(fs3));
64 }
65 if(rc)
66 break;
67Loop:
68 if(p < o) {
69 *buffer = 0;
70 return NULL;
71 }
72 if((*p) + 1 < 'Z' + 1) {
73 (*p)++;
74 while(strchr("*?<>\":/\\|+=;,[]. ",*p))
75 (*p)++;
76 *p = toupper(*p);
77 }
78 else {
79 p--;
80 if(p >= o && *p == '.')
81 p--;
82 goto Loop;
83 }
84 }
85 return buffer;
86}
87
88
89CHAR *TruncName (CHAR *oldname,CHAR *buffer) {
90
91 CHAR *p,*f,*s,*o;
92 FILESTATUS3 fs3;
93 APIRET rc;
94
95 if(!buffer || !oldname || !*oldname) {
96 if(buffer)
97 *buffer = 0;
98 return NULL;
99 }
100 strcpy(buffer,oldname);
101 f = strrchr(buffer,'\\');
102 if(!f)
103 f = strrchr(buffer,'/');
104 if(!f)
105 f = buffer;
106 else
107 f++;
108 p = f;
109 o = p;
110 f = oldname + (f - buffer);
111 strupr(buffer);
112 while(*f == '.') /* skip leading '.'s */
113 f++;
114 s = f;
115 while(*f && *f != '.' && f < s + 8) { /* skip past rootname */
116 *p = toupper(*f);
117 p++;
118 f++;
119 }
120 while(*f == '.')
121 f++;
122 s = f;
123 f = strrchr(f,'.');
124 if(f) {
125 while(*f == '.')
126 f++;
127 }
128 if(f && *(f + 1))
129 s = f;
130 else
131 f = s;
132 if(*f) {
133 *p = '.';
134 p++;
135 while(*f && *f != '.' && f < s + 3) {
136 *p = toupper(*f);
137 p++;
138 f++;
139 }
140 }
141 *p = 0;
142
143 p = o;
144 while(*p) {
145 if(strchr("*?<>\":/\\|+=;,[] ",*p) || *p < 0x20)
146 *p = '_';
147 if(*p == '.' && *(p + 1) == '.')
148 *(p + 1) = '_';
149 p++;
150 }
151
152 p = o + (strlen(o) - 1);
153 for(;;) {
154 DosError(FERR_DISABLEHARDERR);
155 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
156 (ULONG)sizeof(fs3));
157 if(rc == ERROR_DISK_CHANGE) {
158 DosError(FERR_ENABLEHARDERR);
159 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
160 (ULONG)sizeof(fs3));
161 }
162 if(rc)
163 break;
164Loop:
165 if(p < o) {
166 *buffer = 0;
167 return NULL;
168 }
169 if((*p) + 1 < 'Z' + 1) {
170 (*p)++;
171 while(strchr("*?<>\":/\\|+=;,[]. ",*p))
172 (*p)++;
173 *p = toupper(*p);
174 }
175 else {
176 p--;
177 if(p >= o && *p == '.')
178 p--;
179 goto Loop;
180 }
181 }
182 return buffer;
183}
184
185
186CHAR *GetLongName (CHAR *oldname,CHAR *longname) {
187
188 if(!longname)
189 return NULL;
190 *longname = 0;
191 if(!oldname ||
192 !*oldname)
193 return NULL;
194 if(IsFullName(oldname)) {
195
196 APIRET rc;
197 EAOP2 eaop;
198 PGEA2LIST pgealist;
199 PFEA2LIST pfealist;
200 PGEA2 pgea;
201 PFEA2 pfea;
202 CHAR *value;
203
204 strcpy(longname,
205 oldname);
206 value = longname;
207 while(*value) {
208 if(*value == '/')
209 *value = '\\';
210 value++;
211 }
212 value = strrchr(longname,'\\');
213 if(value) {
214 value++;
215 *value = 0;
216 }
217 pgealist = malloc(sizeof(GEA2LIST) + 32);
218 if(pgealist) {
219 memset(pgealist,0,sizeof(GEA2LIST) + 32);
220 pgea = &pgealist->list[0];
221 strcpy(pgea->szName,LONGNAME);
222 pgea->cbName = strlen(pgea->szName);
223 pgea->oNextEntryOffset = 0L;
224 pgealist->cbList = (sizeof(GEA2LIST) + pgea->cbName);
225 pfealist = malloc(1536);
226 if(pfealist) {
227 memset(pfealist,0,1024);
228 pfealist->cbList = 1024;
229 eaop.fpGEA2List = pgealist;
230 eaop.fpFEA2List = pfealist;
231 eaop.oError = 0L;
232 DosError(FERR_DISABLEHARDERR);
233 rc = DosQueryPathInfo(oldname,
234 FIL_QUERYEASFROMLIST,
235 (PVOID)&eaop,
236 (ULONG)sizeof(EAOP2));
237 if(!rc) {
238 pfea = &eaop.fpFEA2List->list[0];
239 value = pfea->szName + pfea->cbName + 1;
240 value[pfea->cbValue] = 0;
241 if(*(USHORT *)value == EAT_ASCII)
242 strncat(longname,
243 value + (sizeof(USHORT) * 2),
244 CCHMAXPATH - strlen(longname));
245 longname[CCHMAXPATH - 1] = 0;
246 }
247 free(pfealist);
248 }
249 free(pgealist);
250 }
251 }
252 return longname;
253}
254
255
256BOOL ZapLongName (char *filename) {
257
258#ifdef DEBUG
259 saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Zapped longname."); // fixme to be gone
260#endif
261 return WriteLongName(filename, "");
262}
263
264
265BOOL WriteLongName (CHAR *filename,CHAR *longname) {
266
267 APIRET rc;
268 EAOP2 eaop;
269 PFEA2LIST pfealist = NULL;
270 ULONG ealen;
271 USHORT len;
272 CHAR *eaval,*p;
273
274 if(!filename ||
275 !*filename ||
276 !longname)
277 return FALSE;
278 p = strrchr(longname,'\\');
279 if(p)
280 memmove(longname,
281 p + 1,
282 strlen(p + 1) + 1);
283 p = strrchr(longname,'/');
284 if(p)
285 memmove(longname,
286 p + 1,
287 strlen(p + 1) + 1);
288 lstrip(rstrip(longname));
289 len = strlen(longname);
290 if(len)
291 ealen = sizeof(FEA2LIST) + 10 + len + 4;
292 else
293 ealen = sizeof(FEALIST) + 10;
294 if(!DosAllocMem((PPVOID)&pfealist,
295 ealen + 32L,
296 OBJ_TILE | PAG_COMMIT | PAG_READ | PAG_WRITE)) {
297 memset(pfealist,
298 0,
299 ealen + 1);
300 pfealist->cbList = ealen;
301 pfealist->list[0].oNextEntryOffset = 0L;
302 pfealist->list[0].fEA = 0;
303 pfealist->list[0].cbName = 9;
304 strcpy(pfealist->list[0].szName,
305 LONGNAME);
306 if(len) {
307 eaval = pfealist->list[0].szName + 10;
308 *(USHORT *)eaval = (USHORT)EAT_ASCII;
309 eaval += sizeof(USHORT);
310 *(USHORT *)eaval = (USHORT)len;
311 eaval += sizeof(USHORT);
312 memcpy(eaval,
313 longname,
314 len);
315 pfealist->list[0].cbValue = len + (sizeof(USHORT) * 2);
316 }
317 else
318 pfealist->list[0].cbValue = 0;
319 eaop.fpGEA2List = (PGEA2LIST)0;
320 eaop.fpFEA2List = pfealist;
321 eaop.oError = 0L;
322 DosError(FERR_DISABLEHARDERR);
323 rc = DosSetPathInfo(filename,
324 FIL_QUERYEASIZE,
325 (PVOID)&eaop,
326 (ULONG)sizeof(EAOP2),
327 DSPI_WRTTHRU);
328 DosFreeMem(pfealist);
329 if(rc)
330 return FALSE;
331 }
332 return TRUE;
333}
334
335
336BOOL AdjustWildcardName (CHAR *oldname,CHAR *newname) {
337
338 BOOL ret = FALSE;
339
340 /* NOTE: newname should be CCHMAXPATH chars long! */
341
342 if(strchr(newname,'*') || strchr(newname,'?')) {
343
344 CHAR srce[CCHMAXPATHCOMP],dest[CCHMAXPATHCOMP],result[CCHMAXPATHCOMP],*p;
345
346 p = strrchr(newname,'\\');
347 if(p && *(p + 1)) {
348 strcpy(dest,p + 1);
349 p = strrchr(oldname,'\\');
350 if(p && *(p + 1)) {
351 strcpy(srce,p + 1);
352 DosError(FERR_DISABLEHARDERR);
353 if(!DosEditName(1L,srce,dest,result,(ULONG)sizeof(result))) {
354 p = strrchr(newname,'\\');
355 p++;
356 strcpy(p,result);
357 ret = TRUE;
358 }
359 }
360 }
361 }
362 return ret;
363}
364
365
366CHAR default_disk (VOID) {
367
368 ULONG ulDriveNum,ulDriveMap;
369
370 DosError(FERR_DISABLEHARDERR);
371 DosQCurDisk(&ulDriveNum,&ulDriveMap);
372 return (CHAR)toupper((INT)ulDriveNum) + '@';
373}
374
375
376#ifdef NEVER
377
378APIRET docopyallf (INT type,CHAR *oldname,CHAR *newname,...) {
379
380 FILEFINDBUF3 fb;
381 ULONG nm;
382 HDIR hdir;
383 APIRET rc = 0;
384 CHAR *enddir,fullname[CCHMAXPATH];
385
386 va_start(ap,newname);
387 vsprintf(fullname,newname,ap);
388 va_end(ap);
389
390 DosError(FERR_DISABLEHARDERR);
391 if(!DosFindFirst(oldname)) {
392 do {
393
394 /* build target name */
395
396 if(fb.attrFile & FILE_DIRECTORY) {
397 DosError(FERR_ENABLEHARDERR);
398 rc = DosCreateDir();
399 if(rc == ERROR_INVALID_NAME || rc == ERROR_FILENAME_EXCED_RANGE) {
400
401 /* truncate directory name */
402 /* create that directory */
403 /* update containers for name used */
404
405 }
406 rc = docopyallf(type,,"%s",); /* recurse */
407 }
408 else
409 rc = docopyf(type,,"%s",); /* copy file */
410 DosError(FERR_DISABLEHARDERR);
411 } while(!rc && !DosFindNext());
412 DosFindClose(hdir);
413 }
414 else
415 rc = ERROR_FILE_NOT_FOUND;
416 return rc;
417}
418
419#endif
420
421
422APIRET docopyf (INT type,CHAR *oldname,CHAR *newname,...) {
423
424 /*
425 * returns:
426 * 0: success
427 * -1: bad string parameter(s)
428 * -2: source didn't exist
429 * -3: bad type
430 * anything else: API return
431 */
432
433 CHAR fullnewname[CCHMAXPATH + 1],longname[CCHMAXPATH],
434 shortname[CCHMAXPATH];
435 CHAR olddisk,newdisk,dir[CCHMAXPATH],*p,*pp;
436 APIRET ret = -1,rc;
437 FILESTATUS3 st,st2,dummy;
438 BOOL diskchange = FALSE,zaplong = FALSE;
439 va_list ap;
440
441 *fullnewname = *longname = *shortname = *dir = 0;
442
443 va_start(ap,
444 newname);
445 vsprintf(fullnewname,
446 newname,
447 ap);
448 va_end(ap);
449
450 if(!oldname ||
451 !*oldname ||
452 !*fullnewname) /* bad string args */
453 return (APIRET)-1;
454
455 DosError(FERR_DISABLEHARDERR);
456 if(DosQueryPathInfo(oldname,
457 FIL_STANDARD,
458 &st,
459 sizeof(FILESTATUS3)))
460 return (APIRET)-2; /* no source */
461
462 AdjustWildcardName(oldname,
463 fullnewname);
464 MakeFullName(oldname);
465 MakeFullName(fullnewname);
466 olddisk = toupper(*oldname); /* source drive */
467 newdisk = toupper(*fullnewname); /* destination drive */
468 GetLongName(oldname,
469 longname);
470 if(*longname) {
471 if(!(driveflags[toupper(*oldname) - 'A'] & DRIVE_NOLONGNAMES))
472 *longname = 0;
473 else {
474 p = RootName(longname);
475 if(p != longname)
476 memmove(longname,
477 p,
478 strlen(p));
479 }
480 /* did root name change? */
481 p = RootName(oldname);
482 pp = RootName(fullnewname);
483 if(stricmp(p,
484 pp))
485 {
486#ifdef DEBUG
487 saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"oldname: %s\rnewname: %s",oldname,fullnewname); // fixme to be gone
488#endif
489 zaplong = TRUE;
490 }
491 }
492
493 DosError(FERR_DISABLEHARDERR);
494 switch(type)
495 {
496 case WPSMOVE:
497 {
498 HOBJECT hobjsrc;
499 HOBJECT hobjdest;
500
501 ret = ERROR_FILE_NOT_FOUND;
502 hobjsrc = WinQueryObject(oldname);
503 if(hobjsrc) {
504 strcpy(dir,
505 fullnewname);
506 p = strrchr(dir,'\\');
507 if(p < dir + 3)
508 p++;
509 *p = 0;
510 ret = ERROR_PATH_NOT_FOUND;
511 hobjdest = WinQueryObject(dir);
512 if(hobjdest) {
513 ret = ERROR_GEN_FAILURE;
514 hobjsrc = WinMoveObject(hobjsrc,
515 hobjdest,
516 0);
517 if(hobjsrc)
518 ret = 0;
519 }
520 }
521 }
522 return ret;
523
524 case WPSCOPY:
525 {
526 HOBJECT hobjsrc;
527 HOBJECT hobjdest;
528
529 ret = ERROR_FILE_NOT_FOUND;
530 hobjsrc = WinQueryObject(oldname);
531 if(hobjsrc) {
532 strcpy(dir,
533 fullnewname);
534 p = strrchr(dir,'\\');
535 if(p < dir + 3)
536 p++;
537 *p = 0;
538 ret = ERROR_PATH_NOT_FOUND;
539 hobjdest = WinQueryObject(dir);
540 if(hobjdest) {
541 ret = ERROR_GEN_FAILURE;
542 hobjsrc = WinCopyObject(hobjsrc,
543 hobjdest,
544 0);
545 if(hobjsrc)
546 ret = 0;
547 }
548 }
549 }
550 return ret;
551
552 case MOVE:
553 *dir = 0;
554 if(olddisk == newdisk) { /* same drive */
555 /* make temporary copy in case move fails */
556 if(IsFile(fullnewname) != -1 &&
557 stricmp(oldname,
558 fullnewname)) {
559 strcpy(dir,
560 fullnewname);
561 p = strrchr(dir,'\\');
562 if(p)
563 *p = 0;
564 strcat(dir,"\\");
565 MakeTempName(dir);
566 if(DosMove(fullnewname,
567 dir))
568 *dir = 0;
569 }
570 DosError(FERR_DISABLEHARDERR);
571 ret = DosMove(oldname,
572 fullnewname); /* move it */
573 if(ret &&
574 *dir) { /* failed -- clean up */
575 DosError(FERR_DISABLEHARDERR);
576 if(!DosMove(dir,
577 fullnewname))
578 Broadcast((HAB)0,
579 hwndMain,
580 UM_UPDATERECORD,
581 MPFROMP(dir),
582 MPVOID);
583 }
584 else if(!ret &&
585 *dir) {
586 if(!IsFile(dir)) {
587 if(!strchr(dir,'?') &&
588 !strchr(dir,'*'))
589 wipeallf("%s\\*",dir);
590 DosError(FERR_DISABLEHARDERR);
591 if(DosDeleteDir(dir)) {
592 make_deleteable(dir);
593 DosDeleteDir(dir);
594 }
595 }
596 else if(IsFile(dir) > 0) {
597 DosError(FERR_DISABLEHARDERR);
598 if(DosForceDelete(dir)) {
599 make_deleteable(dir);
600 DosForceDelete(dir);
601 }
602 if(zaplong)
603 ZapLongName(dir);
604 Broadcast((HAB)0,
605 hwndMain,
606 UM_UPDATERECORD,
607 MPFROMP(dir),
608 MPVOID);
609 }
610 }
611 }
612 else { /* different drives */
613 DosError(FERR_DISABLEHARDERR);
614 ret = DosCopy(oldname,
615 fullnewname,
616 DCPY_EXISTING); /* <=-NOTE! */
617 if(ret == ERROR_DISK_CHANGE) {
618 DosError(FERR_ENABLEHARDERR);
619 ret = DosCopy(oldname,
620 fullnewname,
621 DCPY_EXISTING);
622 diskchange = TRUE;
623 }
624 if(ret == ERROR_INVALID_NAME ||
625 ret == ERROR_FILENAME_EXCED_RANGE) {
626 if(TruncName(fullnewname,
627 shortname)) { /* make 8.3 filename */
628 DosError(FERR_DISABLEHARDERR);
629 ret = DosCopy(oldname,
630 shortname,
631 DCPY_EXISTING);
632 if(!ret) { /* success -- write longname ea */
633 WriteLongName(shortname,
634 fullnewname);
635 strcpy(fullnewname,
636 shortname);
637 /* broadcast fixup msg to windows */
638 Broadcast((HAB)0,
639 hwndMain,
640 UM_UPDATERECORD,
641 MPFROMP(shortname),
642 MPVOID);
643 }
644 }
645 }
646 else if(!ret &&
647 *longname) {
648
649 CHAR fixname[CCHMAXPATH];
650
651 strcpy(fixname,
652 fullnewname);
653 p = strrchr(fixname,'\\');
654 if(p) {
655 p++;
656 *p = 0;
657 }
658 strcat(fixname,
659 longname);
660 DosError(FERR_DISABLEHARDERR);
661 DosMove(fullnewname,
662 fixname);
663 strcpy(fullnewname,
664 fixname);
665 if(zaplong)
666 ZapLongName(fixname);
667 Broadcast((HAB)0,
668 hwndMain,
669 UM_UPDATERECORD,
670 MPFROMP(fixname),
671 MPVOID);
672 }
673 if(!ret) { /* double-check success */
674 DosError(FERR_DISABLEHARDERR);
675 rc = DosQueryPathInfo(fullnewname,
676 FIL_STANDARD,
677 &st2,
678 sizeof(FILESTATUS3));
679 if(rc == ERROR_DISK_CHANGE) {
680 DosError(FERR_ENABLEHARDERR);
681 rc = DosQueryPathInfo(fullnewname,
682 FIL_STANDARD,
683 &st2,
684 sizeof(FILESTATUS3));
685 }
686 if(!rc && st2.cbFile == st.cbFile) { /* seems to have worked... */
687 DosError(FERR_DISABLEHARDERR);
688 if(diskchange) {
689 DosError(FERR_ENABLEHARDERR);
690 DosQueryPathInfo(oldname,
691 FIL_STANDARD,
692 &dummy,
693 sizeof(FILESTATUS3)); /* force disk change */
694 }
695 if(!(st2.attrFile & FILE_DIRECTORY)) /* erase file */
696 unlinkf("%s",oldname);
697 else { /* remove directory */
698 wipeallf("%s\\*",oldname);
699 DosError(FERR_DISABLEHARDERR);
700 if(DosDeleteDir(oldname)) {
701 make_deleteable(oldname);
702 DosDeleteDir(oldname);
703 }
704 }
705 }
706 }
707 }
708 return ret;
709
710 case COPY:
711 DosError(FERR_DISABLEHARDERR);
712 ret = DosCopy(oldname,
713 fullnewname,
714 DCPY_EXISTING); /* <=-NOTE! */
715 if(ret == ERROR_DISK_CHANGE) {
716 DosError(FERR_ENABLEHARDERR);
717 ret = DosCopy(oldname,
718 fullnewname,
719 DCPY_EXISTING);
720 diskchange = TRUE;
721 }
722 if(ret == ERROR_INVALID_NAME ||
723 ret == ERROR_FILENAME_EXCED_RANGE) {
724 if(TruncName(fullnewname,
725 shortname)) {
726 DosError((diskchange) ?
727 FERR_ENABLEHARDERR :
728 FERR_DISABLEHARDERR);
729 ret = DosCopy(oldname,
730 shortname,
731 DCPY_EXISTING);
732 if(!ret) {
733 WriteLongName(shortname,
734 fullnewname);
735 strcpy(fullnewname,
736 shortname);
737 Broadcast((HAB)0,
738 hwndMain,
739 UM_UPDATERECORD,
740 MPFROMP(shortname),
741 MPVOID);
742 }
743 }
744 }
745 else if(!ret &&
746 *longname) {
747
748 CHAR fixname[CCHMAXPATH];
749
750 strcpy(fixname,
751 fullnewname);
752 p = strrchr(fixname,'\\');
753 if(p) {
754 p++;
755 *p = 0;
756 }
757 strcat(fixname,
758 longname);
759 DosError(FERR_DISABLEHARDERR);
760 DosMove(fullnewname,
761 fixname);
762 if(zaplong)
763 ZapLongName(fixname);
764 Broadcast((HAB)0,
765 hwndMain,
766 UM_UPDATERECORD,
767 MPFROMP(fixname),
768 MPVOID);
769 }
770 return ret;
771
772 default: /* shouldn't happen */
773 DosBeep(50,100);
774 break;
775 }
776 return (APIRET)-3; /* bad type */
777}
778
779
780INT make_deleteable (CHAR *filename) {
781
782 INT ret = -1;
783 FILESTATUS3 fsi;
784
785 DosError(FERR_DISABLEHARDERR);
786 if(!DosQueryPathInfo(filename,
787 FIL_STANDARD,
788 &fsi,
789 sizeof(FILESTATUS3))) {
790 fsi.attrFile = 0;
791 DosError(FERR_DISABLEHARDERR);
792 if(!DosSetPathInfo(filename,
793 FIL_STANDARD,
794 &fsi,
795 sizeof(FILESTATUS3),
796 0L))
797 ret = 0;
798 }
799 return ret;
800}
801
802
803INT wipeallf (CHAR *string,...) {
804
805 /* unlink everything from directory on down... */
806
807 FILEFINDBUF3 *f;
808 HDIR search_handle;
809 ULONG num_matches;
810 CHAR *p,*ss,*str;
811 CHAR s[CCHMAXPATH],mask[257];
812 va_list ap;
813 INT rc;
814
815 va_start(ap,string);
816 vsprintf(s,string,ap);
817 va_end(ap);
818
819 if(!*s)
820 return -1;
821 p = s;
822 while((p = strchr(p,'/')) != NULL) {
823 *p = '\\';
824 p++;
825 }
826
827 str = strdup(s);
828 if(!str)
829 return -1;
830
831 { /* safety net -- disallow deleting a root dir or partial name */
832 CHAR temp;
833
834 p = strrchr(str,'\\');
835 if(p) {
836 p++;
837 temp = *p;
838 *p = 0;
839 if(IsRoot(str) || !IsFullName(str)) { /* under no circumstances! */
840 free(str);
841 DosBeep(100,250);
842 return -1;
843 }
844 *p = temp;
845 }
846 }
847
848 p = s;
849 p = strrchr(s,'\\'); /* strip s to just path */
850 if(!p)
851 p = strrchr(s,':');
852 if(p) {
853 p++;
854 strncpy(mask,p,256);
855 mask[256] = 0;
856 *p = 0;
857 }
858 else {
859 *mask = 0;
860 *s = 0;
861 }
862
863 ss = (CHAR *)malloc(CCHMAXPATH);
864 f = (FILEFINDBUF3 *)malloc(sizeof(FILEFINDBUF3));
865 if(!ss || !f) {
866 if(ss)
867 free(ss);
868 if(f)
869 free(f);
870 free(str);
871 return -1;
872 }
873
874 search_handle = HDIR_CREATE;
875 num_matches = 1L;
876
877 DosError(FERR_DISABLEHARDERR);
878 if(!DosFindFirst(str,&search_handle,FILE_NORMAL | FILE_DIRECTORY |
879 FILE_SYSTEM | FILE_READONLY | FILE_HIDDEN | FILE_ARCHIVED,f,
880 sizeof(FILEFINDBUF3),&num_matches,FIL_STANDARD)) {
881
882 strcpy(ss,s);
883 p = &ss[strlen(ss)];
884
885 do {
886 strcpy(p,f->achName);
887 if(f->attrFile & FILE_DIRECTORY) {
888 if(strcmp(f->achName,".") && strcmp(f->achName,"..")) {
889 wipeallf("%s/%s",ss,mask); /* recurse to wipe files */
890 DosError(FERR_DISABLEHARDERR);
891 if(DosDeleteDir(ss)) { /* remove directory */
892 make_deleteable(ss);
893 DosError(FERR_DISABLEHARDERR);
894 DosDeleteDir(ss);
895 }
896 }
897 }
898 else {
899 DosError(FERR_DISABLEHARDERR);
900 if(DosForceDelete(ss)) {
901 make_deleteable(ss);
902 DosError(FERR_DISABLEHARDERR);
903 rc = (INT)DosForceDelete(ss);
904 if(rc)
905 return rc;
906 }
907 }
908 num_matches = 1L;
909 DosError(FERR_DISABLEHARDERR);
910 } while(!DosFindNext(search_handle,f,sizeof(FILEFINDBUF3),
911 &num_matches));
912 DosFindClose(search_handle);
913 }
914
915 free(f);
916 free(ss);
917 free(str);
918 return 0;
919}
920
921
922INT unlink_allf (CHAR *string,...) {
923
924 /* wildcard delete */
925
926 FILEFINDBUF3 *f;
927 HDIR search_handle;
928 ULONG num_matches;
929 CHAR *p,*ss,*str;
930 CHAR s[CCHMAXPATH];
931 va_list ap;
932
933 va_start(ap,string);
934 vsprintf(s,string,ap);
935 va_end(ap);
936
937 if(!*s)
938 return -1;
939 p = s;
940 while((p = strchr(p,'/')) != NULL) {
941 *p = '\\';
942 p++;
943 }
944
945 str = strdup(s);
946 if(!str)
947 return -1;
948
949 p = s;
950 p = strrchr(s,'\\'); /* strip s to just path */
951 if(!p)
952 p = strrchr(s,':');
953 if(p) {
954 p++;
955 *p = 0;
956 }
957 else
958 *s = 0;
959
960 ss = (CHAR *)malloc(CCHMAXPATH);
961 f = (FILEFINDBUF3 *)malloc(sizeof(FILEFINDBUF3));
962 if(!ss || !f) {
963 if(ss)
964 free(ss);
965 if(f)
966 free(f);
967 free(str);
968 return -1;
969 }
970
971 search_handle = HDIR_CREATE;
972 num_matches = 1L;
973
974 DosError(FERR_DISABLEHARDERR);
975 if(!DosFindFirst(str,&search_handle,FILE_NORMAL,f,
976 sizeof(FILEFINDBUF3),&num_matches,FIL_STANDARD)) {
977
978 strcpy(ss,s);
979 p = &ss[strlen(ss)];
980
981 do {
982 strcpy(p,f->achName);
983 unlinkf("%s",ss);
984 num_matches = 1L;
985 DosError(FERR_DISABLEHARDERR);
986 } while(!DosFindNext(search_handle,f,sizeof(FILEFINDBUF3),
987 &num_matches));
988 DosFindClose(search_handle);
989 }
990
991 free(f);
992 free(ss);
993 free(str);
994 return 0;
995}
996
997
998INT unlinkf (CHAR *string,...) {
999
1000 CHAR buffer[CCHMAXPATH];
1001 va_list ap;
1002
1003 va_start(ap,string);
1004 vsprintf(buffer,string,ap);
1005 va_end(ap);
1006
1007 if(!strstr(buffer,ArcTempRoot)) {
1008 DosError(FERR_DISABLEHARDERR);
1009 if(DosDelete(buffer)) {
1010 make_deleteable(buffer);
1011 DosError(FERR_DISABLEHARDERR);
1012 return DosDelete(buffer);
1013 }
1014 }
1015 else {
1016 DosError(FERR_DISABLEHARDERR);
1017 if(DosForceDelete(buffer)) {
1018 make_deleteable(buffer);
1019 DosError(FERR_DISABLEHARDERR);
1020 return DosForceDelete(buffer);
1021 }
1022 }
1023 return 0;
1024}
1025
Note: See TracBrowser for help on using the repository browser.