home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
BOUNDUTI.ZIP
/
MV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-16
|
2KB
|
101 lines
/*
-----------------------------
mv.c - file move facility
-----------------------------
written 8/15/90 by M. Mackey
*/
#define INCL_DOS
#include <os2.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <io.h>
#define ATTR 0x0021
#define LENGTH sizeof(buff)
static void error(char *fmt,char *arg1);
static void MoveFile(char *Oldpath,char *NewPath,char *FileName);
void main(int argc,char *argv[])
{
char OldPath[_MAX_PATH];
char NewPath[_MAX_PATH];
char buf[10];
char *LastSlash;
struct _FILEFINDBUF buff;
unsigned handle=0xffff,count=1,rc,index;
if(argc!=3)
error("Syntax: mv pathname1 pathname2",NULL);
strcpy(NewPath,argv[2]);
if(NewPath[strlen(NewPath)-1]!='\\')
strcat(NewPath,"\\");
strcpy(OldPath,argv[1]);
if(rc=DosFindFirst(OldPath,&handle,ATTR,&buff,LENGTH,&count,0L))
if(rc==18)
error("mv: file %s not found\n",OldPath);
else
error("mv: internal error - DosFindFirst() #%s\n",itoa(rc,buf,10));
if(LastSlash=strrchr(OldPath,'\\'))
OldPath[LastSlash-OldPath+1]='\0';
else
strcpy(OldPath,"");
MoveFile(OldPath,NewPath,buff.achName);
while(-1) {
DosFindNext(handle,&buff,LENGTH,&count);
if(!count)
break;
MoveFile(OldPath,NewPath,buff.achName);
}
DosFindClose(handle);
exit(0);
}
void MoveFile(char *OldPath,char *NewPath,char *FileName)
{
unsigned rc,attr,FileSaved=0;
char buf1[_MAX_PATH],buf2[_MAX_PATH],tmpfile[]="MVXXXXXX";
strcpy(buf1,NewPath);
strcat(buf1,FileName);
strcpy(buf2,OldPath);
strcat(buf2,FileName);
if(!DosQFileMode(buf1,&attr,0L)) {
printf("Remove %s (y/n)",buf1);
if(getche()=='n') {
printf("\n");
return;
} else {
if(!mktemp(tmpfile))
error("mv: cannot make temporary file %s",tmpfile);
FileSaved=1;
DosMove(buf1,tmpfile,0L);
}
}
printf(" Moving %s to %s\n",buf2,buf1);
if(rc=DosMove(buf2,buf1,0L)) {
if(FileSaved) {
DosMove(tmpfile,buf1,0L);
FileSaved=0;
}
if(rc==2)
printf("mv: no such file\n");
else if(rc==3 || rc==5)
printf("mv: access denied\n");
else if(rc==17)
printf("mv: cannot move to another drive\n");
else
printf("mv: unknown error - DosMove() #%s\n",itoa(rc,buf1,10));
}
if(FileSaved)
DosDelete(tmpfile,0L);
}
void error(char *fmt,char *arg1)
{
printf(fmt, arg1);
exit(1);
}