home *** CD-ROM | disk | FTP | other *** search
/ Boston 2 / boston-2.iso / DOS / PROGRAM / C / FCOPY / COPYTEST.C < prev    next >
C/C++ Source or Header  |  1993-12-01  |  3KB  |  75 lines

  1. /*  This is a short program demonstrating the fcopy function.  It
  2.     prompts the user for source and target filenames and then performs
  3.     the copy.  If an error occurred during the copy operation, an error
  4.     message is displayed.
  5.  
  6.     Written by Ray Waters */
  7.  
  8. #include <io.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "fcopy.h"
  13.  
  14. main()
  15. {
  16.     char SrcName[80];                   /* buffer for source filename */
  17.     char TgtName[80];                   /* buffer for target filename */
  18.     register int count = 0;
  19.     int InputError;
  20.  
  21.     printf("\nDemonstration of fcopy function:\n");
  22.  
  23.     for (;;) {
  24.         do {
  25.             printf("\n  Enter the SOURCE file name:  ");
  26.             gets(SrcName);                      /* get source filename */
  27.             if (!*strtrim(SrcName)) {           /* trim white space */
  28.                 printf("\nDemonstration terminated by user\n");
  29.                 return 0;                       /* exit if nothing entered */
  30.             }
  31.  
  32.             InputError = access(SrcName, 0);    /* see if source exists */
  33.             if (InputError) {
  34.                 count++;
  35.                 if (count > 2) {                /* give them up to 3 tries */
  36.                     fprintf(stderr, "\nDemonstration terminated: "
  37.                                     "Too many input errors\n");
  38.                     return -1;
  39.                 }
  40.                 perror("\nINPUT ERROR");        /* if not, display error */
  41.             }
  42.         }
  43.         while (InputError);                     /* and try again */
  44.  
  45.         printf("\n  Enter the TARGET file name:  ");
  46.         gets(TgtName);                          /* get target filename */
  47.         if (!*strtrim(TgtName)) {               /* trim white space */
  48.             printf("\nDemonstration terminated by user\n");
  49.             return 0;                           /* exit if nothing entered */
  50.         }
  51.  
  52.         if (!fcopy(SrcName, TgtName)) {     /* perform the file copy */
  53.                                             /* if successful, */
  54.             strupr(SrcName);                /* fold file names to upper case */
  55.             strupr(TgtName);
  56.             printf("\nSUCCESS: Copied %s to %s\n", SrcName, TgtName);
  57.         }
  58.  
  59.         else switch (errno) {   /* on error, display appropriate message */
  60.             case DISKFUL:
  61.                 fprintf(stderr, "\nFCOPY ERROR: Target disk full\n");
  62.                 if (!remove(TgtName))       /* try to delete bad target file */
  63.                     printf("Target file %s deleted\n", TgtName);
  64.                 break;
  65.             case NOCOPY:
  66.                 fprintf(stderr, "\nFCOPY ERROR: File cannot be copied "
  67.                                                "onto itself\n");
  68.                 break;
  69.             default:                    /* message depends upon errno */
  70.                 perror("\nFCOPY ERROR");
  71.                 break;
  72.         }
  73.     }
  74. }
  75.