home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / move.zip / mov.c next >
C/C++ Source or Header  |  1994-12-18  |  3KB  |  140 lines

  1. #define INCL_DOS
  2.  
  3. #include <os2.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. /*
  9.  * Public domain from M. Kimes
  10.  *
  11.  * Compiled with IBM CSet/2 as:
  12.  *   ICC /G4 /O+ /Gs+ /W3 /Kb /Rn /B"/RUNFROMVDM" /B"/STACK:16384" mov.c
  13.  */
  14.  
  15.  
  16. VOID ShowHelp (APIRET rc) {
  17.  
  18.   static RESULTCODES rt;
  19.   static CHAR        object[32],runme[CCHMAXPATH];
  20.  
  21.   sprintf(runme,"CMD.EXE /C HELP.CMD SYS%04u",rc);
  22.   runme[strlen(runme) + 1] = 0;
  23.   runme[7] = 0;
  24.   DosExecPgm(object,sizeof(object),EXEC_SYNC,(PVOID)runme,NULL,&rt,(PSZ)runme);
  25. }
  26.  
  27.  
  28. INT to_upper (INT key) {
  29.  
  30.   if(key >= 'a' && key <= 'z')
  31.     return ((key) + 'A' - 'a');
  32.   return key;
  33. }
  34.  
  35.  
  36. BOOL stri_cmp (CHAR *a,CHAR *b) {
  37.  
  38.   while(*a && *b) {
  39.     if(to_upper(*a) != to_upper(*b))
  40.       return FALSE;
  41.     a++;
  42.     b++;
  43.   }
  44.   if(*a || *b)
  45.     return FALSE;
  46.   return TRUE;
  47. }
  48.  
  49.  
  50. int main (int argc,char *argv[]) {
  51.  
  52.   static CHAR        from[CCHMAXPATH * 2],to[CCHMAXPATH * 2];
  53.   static BOOL        overwrite = FALSE;
  54.   static FILESTATUS3 fs3f,fs3d;
  55.   CHAR              *p;
  56.   APIRET             rc;
  57.  
  58.   if(argc >= 2) {
  59.     if(argc > 3) {
  60.       if((*argv[3] == '/' || *argv[3] == '-') && to_upper(argv[3][1]) == 'O')
  61.         overwrite = TRUE;
  62.       else {
  63.         printf("**\07Unknown third argument \"%s\" ignored.\n",argv[3]);
  64.         DosSleep(1000L);
  65.       }
  66.     }
  67.     p = argv[1];
  68.     while(*p) {
  69.       if(*p == '/')
  70.         *p = '\\';
  71.       p++;
  72.     }
  73.     if(argc > 2) {
  74.       p = argv[2];
  75.       while(*p) {
  76.         if(*p == '/')
  77.           *p = '\\';
  78.         p++;
  79.       }
  80.     }
  81.     rc = DosQueryPathInfo(argv[1],FIL_QUERYFULLNAME,from,sizeof(from));
  82.     if(!rc) {
  83.       if(argc > 2)
  84.         rc = DosQueryPathInfo(argv[2],FIL_QUERYFULLNAME,to,sizeof(to));
  85.       else
  86.         rc = DosQueryPathInfo(".",FIL_QUERYFULLNAME,to,sizeof(to));
  87.       if(!rc) {
  88.         rc = DosQueryPathInfo(from,FIL_STANDARD,&fs3f,sizeof(fs3f));
  89.         if(!rc) {
  90.           rc = DosQueryPathInfo(to,FIL_STANDARD,&fs3d,sizeof(fs3d));
  91.           if(!rc) {
  92.             if(!(fs3f.attrFile & FILE_DIRECTORY) &&
  93.                (fs3d.attrFile & FILE_DIRECTORY)) {
  94.               p = strrchr(from,'\\');
  95.               if(p && *(p + 1)) {
  96.                 p++;
  97.                 if(to[strlen(to) - 1] != '\\')
  98.                   strcat(to,"\\");
  99.                 strcat(to,p);
  100.               }
  101.             }
  102.           }
  103.         }
  104.         printf("MOVE%s %s -> %s\n",((overwrite) ? "OVER" : ""),from,to);
  105.         if((!overwrite || stri_cmp(from,to)) &&
  106.            to_upper(*from) == to_upper(*to)) {
  107.           rc = DosMove(from,to);
  108.           if(rc) {
  109.             ShowHelp(rc);
  110.             return rc;
  111.           }
  112.         }
  113.         else {
  114.           rc = DosCopy(from,to,((overwrite) ? DCPY_EXISTING : 0));
  115.           if(rc) {
  116.             ShowHelp(rc);
  117.             return rc;
  118.           }
  119.           else if(!stri_cmp(from,to))
  120.             DosDelete(from);
  121.         }
  122.       }
  123.       else {
  124.         ShowHelp(rc);
  125.         return rc;
  126.       }
  127.     }
  128.     else {
  129.       ShowHelp(rc);
  130.       return rc;
  131.     }
  132.   }
  133.   else
  134.     printf("\n Usage:  MOV source target [/o[verwrite]]\n"
  135.            "\n Same basic format as OS/2's MOVE command except it can move between\n"
  136.            " different drives and can allow overwriting.\n"
  137.            "\nHector wuz here.\n");
  138.   return 0;
  139. }
  140.