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

  1. /*
  2. **  FLOPCOPY.C
  3. **
  4. **  Copy a floppy to a hard disk directory with directory recursion
  5. **  Public domain, uses functions from SNIPPETS.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <assert.h>
  12. #include <io.h>
  13. #include <fcntl.h>
  14.  
  15. #ifdef __TURBOC__
  16.  #include <dir.h>
  17.  #include <dos.h>
  18.  #define _dos_findfirst(f,a,b) findfirst(f,b,a)
  19.  #define _dos_findnext(b) findnext(b)
  20.  #define find_t ffblk
  21.  #define _A_SUBDIR FA_DIREC
  22.  #define attrib ff_attrib
  23.  #define name ff_name
  24. #else
  25.  #include <direct.h>
  26.  #ifdef __ZTC__
  27.   #include <dos.h>
  28.   #ifndef _A_SUBDIR
  29.    #define _A_SUBDIR FA_DIREC
  30.   #endif
  31.  #else                   /* assume MSC/QC                                */
  32.   #include <dos.h>
  33.   #include <errno.h>
  34.   #include <sys\types.h>
  35.  #endif
  36. #endif
  37.  
  38. #include <sys\stat.h>
  39.  
  40. #undef SUCCESS
  41. #define SUCCESS 0
  42.  
  43. #undef NUL
  44. #define NUL '\0'
  45.  
  46. #define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
  47.  
  48. int file_copy(char *,char *);                   /* In Wb_Fcopy.C  */
  49. void do_dir(char *, char *);
  50.  
  51. /*
  52. **  Copy a floppy to an HD subdirectory
  53. */
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57.       char fdrv[4] = "A:\\", target[FILENAME_MAX];
  58.  
  59.       if (3 > argc)
  60.       {
  61.             puts("Usage: FLOPCOPY drive_letter subdir");
  62.             puts("where: drive_letter is \"A\" or \"B\" (colon optional)");
  63.             puts("       subdir is drive:dir target, e.g. \"C:\\FLOPSTUF\"");
  64.             return EXIT_FAILURE;
  65.       }
  66.       *fdrv = *argv[1];
  67.       strcpy(target, argv[2]);
  68.       if ('\\' != LAST_CHAR(target))
  69.             strcat(target, "\\");
  70.       
  71.       do_dir(fdrv, target);
  72. }
  73.  
  74. /*
  75. **  Process a directory (SNIPPETS: Treedir.C, modified)
  76. */
  77.  
  78. void do_dir(char *from, char *to)
  79. {
  80.       char search[FILENAME_MAX], new[FILENAME_MAX], newto[FILENAME_MAX];
  81.       struct find_t ff;
  82.  
  83.       strcat(strcpy(search, from), "*.*");
  84.       if (SUCCESS == _dos_findfirst(to, 0xff, &ff))
  85.       {
  86.             if (0 == (ff.attrib & _A_SUBDIR))
  87.             {
  88.                   printf("*** %s Exists and is not a directory!\n", to);
  89.                   return;
  90.             }
  91.       }
  92.       else
  93.       {
  94.             strcpy(newto, to);
  95.             if ('\\' == LAST_CHAR(newto))
  96.                   LAST_CHAR(newto) = NUL;
  97.             mkdir(newto);
  98.       }
  99.       if (SUCCESS == _dos_findfirst(search, 0xff, &ff)) do
  100.       {
  101.             if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
  102.             {
  103.                   strcat(strcat(strcpy(new, from), ff.name), "\\");
  104.                   strcat(strcat(strcpy(newto, to), ff.name), "\\");
  105.                   do_dir(new, newto);
  106.             }
  107.             else
  108.             {
  109.                   char file1[FILENAME_MAX], file2[FILENAME_MAX];
  110.  
  111.                   if ((ff.attrib & (_A_SUBDIR | _A_VOLID)) || '.' == *ff.name)
  112.                         continue;
  113.                   strcat(strcpy(file1, from), ff.name);
  114.                   strcat(strcpy(file2, to), ff.name);
  115.                   if (SUCCESS != file_copy(file1, file2))
  116.                         printf("*** Unable to copy %s to %s\n", file1, file2);
  117.                   else  printf("Copied %s to %s\n", file1, file2);
  118.             }
  119.       } while (SUCCESS == _dos_findnext(&ff));
  120. }
  121.