home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / DOSCOPY.C < prev    next >
C/C++ Source or Header  |  1992-04-20  |  2KB  |  56 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  : 0                               *
  10.  *                                                 *
  11.  *      By       :  Peter Yard (29 May 1991)       *
  12.  ***************************************************/
  13.  
  14. #include <stdio.h>
  15.  
  16. #define STDOUT fileno(stdout)
  17.  
  18. int copy(char *fromDir, char *fname, char *toDir)
  19. {
  20.       FILE    *nul;       /* nul will redirect stdout to DOS 'nul' */
  21.       char    from[FILENAME_MAX], to[FILENAME_MAX], comd[128];
  22.       int     bytesRead, oldStdout;
  23.  
  24.       /* Create the strings to describe the paths                  */
  25.  
  26.       make_path(from, fromDir, fname);
  27.       make_path(to, toDir, fname);
  28.  
  29.       /* Construct 'comd' string which is a dos command for a copy */
  30.  
  31.       strcpy(comd, "copy ");
  32.       strcat(comd, from); strcat(comd, " ");
  33.       strcat(comd, to);
  34.  
  35.       /* Redirect stdout to a nul file, kills output to the screen */
  36.  
  37.       nul = fopen("NUL", "w");
  38.       oldStdout = dup(STDOUT);
  39.       dup2(fileno(nul), STDOUT);
  40.       close(fileno(nul));
  41.  
  42.       system(comd);           /* COPY file */
  43.  
  44.       /* Restore stdout and close nul file */
  45.  
  46.       dup2(oldStdout, STDOUT);
  47.       close(oldStdout);
  48.  
  49.       /* Display file source and target,      */
  50.       /* otherwise comment out the next line. */
  51.  
  52.       printf("\n%s copied to %s",from,to);
  53.  
  54.       return 0;
  55. }
  56.