home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / MV.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  135 lines

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