home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / copy2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-02  |  2.7 KB  |  86 lines

  1. /* COPY2.C illustrates DOS file I/O and DOS memory allocation functions
  2.  * including:
  3.  *      _dos_open       _dos_close      _dos_creatnew   _dos_creat
  4.  *      _dos_read       _dos_write      _dos_allocmem   _dos_free
  5.  *
  6.  * See COPY1.C for another version of the copyfile function.
  7.  */
  8.  
  9. #include <dos.h>
  10. #include <fcntl.h>
  11. #include <conio.h>
  12. #include <stdio.h>
  13.  
  14. int copyfile( char *source, char *destin );     /* Prototype */
  15.  
  16. main( int argc, char *argv[] )
  17. {
  18.     if( argc == 3 )
  19.         if( copyfile( argv[1], argv[2] ) )
  20.             printf( "Copy failed\n" );
  21.         else
  22.             printf( "Copy successful\n" );
  23.     else
  24.         printf( "  SYNTAX: COPY2 <source> <target>\n" );
  25. }
  26.  
  27. /* Function to copy one file to another (both specified by path).
  28.  * Dynamically allocates memory for the file buffer. Prompts before
  29.  * overwriting existing file. Returns 0 if successful, or an error
  30.  * number if unsuccessful. This function uses dos functions only; no
  31.  * standard C library functions are used.
  32.  */
  33. #define EXIST 80
  34. enum ATTRIB { NORMAL, RDONLY, HIDDEN, SYSTEM = 4 };
  35. int copyfile( char *source, char *target )
  36. {
  37.     char far *buf = NULL;
  38.     static char prompt[] = "Target exists. Overwrite? ", newline[] = "\n\r";
  39.     int hsource, htarget, ch;
  40.     unsigned ret, segbuf, count;
  41.  
  42.     /* Attempt to dynamically allocate all of memory (0xffff paragraphs).
  43.      * This will fail, but will return the amount actually available
  44.      * in segbuf. Then allocate this amount.
  45.      */
  46.     _dos_allocmem( 0xffff, &segbuf );
  47.     count = segbuf;
  48.     if( ret = _dos_allocmem( count, &segbuf ) )
  49.         return ret;
  50.     FP_SEG( buf ) = segbuf;
  51.  
  52.     /* Open source file and create target, overwriting if necessary. */
  53.     if( ret = _dos_open( source, O_RDONLY, &hsource ) )
  54.         return ret;
  55.     ret = _dos_creatnew( target, NORMAL, &htarget );
  56.     if( ret == EXIST )
  57.     {
  58.         /* Use _dos_write to display prompts. Use bdos to call
  59.          * function 1 to get and echo keystroke.
  60.          */
  61.         _dos_write( 1, prompt, sizeof( prompt ) - 1, &ch );
  62.         ch = bdos( 1, 0, 0 ) & 0x00ff;
  63.         if( (ch == 'y') || (ch == 'Y') )
  64.             ret = _dos_creat( target, NORMAL, &htarget );
  65.         _dos_write( 1, newline, sizeof( newline ) - 1, &ch );
  66.     }
  67.     if( ret )
  68.         return ret;
  69.  
  70.     /* Read and write until there is nothing left. */
  71.     while( count )
  72.     {
  73.         /* Read and write input. */
  74.         if( (ret = _dos_read( hsource, buf, count, &count )) )
  75.             return ret;
  76.         if( (ret = _dos_write( htarget, buf, count, &count )) )
  77.             return ret;
  78.     }
  79.  
  80.     /* Close files and free memory. */
  81.     _dos_close( hsource );
  82.     _dos_close( htarget );
  83.     _dos_freemem( segbuf );
  84.     return 0;
  85. }
  86.