home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / DOpus4-GPL / Program / main5.c < prev    next >
C/C++ Source or Header  |  2000-01-27  |  11KB  |  478 lines

  1. /*
  2.  
  3. Directory Opus 4
  4. Original GPL release version 4.12
  5. Copyright 1993-2000 Jonathan Potter
  6.  
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License
  9. as published by the Free Software Foundation; either version 2
  10. of the License, or (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. All users of Directory Opus 4 (including versions distributed
  22. under the GPL) are entitled to upgrade to the latest version of
  23. Directory Opus version 5 at a reduced price. Please see
  24. http://www.gpsoft.com.au for more information.
  25.  
  26. The release of Directory Opus 4 under the GPL in NO WAY affects
  27. the existing commercial status of Directory Opus 5.
  28.  
  29. */
  30.  
  31. #include "dopus.h"
  32. #include "asyncio.h"
  33.  
  34. copyfile(src,dst,err,size,password,encryptstate)
  35. char *src,*dst;
  36. int *err,size;
  37. char *password;
  38. int encryptstate;
  39. {
  40.     char *buffer;
  41.     struct FileInfoBlock __aligned cfinfo;
  42.     int out,length,suc,readsize,size_read,size_write,size_total,ret=0,prog=0,buffer_size;
  43.     int inhandle;
  44.     int outhandle;
  45.     struct AsyncFile *infile;
  46. /*
  47.     struct AsyncFile *outfile;
  48. */
  49.     struct DateStamp ds,*dsp;
  50.     ULONG owner_info;
  51.  
  52.     buffer=NULL;
  53.     infile=NULL;
  54. /*
  55.     outfile=NULL;
  56. */
  57.     inhandle=0;
  58.     outhandle=0;
  59.  
  60.     if (password) {
  61.         int a,encrypt=1;
  62.  
  63.         for (a=0;password[a];a++) encrypt*=password[a];
  64.         Seed(encrypt);
  65.     }
  66.  
  67.     if (config->dynamicflags&UPDATE_PROGRESSIND_COPY) prog=1;
  68.  
  69.     suc=lockandexamine(src,&cfinfo);
  70.  
  71.     if (size==0) {
  72.         if (suc) {
  73.             if (cfinfo.fib_Size==0) {
  74.                 if ((out=Open(dst,MODE_NEWFILE))) Close(out);
  75.                 else goto failed;
  76.             }
  77.             if (config->copyflags©_DATE) setdate(dst,&(cfinfo.fib_Date));
  78.             if (config->copyflags©_PROT) SetProtection(dst,cfinfo.fib_Protection);
  79.             if (config->copyflags©_NOTE) SetComment(dst,cfinfo.fib_Comment);
  80.             if (system_version2) {
  81.                 owner_info=(cfinfo.fib_OwnerUID<<16)|cfinfo.fib_OwnerGID;
  82.                 if (owner_info) SetOwner(dst,owner_info);
  83.             }
  84.         }
  85.         filloutcopydatafile(dst);
  86.         if (suc) return(1);
  87.     }
  88.  
  89.     if (!suc) {
  90.         *err=IoErr();
  91.         return(0);
  92.     }
  93.  
  94.     if (!(size=cfinfo.fib_Size)) {
  95.         if (!(out=Open(dst,MODE_NEWFILE))) goto failed;
  96.         Close(out);
  97.         return(1);
  98.     }
  99.  
  100.     if (size<=COPY_BUF_SIZE) {
  101.         dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,100,100,NULL,1);
  102.         prog=0;
  103.     }
  104.     else dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,0,100,NULL,1);
  105.  
  106. /*
  107.     if (prog) {
  108.         buffer_size=COPY_BUF_SIZE;
  109.         buffer=AllocMem(COPY_BUF_SIZE,0);
  110.         if (infile=OpenAsync(src,MODE_READ,ASYNC_READ_SIZE))
  111.             outfile=OpenAsync(dst,MODE_WRITE,ASYNC_WRITE_SIZE);
  112.         if (!outfile) goto failed;
  113.     }
  114.     else {
  115.         if (size>65536) buffer_size=size/2;
  116.         else buffer_size=size;
  117.         if (buffer_size>122880) buffer_size=122880;
  118.  
  119.         while (buffer_size>0) {
  120.             if (buffer=AllocMem(buffer_size,0)) break;
  121.             buffer_size/=2;
  122.         }
  123.         if (inhandle=Open(src,MODE_OLDFILE))
  124.             outhandle=Open(dst,MODE_NEWFILE);
  125.         if (!outhandle) goto failed;
  126.     }
  127.  
  128.     if (!buffer) goto failed;
  129. */
  130.  
  131.     if (prog) {
  132.         buffer_size=2048;
  133.         infile=OpenAsync(src,MODE_READ,ASYNC_READ_SIZE);
  134.     }
  135.     else {
  136.         if (size>65536) buffer_size=size/2;
  137.         else buffer_size=size;
  138.         if (buffer_size>122880) buffer_size=122880;
  139.     }
  140.  
  141.     while (buffer_size>0) {
  142.         if (buffer=AllocMem(buffer_size,0)) break;
  143.         buffer_size/=2;
  144.     }
  145.  
  146.     if (!buffer) goto failed;
  147.  
  148.     if (!infile && (!(inhandle=Open(src,MODE_OLDFILE)))) goto failed;
  149.     if (!(outhandle=Open(dst,MODE_NEWFILE))) goto failed;
  150.  
  151.     DateStamp(&ds);
  152.  
  153.     size_read=size_write=0;
  154.     size_total=size*2;
  155.  
  156.     while (size>0) {
  157.         readsize=(size>buffer_size)?buffer_size:size;
  158.         if (infile) length=ReadAsync(infile,buffer,readsize);
  159.         else length=Read(inhandle,buffer,readsize);
  160.  
  161.         size-=readsize;
  162.         size_read+=length;
  163.  
  164.         if (prog)
  165.             dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,size_read+size_write,size_total,NULL,1);
  166.  
  167.         if (status_haveaborted) {
  168.             ret=-1;
  169.             goto failed;
  170.         }
  171.  
  172.         if (password) {
  173.             int a;
  174.             char enbyte;
  175.  
  176.             if (encryptstate) {
  177.                 for (a=0;a<length;a++) {
  178.                     enbyte=Random(9999);
  179.                     buffer[a]+=enbyte;
  180.                 }
  181.             }
  182.             else {
  183.                 for (a=0;a<length;a++) {
  184.                     enbyte=Random(9999);
  185.                     buffer[a]-=enbyte;
  186.                 }
  187.             }
  188.         }
  189.  
  190.         if (length>0) {
  191. /*
  192.             if (outfile) {
  193.                 if ((WriteAsync(outfile,buffer,length))==-1) goto failed;
  194.             }
  195.             else
  196. */
  197.             if ((Write(outhandle,buffer,length))==-1) goto failed;
  198.         }
  199.         size_write+=length;
  200.     
  201.         if (prog)
  202.             dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,size_read+size_write,size_total,NULL,1);
  203.  
  204.         if (status_haveaborted) {
  205.             ret=-1;
  206.             goto failed;
  207.         }
  208.     }
  209.  
  210.     if (infile) CloseAsync(infile);
  211. /*
  212.     if (outfile) CloseAsync(outfile);
  213. */
  214.     if (inhandle) Close(inhandle);
  215.     if (outhandle) Close(outhandle);
  216.  
  217.     FreeMem(buffer,buffer_size);
  218.  
  219.     if (config->copyflags©_DATE) {
  220.         setdate(dst,&(cfinfo.fib_Date));
  221.         dsp=&cfinfo.fib_Date;
  222.     }
  223.     else dsp=&ds;
  224.     copy_datestamp(dsp,&dos_copy_date);
  225.     if (config->copyflags©_PROT) SetProtection(dst,cfinfo.fib_Protection);
  226.     dos_copy_protection=cfinfo.fib_Protection;
  227.     if (config->copyflags©_NOTE) {
  228.         SetComment(dst,cfinfo.fib_Comment);
  229.         strcpy(dos_copy_comment,cfinfo.fib_Comment);
  230.     }
  231.     else dos_copy_comment[0]=0;
  232.     return(1);
  233.  
  234. failed:
  235.     *err=IoErr();
  236.     if (buffer) FreeMem(buffer,buffer_size);
  237.     if (infile) CloseAsync(infile);
  238.     if (inhandle) Close(inhandle);
  239.     if (/* outfile || */ outhandle) {
  240. /*
  241.         if (outfile) CloseAsync(outfile);
  242.         else
  243. */
  244.         Close(outhandle);
  245.         DeleteFile(dst);
  246.     }
  247.     return(ret);
  248. }
  249.  
  250. struct Directory *checktot(dir)
  251. struct DirectoryWindow *dir;
  252. {
  253.     struct Directory *first;
  254.  
  255.     if (!dir) return(NULL);
  256.     first=dir->firstentry;
  257.     while (first) {
  258.         if (first->selected && first->type<=ENTRY_FILE) break;
  259.         first=first->next;
  260.     }
  261.     return(first);
  262. }
  263.  
  264. struct Directory *checkdirtot(dir)
  265. struct DirectoryWindow *dir;
  266. {
  267.     struct Directory *first;
  268.  
  269.     if (!dir) return(NULL);
  270.     first=dir->firstentry;
  271.     while (first) {
  272.         if (first->selected && ENTRYTYPE(first->type)==ENTRY_DIRECTORY) break;
  273.         first=first->next;
  274.     }
  275.     return(first);
  276. }
  277.  
  278. struct Directory *checkdevtot(dir)
  279. struct DirectoryWindow *dir;
  280. {
  281.     struct Directory *first;
  282.  
  283.     if (!dir) return(NULL);
  284.     first=dir->firstentry;
  285.     while (first) {
  286.         if (first->selected && first->type==ENTRY_DEVICE) break;
  287.         first=first->next;
  288.     }
  289.     return(first);
  290. }
  291.  
  292. struct Directory *checkalltot(dir)
  293. struct DirectoryWindow *dir;
  294. {
  295.     struct Directory *first;
  296.  
  297.     if (!dir) return(NULL);
  298.     first=dir->firstentry;
  299.     while (first) {
  300.         if (first->selected && first->type!=ENTRY_DEVICE && first->type!=ENTRY_CUSTOM) break;
  301.         first=first->next;
  302.     }
  303.     return(first);
  304. }
  305.  
  306. struct Directory *findfile(dir,name,count)
  307. struct DirectoryWindow *dir;
  308. char *name;
  309. int *count;
  310. {
  311.     struct Directory *find;
  312. /*
  313.     char parsebuf[100];
  314.  
  315.     LParsePatternI(name,parsebuf);
  316. */
  317.  
  318.     if (dir) {
  319.         find=dir->firstentry;
  320.         if (count) *count=0;
  321.         while (find) {
  322. /*
  323.             if (find->name && (LMatchPatternI(parsebuf,find->name))) return(find);
  324. */
  325.             if (find->name && !(LStrCmpI(name,find->name))) return(find);
  326.  
  327.             find=find->next;
  328.             if (count) ++(*count);
  329.         }
  330.     }
  331.     return(NULL);
  332. }
  333.  
  334. delfile(name,nam,errs,unprotect,errcheck)
  335. char *name,*nam,*errs;
  336. int unprotect,errcheck;
  337. {
  338.     int suc,a,err,try=0,recplus=0;
  339.     char buf[300],buf2[100];
  340.  
  341. loop:
  342.     suc=DeleteFile(name);
  343.     if (!suc) {
  344.         if ((err=IoErr())==ERROR_OBJECT_NOT_FOUND) suc=1;
  345.         else {
  346.             if (err==ERROR_DIRECTORY_NOT_EMPTY) return(-2);
  347.             else if (err==ERROR_DELETE_PROTECTED && try==0) {
  348.                 if (!(config->deleteflags&DELETE_SET)) {
  349.                     if (!unprotect) {
  350.                         doerror(ERROR_DELETE_PROTECTED);
  351.                         geterrorstring(buf2,ERROR_DELETE_PROTECTED);
  352.                         lsprintf(buf,globstring[STR_ERROR_OCCURED],globstring[STR_DELETING],nam,buf2);
  353.                         strcat(buf,globstring[STR_SELECT_UNPROTECT]);
  354.                         if (!(a=simplerequest(buf,globstring[STR_UNPROTECT],
  355.                             globstring[STR_ABORT],globstring[STR_UNPROTECT_ALL],globstring[STR_SKIP],NULL)))
  356.                             return(-1);
  357.                         if (a==3) return(0);
  358.                         if (a==2) recplus=1;
  359.                     }
  360.                 }
  361.                 try=1;
  362.                 SetProtection(name,0);
  363.                 goto loop;
  364.             }
  365.             else if (!errcheck) return(-2);
  366.             doerror(err);
  367.             if ((a=checkerror(errs,nam,err))==3) return(-1);
  368.             if (a==1) goto loop;
  369.         }
  370.     }
  371.     else {
  372.         if (recplus) suc=2;
  373.         else suc=1;
  374.     }
  375.     return(suc);
  376. }
  377.  
  378. getwildrename(sname,dname,name,newn)
  379. char *sname,*dname,*name,*newn;
  380. {
  381.     char sfirst[40],slast[40],dfirst[40],dlast[40],foon[40];
  382.     int a,b,c,flen,llen,d;
  383.  
  384.     b=strlen(sname); sfirst[0]=slast[0]=0;
  385.     for (a=0;a<b;a++)
  386.         if (sname[a]=='*') {
  387.             strcpy(sfirst,sname); sfirst[(flen=a)]=0;
  388.             strcpy(slast,(char *)&sname[a+1]);
  389.             llen=b-a;
  390.             break;
  391.         }
  392.     b=strlen(dname);
  393.     for (a=0;a<b;a++)
  394.         if (dname[a]=='*') {
  395.             strcpy(dfirst,dname); dfirst[a]=0;
  396.             strcpy(dlast,(char *)&dname[a+1]);
  397.             break;
  398.         }
  399.     a=strlen(sfirst); b=strlen(slast);
  400.     if ((!a || (LStrnCmpI(name,sfirst,flen))==0) &&
  401.         (!b || ((d=strlen(name))>=llen && (LStrCmpI(&name[(d-llen)+1],slast))==0))) {
  402.         c=strlen(name)-a-b;
  403.         CopyMem((char *)&name[a],foon,c);
  404.         foon[c]=0;
  405.         strcpy(newn,dfirst); strcat(newn,foon); strcat(newn,dlast);
  406.         if (newn[0]!=0) return(1);
  407.     }
  408.     return(0);
  409. }
  410.  
  411. void filloutcopydata(dir)
  412. struct Directory *dir;
  413. {
  414.     dos_copy_date.ds_Days=dir->date.ds_Days;
  415.     dos_copy_date.ds_Minute=dir->date.ds_Minute;
  416.     dos_copy_date.ds_Tick=dir->date.ds_Tick;
  417.     dos_copy_protection=dir->protection;
  418.     if (dir->comment) strcpy(dos_copy_comment,dir->comment);
  419.     else dos_copy_comment[0]=0;
  420. }
  421.  
  422. void filloutcopydatafile(fil)
  423. char *fil;
  424. {
  425.     struct FileInfoBlock __aligned fileinfo;
  426.  
  427.     if (lockandexamine(fil,&fileinfo)) {
  428.         dos_copy_date.ds_Days=fileinfo.fib_Date.ds_Days;
  429.         dos_copy_date.ds_Minute=fileinfo.fib_Date.ds_Minute;
  430.         dos_copy_date.ds_Tick=fileinfo.fib_Date.ds_Tick;
  431.         dos_copy_protection=fileinfo.fib_Protection;
  432.         strcpy(dos_copy_comment,fileinfo.fib_Comment);
  433.     }
  434. }
  435.  
  436. void update_buffer_stamp(win,true)
  437. int win,true;
  438. {
  439.     struct FileInfoBlock __aligned fib;
  440.     char dirbuf[256];
  441.     struct DirectoryWindow *dirwin;
  442.  
  443.     if (win==-1 || !(config->dirflags&DIRFLAGS_REREADOLD)) return;
  444.  
  445.     dirwin=dopus_curwin[win];
  446.     strcpy(dirbuf,str_pathbuffer[win]);
  447.     FOREVER {
  448.         if (true) {
  449.             if (lockandexamine(dirbuf,&fib))
  450.                 copy_datestamp(&fib.fib_Date,&dirwin->dirstamp);
  451.             if (!(doparent(dirbuf)) ||
  452.                 !(dirwin=findbuffer(dirbuf,win,0,1))) break;
  453.         }
  454.         else {
  455.             if (!(dirwin=dirwin->next) ||
  456.                 dirwin==dopus_curwin[win]) break;
  457.             if (LStrnCmpI(dirwin->directory,dirbuf,strlen(dirbuf))==0) {
  458.                 dirwin->dirstamp.ds_Days=0;
  459.                 dirwin->dirstamp.ds_Minute=0;
  460.                 dirwin->dirstamp.ds_Tick=0;
  461.             }
  462.         }
  463.     }
  464. }
  465.  
  466. check_key_press(func,code,qual)
  467. struct dopusfunction *func;
  468. USHORT code,qual;
  469. {
  470.     if (!func->function ||
  471.         !func->function[0] ||
  472.         (func->key==0xff && func->qual==0) ||
  473.         func->key==0 ||
  474.         func->qual!=qual) return(0);
  475.     if (func->key==0xff || func->key==code) return(1);
  476.     return(0);
  477. }
  478.