home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / MV.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  3KB  |  137 lines

  1. /*
  2. ** mv.c -- move or rename files or directories  
  3. ** updated for multiple files, 5 jul 92, rlm    
  4. ** placed in the public domain via C_ECHO by the author, Ray McVay
  5. **
  6. ** modified by Bob Stout, 28 Mar 93
  7. ** modified by Bob Stout,  4 Jun 93
  8. **
  9. ** uses file_copy from SNIPPETS file WB_FCOPY.C
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <dos.h>
  16.  
  17. /* For portability, make everything look like MSC 6 */
  18.  
  19. #if defined(__TURBOC__)
  20.  #include <dir.h>
  21.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  22.  #define find_t ffblk
  23.  #define _A_SUBDIR FA_DIREC
  24.  #define attrib ff_attrib
  25. #else                   /* assume MSC/QC                                */
  26.  #include <direct.h>
  27. #endif
  28.  
  29. /*
  30. **  Tell 'em they messed up
  31. */
  32.  
  33. void help(char *s)
  34. {
  35.       puts("usage: mv <oldname [...]> <newname|newdir>");
  36.       printf("error: %s\n", s);
  37. }
  38.  
  39. /*
  40. **  Simple directory test
  41. */
  42.  
  43. isdir(char *path)
  44. {
  45.       struct  find_t f;
  46.  
  47.       /* "Raw" drive specs are always directories     */
  48.  
  49.       if (':' == path[1] && '\0' == path[2])
  50.             return 1;
  51.  
  52.       return (_dos_findfirst(path, _A_SUBDIR, &f) == 0 &&
  53.             (f.attrib & _A_SUBDIR));
  54. }
  55.  
  56. /*
  57. **  Use rename or copy and delete
  58. */
  59.  
  60. int mv(char *src, char *dest)
  61. {
  62.       int errcount = 0;
  63.       char buf[FILENAME_MAX];
  64.       const char *generr = "ERROR: mv - couldn't %s %s %s\n";
  65.  
  66.       if (':' == dest[1] && *dest != *getcwd(buf, FILENAME_MAX))
  67.       {
  68.             if (file_copy(src, dest))
  69.             {
  70.                   printf(generr, "move", src, dest);
  71.                   ++errcount;
  72.             }
  73.             else if (unlink(src))
  74.             {
  75.                   printf(generr, "delete", src, "");
  76.                   ++errcount;
  77.             }
  78.       }
  79.       else
  80.       {
  81.             if (rename(src, dest))
  82.             {
  83.                   printf(generr, "rename", src, dest);
  84.                   ++errcount;
  85.             }
  86.       }
  87.       return errcount;
  88. }
  89.  
  90. /*
  91. **  Enter here
  92. */
  93.  
  94. int main(int argc, char **argv)
  95. {
  96.       int src, errcount = 0;
  97.       char target[FILENAME_MAX];
  98.  
  99.       puts("mv 1.3 (4 jun 93) - Ray L. McVay/Bob Stout");
  100.       if (argc < 3)
  101.             help("Not enough parameters");
  102.  
  103.       /*
  104.       **  Handle cases where target is a directory
  105.       */
  106.  
  107.       else if (isdir(argv[argc -1]))
  108.       {
  109.             for (src = 1; src < argc - 1; src++)
  110.             {
  111.                   char termch;
  112.  
  113.                   strcpy(target, argv[argc - 1]);
  114.                   termch = target[strlen(target) - 1];
  115.                   if ('\\' != termch && ':' != termch)
  116.                         strcat(target, "\\");
  117.  
  118.                   if (strrchr(argv[src], '\\'))
  119.                         strcat(target, strrchr(argv[src], '\\') + 1);
  120.                   else if (argv[src][1] == ':')
  121.                         strcat(target, argv[src] + 2);
  122.                   else  strcat(target, argv[src]);
  123.  
  124.                   errcount += mv(argv[src], target);
  125.             }
  126.       }
  127.  
  128.       /*
  129.       **  Nothing left except 2 explicit file names
  130.       */
  131.  
  132.       else if (argc == 3)
  133.             errcount += mv(argv[1], argv[2]);
  134.  
  135.       return errcount;
  136. }
  137.