home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / BOUNDUTI.ZIP / MV.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  2KB  |  101 lines

  1. /*
  2.     -----------------------------
  3.       mv.c - file move facility
  4.     -----------------------------
  5.  
  6.     written 8/15/90 by M. Mackey
  7. */
  8. #define INCL_DOS
  9.  
  10. #include <os2.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <conio.h>
  15. #include <io.h>
  16.  
  17. #define ATTR 0x0021
  18. #define LENGTH sizeof(buff)
  19.  
  20. static void error(char *fmt,char *arg1);
  21. static void MoveFile(char *Oldpath,char *NewPath,char *FileName);
  22.  
  23. void main(int argc,char *argv[])
  24. {
  25.     char OldPath[_MAX_PATH];
  26.     char NewPath[_MAX_PATH];
  27.     char buf[10];
  28.     char *LastSlash;
  29.     struct _FILEFINDBUF buff;
  30.     unsigned handle=0xffff,count=1,rc,index;
  31.  
  32.     if(argc!=3)
  33.         error("Syntax: mv pathname1 pathname2",NULL);
  34.     strcpy(NewPath,argv[2]);
  35.     if(NewPath[strlen(NewPath)-1]!='\\')
  36.         strcat(NewPath,"\\");
  37.     strcpy(OldPath,argv[1]);
  38.     if(rc=DosFindFirst(OldPath,&handle,ATTR,&buff,LENGTH,&count,0L))
  39.         if(rc==18)
  40.             error("mv: file %s not found\n",OldPath);
  41.         else
  42.             error("mv: internal error - DosFindFirst() #%s\n",itoa(rc,buf,10));
  43.     if(LastSlash=strrchr(OldPath,'\\'))
  44.         OldPath[LastSlash-OldPath+1]='\0';
  45.     else
  46.         strcpy(OldPath,"");
  47.     MoveFile(OldPath,NewPath,buff.achName);
  48.     while(-1) {
  49.         DosFindNext(handle,&buff,LENGTH,&count);
  50.         if(!count)
  51.             break;
  52.         MoveFile(OldPath,NewPath,buff.achName);
  53.     }
  54.     DosFindClose(handle);
  55.     exit(0);
  56. }
  57. void MoveFile(char *OldPath,char *NewPath,char *FileName)
  58. {
  59.     unsigned rc,attr,FileSaved=0;
  60.     char buf1[_MAX_PATH],buf2[_MAX_PATH],tmpfile[]="MVXXXXXX";
  61.  
  62.     strcpy(buf1,NewPath);
  63.     strcat(buf1,FileName);
  64.     strcpy(buf2,OldPath);
  65.     strcat(buf2,FileName);
  66.     if(!DosQFileMode(buf1,&attr,0L)) {
  67.         printf("Remove %s (y/n)",buf1);
  68.         if(getche()=='n') {
  69.             printf("\n");
  70.             return;
  71.         } else {
  72.             if(!mktemp(tmpfile))
  73.                 error("mv: cannot make temporary file %s",tmpfile);
  74.             FileSaved=1;
  75.             DosMove(buf1,tmpfile,0L);
  76.         }
  77.     }
  78.     printf("  Moving %s to %s\n",buf2,buf1);
  79.     if(rc=DosMove(buf2,buf1,0L)) {
  80.         if(FileSaved) {
  81.             DosMove(tmpfile,buf1,0L);
  82.             FileSaved=0;
  83.         }
  84.         if(rc==2)
  85.             printf("mv: no such file\n");
  86.         else if(rc==3 || rc==5)
  87.             printf("mv: access denied\n");
  88.         else if(rc==17)
  89.             printf("mv: cannot move to another drive\n");
  90.         else
  91.             printf("mv: unknown error - DosMove() #%s\n",itoa(rc,buf1,10));
  92.     }
  93.     if(FileSaved)
  94.         DosDelete(tmpfile,0L);
  95. }
  96. void error(char *fmt,char *arg1)
  97. {
  98.     printf(fmt, arg1);
  99.     exit(1);
  100. }
  101.