home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- #include <sys\stat.h>
- #include <dos.h>
-
-
- #define BUFF_SIZE 20480 /* size of buffer for FileCopy() */
-
- main(int argc, char *argv[])
- {
- if (argc != 3)
- {
- printf("USAGE: FILECOPY source destination\n\n");
- printf("\tCopies source file to destination file.\n");
- exit(1);
- }
- FileCopy(argv[1],argv[2]);
- }
-
-
-
- /* returns 1 on error, else returns 0 */
- int FileCopy(char *file1, char *file2)
- {
- int SourceFile, DestFile, BytesRead;
- int error;
- char *buffer;
- unsigned fdate, ftime;
-
-
- buffer = (char *)malloc(BUFF_SIZE);
- if (!buffer)
- {
- printf("Can't get memory!\n");
- return(1);
- }
-
- SourceFile = open(file1, O_RDONLY|O_BINARY);
- if (SourceFile == -1)
- {
- printf("%s could not be opened!\n", file1);
- free(buffer);
- return(1);
- }
- DestFile = open(file2, O_RDWR|O_BINARY|O_CREAT, S_IWRITE);
- if (DestFile == -1)
- {
- printf("%s could not be opened!\n", file2);
- close(SourceFile);
- free(buffer);
- return(1);
- }
-
- while (1)
- {
- BytesRead = read(SourceFile, buffer, BUFF_SIZE);
- if (BytesRead == -1)
- {
- puts("Read Error");
- close(SourceFile);
- close(DestFile);
- free(buffer);
- return(1);
- }
- if (BytesRead == 0)
- break;
-
- error = write(DestFile, buffer, BytesRead);
- if (error == -1)
- {
- puts("Write Error");
- close(SourceFile);
- close(DestFile);
- free(buffer);
- return(1);
- }
- }
-
- dos_getftime(SourceFile, &fdate, &ftime);
- dos_setftime(DestFile, fdate, ftime);
- close(SourceFile);
- close(DestFile);
- free(buffer);
- return(0);
- }
-
-