summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/flopcopy.c
blob: 18b16d7f250e43e26a6e493be98ede499597a689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
**  FLOPCOPY.C
**
**  Copy a floppy to a hard disk directory with directory recursion
**  Public domain, uses functions from SNIPPETS.
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <io.h>
#include <fcntl.h>

#ifdef __TURBOC__
 #include <dir.h>
 #include <dos.h>
 #define _dos_findfirst(f,a,b) findfirst(f,b,a)
 #define _dos_findnext(b) findnext(b)
 #define find_t ffblk
 #define _A_SUBDIR FA_DIREC
 #define attrib ff_attrib
 #define name ff_name
#else
 #include <direct.h>
 #ifdef __ZTC__
  #include <dos.h>
  #ifndef _A_SUBDIR
   #define _A_SUBDIR FA_DIREC
  #endif
 #else                   /* assume MSC/QC                                */
  #include <dos.h>
  #include <errno.h>
  #include <sys\types.h>
 #endif
#endif

#include <sys\stat.h>

#undef SUCCESS
#define SUCCESS 0

#undef NUL
#define NUL '\0'

#define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])

int file_copy(char *,char *);                   /* In Wb_Fcopy.C  */
void do_dir(char *, char *);

/*
**  Copy a floppy to an HD subdirectory
*/

int main(int argc, char *argv[])
{
      char fdrv[4] = "A:\\", target[FILENAME_MAX];

      if (3 > argc)
      {
            puts("Usage: FLOPCOPY drive_letter subdir");
            puts("where: drive_letter is \"A\" or \"B\" (colon optional)");
            puts("       subdir is drive:dir target, e.g. \"C:\\FLOPSTUF\"");
            return EXIT_FAILURE;
      }
      *fdrv = *argv[1];
      strcpy(target, argv[2]);
      if ('\\' != LAST_CHAR(target))
            strcat(target, "\\");
      
      do_dir(fdrv, target);
}

/*
**  Process a directory (SNIPPETS: Treedir.C, modified)
*/

void do_dir(char *from, char *to)
{
      char search[FILENAME_MAX], new[FILENAME_MAX], newto[FILENAME_MAX];
      struct find_t ff;

      strcat(strcpy(search, from), "*.*");
      if (SUCCESS == _dos_findfirst(to, 0xff, &ff))
      {
            if (0 == (ff.attrib & _A_SUBDIR))
            {
                  printf("*** %s Exists and is not a directory!\n", to);
                  return;
            }
      }
      else
      {
            strcpy(newto, to);
            if ('\\' == LAST_CHAR(newto))
                  LAST_CHAR(newto) = NUL;
            mkdir(newto);
      }
      if (SUCCESS == _dos_findfirst(search, 0xff, &ff)) do
      {
            if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
            {
                  strcat(strcat(strcpy(new, from), ff.name), "\\");
                  strcat(strcat(strcpy(newto, to), ff.name), "\\");
                  do_dir(new, newto);
            }
            else
            {
                  char file1[FILENAME_MAX], file2[FILENAME_MAX];

                  if ((ff.attrib & (_A_SUBDIR | _A_VOLID)) || '.' == *ff.name)
                        continue;
                  strcat(strcpy(file1, from), ff.name);
                  strcat(strcpy(file2, to), ff.name);
                  if (SUCCESS != file_copy(file1, file2))
                        printf("*** Unable to copy %s to %s\n", file1, file2);
                  else  printf("Copied %s to %s\n", file1, file2);
            }
      } while (SUCCESS == _dos_findnext(&ff));
}