source: trunk/dll/copyf.c@ 45

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

docopyf - don't forget to terminate longname, optimize longname logic

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