home *** CD-ROM | disk | FTP | other *** search
- /*
- move - Written by S.Klemola. (C)1993,1994 SH-Ware.
- */
-
- #include "exec/types.h"
- #include "exec/nodes.h"
- #include "exec/libraries.h"
- #include "libraries/dos.h"
-
- #define BUFSIZE 16384
-
- extern APTR AllocMem(long,long);
- extern char *FilePart(APTR);
- extern struct Library *SysBase;
- void Fatal(),error(),cleanup();
-
- APTR mem;
- BPTR srcf,destf,lock;
- long flen;
-
- char *cmd;
- char *Template = "SOURCE/A,DESTINATION/A\n";
- char *Version = "Move v1.02 for V34 & V36 by S.Klemola. (C)1994 SH-Ware.\n";
-
- char *Error[] = {
- "required argument missing",
- "can't open source file",
- "can't open destination file",
- "can't allocate memory"
- };
-
- char Buffer[64];
-
- int main(argc,argv) int argc; char *argv[]; {
- long rlen,ProtectionBits;
- char temp;
-
- cmd=argv[0];
-
- if(!strcmp(argv[1],"?")) {
- printf(Template);
- exit(RETURN_OK);
- };
-
- if(!strcmp(argv[1],"??")) {
- printf(Version);
- exit(RETURN_OK);
- };
-
- if(argc<3) error(0);
-
- strcpy(Buffer,argv[2]);
-
- if((SysBase->lib_Version)>=36) if(FilePart(Buffer)[0]==0) {
- AddPart(Buffer,FilePart(argv[1]),64);
- temp++;
- };
-
- if(!(srcf=Open(argv[1],MODE_OLDFILE))) error(1);
- if(destf=Open(argv[2],MODE_OLDFILE)) {
- printf("Can't move %s as %s because object already exists\n",
- argv[1],Buffer);
- cleanup();
- exit(20);
- };
- if(!(destf=Open(Buffer,MODE_NEWFILE))) error(2);
-
- if(temp) printf("Moving %s as %s\n",argv[1],Buffer);
-
- Seek(srcf,0,OFFSET_END);
- flen=Seek(srcf,0,OFFSET_BEGINNING);
-
- if(!(mem=AllocMem(BUFSIZE,0))) error(3);
-
- lock=Lock(argv[1],SHARED_LOCK);
- Examine(lock,mem);
- UnLock(lock);
-
- ProtectionBits=((struct FileInfoBlock *)mem)->fib_Protection;
-
- while(flen>0) {
- rlen=BUFSIZE;
- if(flen<rlen) rlen=flen;
-
- if(Read(srcf,mem,rlen)<rlen) {
- printf("Error in reading\n");
- Fatal(Buffer);
- };
-
- if(Write(destf,mem,rlen)<rlen) {
- printf("Error in writing\n");
- Fatal(Buffer);
- };
-
- flen=flen-rlen;
- };
-
- cleanup();
- SetProtection(argv[1],0);
- DeleteFile(argv[1]);
- SetProtection(argv[2],ProtectionBits);
- return(RETURN_OK);
- };
-
- void Fatal(FileName) char *FileName; {
- printf("Destination file ""%s"" removed.\n",FileName);
- cleanup();
- DeleteFile(FileName);
- exit(RETURN_FAIL);
- };
-
- void error(err) int err; {
- printf("%s: %s\n",cmd,Error[err]);
- cleanup();
- exit(RETURN_FAIL);
- };
-
- void cleanup() {
- if(srcf) Close(srcf);
- if(destf) Close(destf);
- if(mem) FreeMem(mem,BUFSIZE);
- };
-
-