source: trunk/dll/copyf.c@ 123

Last change on this file since 123 was 123, checked in by root, 21 years ago

Rework lstrip/rstrip usage

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.2 KB
Line 
1
2/***********************************************************************
3
4 $Id: copyf.c 123 2004-12-05 00:20:19Z root $
5
6 Copy functions
7
8 Copyright (c) 1993-98 M. Kimes
9 Copyright (c) 2001, 2004 Steven H.Levine
10
11 Revisions 14 Sep 02 SHL Drop obsolete debug code
12 14 Oct 02 SHL Drop obsolete debug code
13 10 Nov 02 SHL docopyf - don't forget to terminate longname
14 optimize longname logic
15 01 Aug 04 SHL Rework lstrip/rstrip usage
16
17***********************************************************************/
18
19#define INCL_DOS
20#define INCL_DOSERRORS
21#define INCL_WIN
22
23#include <os2.h>
24#include <io.h>
25#include <string.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <stdarg.h>
29#include <ctype.h>
30#include <time.h>
31#include "fm3dll.h"
32
33#ifndef WinMoveObject
34 HOBJECT APIENTRY WinMoveObject(HOBJECT hObjectofObject,
35 HOBJECT hObjectofDest,
36 ULONG ulReserved);
37#endif
38#ifndef WinCopyObject
39 HOBJECT APIENTRY WinCopyObject(HOBJECT hObjectofObject,
40 HOBJECT hObjectofDest,
41 ULONG ulReserved);
42#endif
43
44#pragma alloc_text(LONGNAMES,TruncName,GetLongName,WriteLongName)
45#pragma alloc_text(LONGNAMES,ZapLongName,AdjustWildcardName)
46#pragma alloc_text(COPYF,default_disk,docopyf)
47#pragma alloc_text(UNLINKF,unlinkf,unlink_allf,make_deleteable,wipeallf)
48
49
50char *MakeTempName (char *buffer) {
51
52 FILESTATUS3 fs3;
53 APIRET rc;
54 char *p,*o;
55
56 p = o = buffer + strlen(buffer);
57 sprintf(p,"%08lx.%03lx",clock(),mypid);
58 p = buffer + (strlen(buffer) - 1);
59 for(;;) {
60 DosError(FERR_DISABLEHARDERR);
61 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
62 (ULONG)sizeof(fs3));
63 if(rc == ERROR_DISK_CHANGE) {
64 DosError(FERR_ENABLEHARDERR);
65 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
66 (ULONG)sizeof(fs3));
67 }
68 if(rc)
69 break;
70Loop:
71 if(p < o) {
72 *buffer = 0;
73 return NULL;
74 }
75 if((*p) + 1 < 'Z' + 1) {
76 (*p)++;
77 while(strchr("*?<>\":/\\|+=;,[]. ",*p))
78 (*p)++;
79 *p = toupper(*p);
80 }
81 else {
82 p--;
83 if(p >= o && *p == '.')
84 p--;
85 goto Loop;
86 }
87 }
88 return buffer;
89}
90
91
92CHAR *TruncName (CHAR *oldname,CHAR *buffer) {
93
94 CHAR *p,*f,*s,*o;
95 FILESTATUS3 fs3;
96 APIRET rc;
97
98 if(!buffer || !oldname || !*oldname) {
99 if(buffer)
100 *buffer = 0;
101 return NULL;
102 }
103 strcpy(buffer,oldname);
104 f = strrchr(buffer,'\\');
105 if(!f)
106 f = strrchr(buffer,'/');
107 if(!f)
108 f = buffer;
109 else
110 f++;
111 p = f;
112 o = p;
113 f = oldname + (f - buffer);
114 strupr(buffer);
115 while(*f == '.') /* skip leading '.'s */
116 f++;
117 s = f;
118 while(*f && *f != '.' && f < s + 8) { /* skip past rootname */
119 *p = toupper(*f);
120 p++;
121 f++;
122 }
123 while(*f == '.')
124 f++;
125 s = f;
126 f = strrchr(f,'.');
127 if(f) {
128 while(*f == '.')
129 f++;
130 }
131 if(f && *(f + 1))
132 s = f;
133 else
134 f = s;
135 if(*f) {
136 *p = '.';
137 p++;
138 while(*f && *f != '.' && f < s + 3) {
139 *p = toupper(*f);
140 p++;
141 f++;
142 }
143 }
144 *p = 0;
145
146 p = o;
147 while(*p) {
148 if(strchr("*?<>\":/\\|+=;,[] ",*p) || *p < 0x20)
149 *p = '_';
150 if(*p == '.' && *(p + 1) == '.')
151 *(p + 1) = '_';
152 p++;
153 }
154
155 p = o + (strlen(o) - 1);
156 for(;;) {
157 DosError(FERR_DISABLEHARDERR);
158 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
159 (ULONG)sizeof(fs3));
160 if(rc == ERROR_DISK_CHANGE) {
161 DosError(FERR_ENABLEHARDERR);
162 rc = DosQueryPathInfo(buffer,FIL_STANDARD,&fs3,
163 (ULONG)sizeof(fs3));
164 }
165 if(rc)
166 break;
167Loop:
168 if(p < o) {
169 *buffer = 0;
170 return NULL;
171 }
172 if((*p) + 1 < 'Z' + 1) {
173 (*p)++;
174 while(strchr("*?<>\":/\\|+=;,[]. ",*p))
175 (*p)++;
176 *p = toupper(*p);
177 }
178 else {
179 p--;
180 if(p >= o && *p == '.')
181 p--;
182 goto Loop;
183 }
184 }
185 return buffer;
186}
187
188
189CHAR *GetLongName (CHAR *oldname,CHAR *longname) {
190
191 if(!longname)
192 return NULL;
193 *longname = 0;
194 if(!oldname ||
195 !*oldname)
196 return NULL;
197 if(IsFullName(oldname)) {
198
199 APIRET rc;
200 EAOP2 eaop;
201 PGEA2LIST pgealist;
202 PFEA2LIST pfealist;
203 PGEA2 pgea;
204 PFEA2 pfea;
205 CHAR *value;
206
207 strcpy(longname,
208 oldname);
209 value = longname;
210 while(*value) {
211 if(*value == '/')
212 *value = '\\';
213 value++;
214 }
215 value = strrchr(longname,'\\');
216 if(value) {
217 value++;
218 *value = 0;
219 }
220 pgealist = malloc(sizeof(GEA2LIST) + 32);
221 if(pgealist) {
222 memset(pgealist,0,sizeof(GEA2LIST) + 32);
223 pgea = &pgealist->list[0];
224 strcpy(pgea->szName,LONGNAME);
225 pgea->cbName = strlen(pgea->szName);
226 pgea->oNextEntryOffset = 0L;
227 pgealist->cbList = (sizeof(GEA2LIST) + pgea->cbName);
228 pfealist = malloc(1536);
229 if(pfealist) {
230 memset(pfealist,0,1024);
231 pfealist->cbList = 1024;
232 eaop.fpGEA2List = pgealist;
233 eaop.fpFEA2List = pfealist;
234 eaop.oError = 0L;
235 DosError(FERR_DISABLEHARDERR);
236 rc = DosQueryPathInfo(oldname,
237 FIL_QUERYEASFROMLIST,
238 (PVOID)&eaop,
239 (ULONG)sizeof(EAOP2));
240 if(!rc) {
241 pfea = &eaop.fpFEA2List->list[0];
242 value = pfea->szName + pfea->cbName + 1;
243 value[pfea->cbValue] = 0;
244 if(*(USHORT *)value == EAT_ASCII)
245 strncat(longname,
246 value + (sizeof(USHORT) * 2),
247 CCHMAXPATH - strlen(longname));
248 longname[CCHMAXPATH - 1] = 0;
249 }
250 free(pfealist);
251 }
252 free(pgealist);
253 }
254 }
255 return longname;
256}
257
258
259BOOL ZapLongName (char *filename) {
260
261#ifdef DEBUG
262 saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"Zapped longname."); // fixme to be gone
263#endif
264 return WriteLongName(filename, "");
265}
266
267
268BOOL WriteLongName (CHAR *filename,CHAR *longname) {
269
270 APIRET rc;
271 EAOP2 eaop;
272 PFEA2LIST pfealist = NULL;
273 ULONG ealen;
274 USHORT len;
275 CHAR *eaval,*p;
276
277 if(!filename ||
278 !*filename ||
279 !longname)
280 return FALSE;
281 p = strrchr(longname,'\\');
282 if(p)
283 memmove(longname,
284 p + 1,
285 strlen(p + 1) + 1);
286 p = strrchr(longname,'/');
287 if(p)
288 memmove(longname,
289 p + 1,
290 strlen(p + 1) + 1);
291 bstrip(longname);
292 len = strlen(longname);
293 if(len)
294 ealen = sizeof(FEA2LIST) + 10 + len + 4;
295 else
296 ealen = sizeof(FEALIST) + 10;
297 if(!DosAllocMem((PPVOID)&pfealist,
298 ealen + 32L,
299 OBJ_TILE | PAG_COMMIT | PAG_READ | PAG_WRITE)) {
300 memset(pfealist,
301 0,
302 ealen + 1);
303 pfealist->cbList = ealen;
304 pfealist->list[0].oNextEntryOffset = 0L;
305 pfealist->list[0].fEA = 0;
306 pfealist->list[0].cbName = 9;
307 strcpy(pfealist->list[0].szName,
308 LONGNAME);
309 if(len) {
310 eaval = pfealist->list[0].szName + 10;
311 *(USHORT *)eaval = (USHORT)EAT_ASCII;
312 eaval += sizeof(USHORT);
313 *(USHORT *)eaval = (USHORT)len;
314 eaval += sizeof(USHORT);
315 memcpy(eaval,
316 longname,
317 len);
318 pfealist->list[0].cbValue = len + (sizeof(USHORT) * 2);
319 }
320 else
321 pfealist->list[0].cbValue = 0;
322 eaop.fpGEA2List = (PGEA2LIST)0;
323 eaop.fpFEA2List = pfealist;
324 eaop.oError = 0L;
325 DosError(FERR_DISABLEHARDERR);
326 rc = DosSetPathInfo(filename,
327 FIL_QUERYEASIZE,
328 (PVOID)&eaop,
329 (ULONG)sizeof(EAOP2),
330 DSPI_WRTTHRU);
331 DosFreeMem(pfealist);
332 if(rc)
333 return FALSE;
334 }
335 return TRUE;
336}
337
338
339BOOL AdjustWildcardName (CHAR *oldname,CHAR *newname) {
340
341 BOOL ret = FALSE;
342
343 /* NOTE: newname should be CCHMAXPATH chars long! */
344
345 if(strchr(newname,'*') || strchr(newname,'?')) {
346
347 CHAR srce[CCHMAXPATHCOMP],dest[CCHMAXPATHCOMP],result[CCHMAXPATHCOMP],*p;
348
349 p = strrchr(newname,'\\');
350 if(p && *(p + 1)) {
351 strcpy(dest,p + 1);
352 p = strrchr(oldname,'\\');
353 if(p && *(p + 1)) {
354 strcpy(srce,p + 1);
355 DosError(FERR_DISABLEHARDERR);
356 if(!DosEditName(1L,srce,dest,result,(ULONG)sizeof(result))) {
357 p = strrchr(newname,'\\');
358 p++;
359 strcpy(p,result);
360 ret = TRUE;
361 }
362 }
363 }
364 }
365 return ret;
366}
367
368
369CHAR default_disk (VOID) {
370
371 ULONG ulDriveNum,ulDriveMap;
372
373 DosError(FERR_DISABLEHARDERR);
374 DosQCurDisk(&ulDriveNum,&ulDriveMap);
375 return (CHAR)toupper((INT)ulDriveNum) + '@';
376}
377
378
379#ifdef NEVER
380
381APIRET docopyallf (INT type,CHAR *oldname,CHAR *newname,...) {
382
383 FILEFINDBUF3 fb;
384 ULONG nm;
385 HDIR hdir;
386 APIRET rc = 0;
387 CHAR *enddir,fullname[CCHMAXPATH];
388
389 va_start(ap,newname);
390 vsprintf(fullname,newname,ap);
391 va_end(ap);
392
393 DosError(FERR_DISABLEHARDERR);
394 if(!DosFindFirst(oldname)) {
395 do {
396
397 /* build target name */
398
399 if(fb.attrFile & FILE_DIRECTORY) {
400 DosError(FERR_ENABLEHARDERR);
401 rc = DosCreateDir();
402 if(rc == ERROR_INVALID_NAME || rc == ERROR_FILENAME_EXCED_RANGE) {
403
404 /* truncate directory name */
405 /* create that directory */
406 /* update containers for name used */
407
408 }
409 rc = docopyallf(type,,"%s",); /* recurse */
410 }
411 else
412 rc = docopyf(type,,"%s",); /* copy file */
413 DosError(FERR_DISABLEHARDERR);
414 } while(!rc && !DosFindNext());
415 DosFindClose(hdir);
416 }
417 else
418 rc = ERROR_FILE_NOT_FOUND;
419 return rc;
420}
421
422#endif
423
424
425APIRET docopyf (INT type,CHAR *oldname,CHAR *newname,...) {
426
427 /*
428 * returns:
429 * 0: success
430 * -1: bad string parameter(s)
431 * -2: source didn't exist
432 * -3: bad type
433 * anything else: API return
434 */
435
436 CHAR fullnewname[CCHMAXPATH + 1],longname[CCHMAXPATH],
437 shortname[CCHMAXPATH];
438 CHAR olddisk,newdisk,dir[CCHMAXPATH],*p,*pp;
439 APIRET ret = -1,rc;
440 FILESTATUS3 st,st2,dummy;
441 BOOL diskchange = FALSE,zaplong = FALSE;
442 va_list ap;
443
444 *fullnewname = *shortname = *dir = 0;
445
446 va_start(ap,
447 newname);
448 vsprintf(fullnewname,
449 newname,
450 ap);
451 va_end(ap);
452
453 if(!oldname ||
454 !*oldname ||
455 !*fullnewname) /* bad string args */
456 return (APIRET)-1;
457
458 DosError(FERR_DISABLEHARDERR);
459 if(DosQueryPathInfo(oldname,
460 FIL_STANDARD,
461 &st,
462 sizeof(FILESTATUS3)))
463 return (APIRET)-2; /* no source */
464
465 AdjustWildcardName(oldname,
466 fullnewname);
467 MakeFullName(oldname);
468 MakeFullName(fullnewname);
469 olddisk = toupper(*oldname); /* source drive */
470 newdisk = toupper(*fullnewname); /* destination drive */
471 if(!(driveflags[toupper(*oldname) - 'A'] & DRIVE_NOLONGNAMES))
472 *longname = 0;
473 else
474 {
475 GetLongName(oldname, longname);
476 if(*longname) {
477 p = RootName(longname);
478 if(p != longname)
479 memmove(longname, p, strlen(p) + 1);
480 }
481 }
482 /* If root name changed make sure longname EA goes away */
483 p = RootName(oldname);
484 pp = RootName(fullnewname);
485 if(stricmp(p, pp))
486 {
487#ifdef DEBUG
488 saymsg(MB_ENTER,HWND_DESKTOP,DEBUG_STRING,"oldname: %s\rnewname: %s",oldname,fullnewname); // fixme to be gone
489#endif
490 zaplong = TRUE;
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.