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

  1. /***************************************************
  2.  *      function : copy                            *
  3.  *      purpose  : copy one file                   *
  4.  *                                                 *
  5.  *      arguments: path to source 'fromDir',       *
  6.  *                 path to target 'toDir',         *
  7.  *                 filename to copy 'fname'        *
  8.  *                                                 *
  9.  *      returns  : nothing                         *
  10.  *                                                 *
  11.  *      By       :  Peter Yard (29 May 1991)       *
  12.  ***************************************************/
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <io.h>
  18.  
  19. void pmerge(char *path, char *drive, char *dir, char *fname, char *ext);
  20.  
  21. #define STDOUT fileno(stdout)
  22.  
  23. void copy(char *fromDir, char *fname, char *toDir)
  24. {
  25.       FILE    *nul;       /* nul will redirect stdout to DOS 'nul' */
  26.       char    from[FILENAME_MAX], to[FILENAME_MAX], comd[128];
  27.       int     bytesRead, oldStdout;
  28.  
  29.       /* Create the strings to describe the paths                  */
  30.  
  31.       pmerge(from, NULL, fromDir, fname, NULL);
  32.       pmerge(to, NULL, toDir, fname, NULL);
  33.  
  34.       /* Construct 'comd' string which is a dos command for a copy */
  35.  
  36.       strcpy(comd, "copy ");
  37.       strcat(comd, from); strcat(comd, " ");
  38.       strcat(comd, to);
  39.  
  40.       /* Redirect stdout to a nul file, kills output to the screen */
  41.  
  42.       nul = fopen("NUL", "w");
  43.       oldStdout = dup(STDOUT);
  44.       dup2(fileno(nul), STDOUT);
  45.       fclose(nul);
  46.  
  47.       system(comd);           /* COPY file */
  48.  
  49.       /* Restore stdout and close nul file */
  50.  
  51.       dup2(oldStdout, STDOUT);
  52.       close(oldStdout);
  53.  
  54.       /* Display file source and target,      */
  55.       /* otherwise comment out the next line. */
  56.  
  57.       printf("\n%s copied to %s",from,to);
  58. }
  59.