summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/doscopy.c
blob: 2725c60880f4da654d88b655b139fdcc81241c02 (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
/***************************************************
 *      function : copy                            *
 *      purpose  : copy one file                   *
 *                                                 *
 *      arguments: path to source 'fromDir',       *
 *                 path to target 'toDir',         *
 *                 filename to copy 'fname'        *
 *                                                 *
 *      returns  : nothing                         *
 *                                                 *
 *      By       :  Peter Yard (29 May 1991)       *
 ***************************************************/

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

void pmerge(char *path, char *drive, char *dir, char *fname, char *ext);

#define STDOUT fileno(stdout)

void copy(char *fromDir, char *fname, char *toDir)
{
      FILE    *nul;       /* nul will redirect stdout to DOS 'nul' */
      char    from[FILENAME_MAX], to[FILENAME_MAX], comd[128];
      int     bytesRead, oldStdout;

      /* Create the strings to describe the paths                  */

      pmerge(from, NULL, fromDir, fname, NULL);
      pmerge(to, NULL, toDir, fname, NULL);

      /* Construct 'comd' string which is a dos command for a copy */

      strcpy(comd, "copy ");
      strcat(comd, from); strcat(comd, " ");
      strcat(comd, to);

      /* Redirect stdout to a nul file, kills output to the screen */

      nul = fopen("NUL", "w");
      oldStdout = dup(STDOUT);
      dup2(fileno(nul), STDOUT);
      fclose(nul);

      system(comd);           /* COPY file */

      /* Restore stdout and close nul file */

      dup2(oldStdout, STDOUT);
      close(oldStdout);

      /* Display file source and target,      */
      /* otherwise comment out the next line. */

      printf("\n%s copied to %s",from,to);
}