Enable asyncio.library.
[AROS-Contrib.git] / dopus / Program / main5.c
blob1869de472622d3fc09d708e3486c0d0c3ccf87d0
1 /*
3 Directory Opus 4
4 Original GPL release version 4.12
5 Copyright 1993-2000 Jonathan Potter
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.
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.
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.
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.
26 The release of Directory Opus 4 under the GPL in NO WAY affects
27 the existing commercial status of Directory Opus 5.
31 #include "dopus.h"
32 #include <proto/asyncio.h>
35 //struct Library *AsyncIOBase;
37 int copyfile(src,dst,err,/*size,*/password,encryptstate)
38 char *src,*dst;
39 int *err/*,size*/;
40 char *password;
41 int encryptstate;
43 struct FileInfoBlock __aligned cfinfo;
44 char *buffer;
45 int length,suc,readsize,size_read,size_write,size_total,ret=0,buffer_size,size;
46 int prog = (config->dynamicflags&UPDATE_PROGRESSIND_COPY);
47 BPTR out,inhandle,outhandle;
48 struct AsyncFile *infile;
50 struct AsyncFile *outfile;
52 struct DateStamp ds,*dsp;
53 // ULONG owner_info;
55 buffer=NULL;
57 infile=NULL;
58 outfile=NULL;
60 inhandle=0;
61 outhandle=0;
63 if (password) {
64 int a,encrypt=1;
66 for (a=0;password[a];a++) encrypt*=password[a];
67 Seed(encrypt);
70 suc=lockandexamine(src,&cfinfo);
72 if (!suc) {
73 *err=IoErr();
74 return(0);
77 if (!(size=cfinfo.fib_Size)) {
78 if (!(out=Open(dst,MODE_NEWFILE))) goto failed;
79 Close(out);
80 if (config->copyflags&COPY_DATE) setdate(dst,&(cfinfo.fib_Date));
81 if (config->copyflags&COPY_PROT) SetProtection(dst,cfinfo.fib_Protection&((config->copyflags&COPY_COPYARC)?~0:~FIBF_ARCHIVE));
82 if (config->copyflags&COPY_NOTE) SetComment(dst,cfinfo.fib_Comment);
83 SetOwner(dst,(cfinfo.fib_OwnerUID<<16)|cfinfo.fib_OwnerGID);
84 return(1);
87 if (size<=COPY_BUF_SIZE) {
88 dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,100,100,NULL,1);
89 prog=0;
91 else dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,0,100,NULL,1);
93 infile = OpenAsync(src,MODE_READ,ASYNC_READ_SIZE);
94 if (!infile && (!(inhandle=Open(src,MODE_OLDFILE)))) goto failed;
96 if (!(outhandle=Open(dst,MODE_NEWFILE))) goto failed;
98 if (size>(64*1024)) buffer_size=size/2;
99 else buffer_size=size;
100 if (buffer_size>(128*1024)) buffer_size=128*1024;
102 while (buffer_size>0) {
103 if ((buffer=AllocMem(buffer_size,MEMF_ANY))) break;
104 buffer_size/=2;
106 if (!buffer) goto failed;
108 size_read=size_write=0;
109 size_total=size*2;
111 while (size>0) {
112 readsize=(size>buffer_size)?buffer_size:size;
113 if (infile) length=ReadAsync(infile,buffer,readsize);
114 else length=Read(inhandle,buffer,readsize);
116 size-=readsize;
117 size_read+=length;
119 if (prog)
120 dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,size_read+size_write,size_total,NULL,1);
122 if (status_haveaborted) {
123 ret=-1;
124 goto failed;
127 if (password) {
128 int a;
129 char enbyte;
131 if (encryptstate) {
132 for (a=0;a<length;a++) {
133 enbyte=Random(9999);
134 buffer[a]+=enbyte;
137 else {
138 for (a=0;a<length;a++) {
139 enbyte=Random(9999);
140 buffer[a]-=enbyte;
145 if (length>0) {
147 if (outfile) {
148 if ((WriteAsync(outfile,buffer,length))==-1) goto failed;
150 else
152 if ((Write(outhandle,buffer,length))==-1) goto failed;
154 size_write+=length;
156 if (prog)
157 dotaskmsg(hotkeymsg_port,PROGRESS_UPDATE,size_read+size_write,size_total,NULL,1);
159 if (status_haveaborted) {
160 ret=-1;
161 goto failed;
165 if (infile) CloseAsync(infile);
166 else /*if (inhandle)*/ Close(inhandle);
168 if (outfile) CloseAsync(outfile);
170 /*if (outhandle)*/ Close(outhandle);
172 FreeMem(buffer,buffer_size);
174 if (config->copyflags&COPY_DATE) {
175 setdate(dst,&(cfinfo.fib_Date));
176 dsp=&cfinfo.fib_Date;
178 else {
179 DateStamp(&ds);
180 dsp=&ds;
182 copy_datestamp(dsp,&dos_copy_date);
184 if (config->copyflags&COPY_PROT)
185 SetProtection(dst,cfinfo.fib_Protection&((config->copyflags&COPY_COPYARC)?~0:~FIBF_ARCHIVE));
186 dos_copy_protection=cfinfo.fib_Protection;
188 if (config->copyflags&COPY_NOTE) {
189 SetComment(dst,cfinfo.fib_Comment);
190 strcpy(dos_copy_comment,cfinfo.fib_Comment);
192 else dos_copy_comment[0]=0;
194 return(1);
196 failed:
197 *err=IoErr();
198 if (buffer) FreeMem(buffer,buffer_size);
199 if (infile) CloseAsync(infile);
200 if (inhandle) Close(inhandle);
201 if (/* outfile || */ outhandle) {
203 if (outfile) CloseAsync(outfile);
204 else
206 Close(outhandle);
207 DeleteFile(dst);
209 return(ret);
212 struct Directory *checktot(dir)
213 struct DirectoryWindow *dir;
215 struct Directory *first;
217 if (!dir) return(NULL);
218 first=dir->firstentry;
219 while (first) {
220 if (first->selected && first->type<=ENTRY_FILE) break;
221 first=first->next;
223 return(first);
226 struct Directory *checkdirtot(dir)
227 struct DirectoryWindow *dir;
229 struct Directory *first;
231 if (!dir) return(NULL);
232 first=dir->firstentry;
233 while (first) {
234 if (first->selected && ENTRYTYPE(first->type)==ENTRY_DIRECTORY) break;
235 first=first->next;
237 return(first);
240 struct Directory *checkdevtot(dir)
241 struct DirectoryWindow *dir;
243 struct Directory *first;
245 if (!dir) return(NULL);
246 first=dir->firstentry;
247 while (first) {
248 if (first->selected && first->type==ENTRY_DEVICE) break;
249 first=first->next;
251 return(first);
254 struct Directory *checkalltot(dir)
255 struct DirectoryWindow *dir;
257 struct Directory *first;
259 if (!dir) return(NULL);
260 first=dir->firstentry;
261 while (first) {
262 if (first->selected && first->type!=ENTRY_DEVICE && first->type!=ENTRY_CUSTOM) break;
263 first=first->next;
265 return(first);
268 /* Enforcer HIT: with ARexx script sameselect.dopus */
270 struct Directory *findfile(dir,name,count)
271 struct DirectoryWindow *dir;
272 char *name;
273 int *count;
275 struct Directory *find;
277 char parsebuf[100];
279 LParsePatternI(name,parsebuf);
282 D(bug("findfile(%s)\n",name?name:"<NULL>"));
283 if (dir) {
284 if (str_arcorgname[0]) name=str_arcorgname; /* required for double-click */
286 find=dir->firstentry;
287 if (count) *count=0;
288 while (find) {
290 if (find->name && (LMatchPatternI(parsebuf,find->name))) return(find);
292 if (find->name && !(Stricmp(name,find->name))) return(find);
294 find=find->next;
295 if (count) ++(*count);
298 return(NULL);
301 int delfile(name,nam,errs,unprotect,errcheck)
302 char *name,*nam,*errs;
303 int unprotect,errcheck;
305 int suc,a,err,try=0,recplus=0;
306 char buf[300],buf2[100];
308 loop:
309 suc=DeleteFile(name);
310 if (!suc) {
311 if ((err=IoErr())==ERROR_OBJECT_NOT_FOUND) suc=1;
312 else {
313 if (err==ERROR_DIRECTORY_NOT_EMPTY) return(-2);
314 else if (err==ERROR_DELETE_PROTECTED && try==0) {
315 if (!(config->deleteflags&DELETE_SET)) {
316 if (!unprotect) {
317 doerror(ERROR_DELETE_PROTECTED);
318 geterrorstring(buf2,ERROR_DELETE_PROTECTED);
319 lsprintf(buf,globstring[STR_ERROR_OCCURED],globstring[STR_DELETING],nam,buf2);
320 strcat(buf,globstring[STR_SELECT_UNPROTECT]);
321 if (!(a=simplerequest(buf,globstring[STR_UNPROTECT],
322 globstring[STR_ABORT],globstring[STR_UNPROTECT_ALL],globstring[STR_SKIP],NULL)))
323 return(-1);
324 if (a==3) return(0);
325 if (a==2) recplus=1;
328 try=1;
329 SetProtection(name,0);
330 goto loop;
332 else if (!errcheck) return(-2);
333 doerror(err);
334 if ((a=checkerror(errs,nam,err))==3) return(-1);
335 if (a==1) goto loop;
338 else {
339 if (recplus) suc=2;
340 else suc=1;
342 return(suc);
345 int getwildrename(sname,dname,name,newn)
346 char *sname,*dname,*name,*newn;
348 // char sfirst[FILEBUF_SIZE],slast[FILEBUF_SIZE],dfirst[FILEBUF_SIZE],dlast[FILEBUF_SIZE],foon[FILEBUF_SIZE];
349 int a,b,c/*,flen,llen,d*/;
351 char *spat = sname, *dpat = dname, *sn = name, *dn = newn;
352 char c1,c2;
354 D(bug("getwildrename(%s,%s,%s,)\n",sname,dname,name));
355 /* check if filename matches source pattern */
356 a=1; b=0;
357 while (*spat)
359 if (*spat == '*')
361 b++;
362 spat++;
363 if (*spat) for (c1=ToLower(*spat); c1 != ToLower(*sn); sn++);
365 else
367 c1 = ToLower(*spat);
368 c2 = ToLower(*sn);
370 if (c1 == c2)
372 spat++;
373 sn++;
375 else
377 a=0;
378 break;
382 D(bug("getwildrename(): <%smatch>, %ld asterisks in source pattern\n",a?"":"no ",b));
384 /* count asterisks in destination pattern */
385 for(c = 0; *dpat; dpat++) if (*dpat == '*') c++;
386 D(bug("getwildrename(): %ld asterisks in destination pattern\n",c));
388 if (a && (b == c)) // try to build destination filename
390 spat = sname;
391 dpat = dname;
392 sn = name;
394 while (*spat || *dpat || *sn)
396 // skip to wildcard part of filename
397 if (*spat)
399 for (; *spat && (*spat != '*'); spat++, sn++);
400 if (*spat) spat++;
402 // copy replacement text
403 if (*dpat)
405 for (; *dpat && (*dpat != '*'); *dn++ = *dpat++);
406 if (*dpat) dpat++;
408 // copy wildcard part of source filename
409 if (*sn) for (c1=ToLower(*spat); c1 != ToLower(*sn); sn++) if (*spat != '*') *dn++ = *sn;
410 *dn = 0;
411 D(bug("getwildrename(): spat = %s, dpat = %s, sn = %s, destination name: %s\n",spat,dpat,sn,newn));
413 return 1;
416 b=strlen(sname); sfirst[0]=slast[0]=0;
417 for (a=0;a<b;a++)
418 if (sname[a]=='*') {
419 strcpy(sfirst,sname); sfirst[(flen=a)]=0;
420 strcpy(slast,(char *)&sname[a+1]);
421 llen=b-a;
422 break;
424 b=strlen(dname);
425 for (a=0;a<b;a++)
426 if (dname[a]=='*') {
427 strcpy(dfirst,dname); dfirst[a]=0;
428 strcpy(dlast,(char *)&dname[a+1]);
429 break;
431 a=strlen(sfirst); b=strlen(slast);
432 if ((!a || (Strnicmp(name,sfirst,flen))==0) &&
433 (!b || ((d=strlen(name))>=llen && (Stricmp(&name[(d-llen)+1],slast))==0))) {
434 c=strlen(name)-a-b;
435 CopyMem((char *)&name[a],foon,c);
436 foon[c]=0;
437 strcpy(newn,dfirst); strcat(newn,foon); strcat(newn,dlast);
438 D(bug("getwildrename(): newname = %s\n",newn));
439 if (newn[0]!=0) return(1);
442 return(0);
445 void filloutcopydata(dir)
446 struct Directory *dir;
448 dos_copy_date.ds_Days=dir->date.ds_Days;
449 dos_copy_date.ds_Minute=dir->date.ds_Minute;
450 dos_copy_date.ds_Tick=dir->date.ds_Tick;
451 dos_copy_protection=dir->protection;
452 if (dir->comment) strcpy(dos_copy_comment,dir->comment);
453 else dos_copy_comment[0]=0;
456 void filloutcopydatafile(fil)
457 char *fil;
459 struct FileInfoBlock __aligned fileinfo;
461 if (lockandexamine(fil,&fileinfo)) {
462 dos_copy_date.ds_Days=fileinfo.fib_Date.ds_Days;
463 dos_copy_date.ds_Minute=fileinfo.fib_Date.ds_Minute;
464 dos_copy_date.ds_Tick=fileinfo.fib_Date.ds_Tick;
465 dos_copy_protection=fileinfo.fib_Protection;
466 strcpy(dos_copy_comment,fileinfo.fib_Comment);
470 void update_buffer_stamp(win,true)
471 int win,true;
473 struct FileInfoBlock __aligned fib;
474 char dirbuf[256];
475 struct DirectoryWindow *dirwin;
477 if (win==-1 || !(config->dirflags&DIRFLAGS_REREADOLD)) return;
479 dirwin=dopus_curwin[win];
480 strcpy(dirbuf,str_pathbuffer[win]);
481 FOREVER {
482 if (true) {
483 if (lockandexamine(dirbuf,&fib))
484 copy_datestamp(&fib.fib_Date,&dirwin->dirstamp);
485 if (!(doparent(dirbuf)) ||
486 !(dirwin=findbuffer(dirbuf,win,0,1))) break;
488 else {
489 if (!(dirwin=dirwin->next) ||
490 dirwin==dopus_curwin[win]) break;
491 if (Strnicmp(dirwin->directory,dirbuf,strlen(dirbuf))==0) {
492 dirwin->dirstamp.ds_Days=0;
493 dirwin->dirstamp.ds_Minute=0;
494 dirwin->dirstamp.ds_Tick=0;
500 int check_key_press(func,code,qual)
501 struct dopusfunction *func;
502 UWORD code,qual;
504 if (!func->function ||
505 !func->function[0] ||
506 (func->key==0xff && func->qual==0) ||
507 // func->key==0 ||
508 func->qual!=qual) return(0);
509 if (func->key==0xff || func->key==code) return(1);
510 return(0);