home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / mcopy.c < prev    next >
C/C++ Source or Header  |  1990-05-10  |  3KB  |  120 lines

  1. /*
  2.  * A front-end to the mread/mwrite commands.
  3.  *
  4.  * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  5.  * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  6.  *                     Directorate of Engineering & Housing
  7.  *                     Environmental Management Office
  8.  *                     Fort Hood, TX 76544-5057
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. #define NONE    0
  14. #define MREAD    1
  15. #define MWRITE    2
  16.  
  17. main(argc, argv)
  18. int argc;
  19. char *argv[];
  20. {
  21.     extern int optind;
  22.     extern char *optarg;
  23.     int i, oops, msdos_args, unix_args, destination;
  24.     char **nargv, *strcpy();
  25.     void exit();
  26.                     /* get command line options */
  27.     msdos_args = 0;
  28.     unix_args = 0;
  29.     oops = 0;
  30.     while ((i = getopt(argc, argv, "tnvm")) != EOF) {
  31.         switch(i) {
  32.             case 't':
  33.             case 'n':
  34.             case 'v':
  35.             case 'm':
  36.                 break;
  37.             default:
  38.                 oops = 1;
  39.                 break;
  40.         }
  41.     }
  42.  
  43.     if (oops || (argc - optind) < 2) {
  44.         fprintf(stderr, "Usage: mcopy [-tnvm] a:msdosfile unixfile\n");
  45.         fprintf(stderr, "       mcopy [-tnvm] a:msdosfile [a:msdosfiles...] unixdirectory\n");
  46.         fprintf(stderr, "       mcopy [-tnm] unixfile a:msdosfile\n");
  47.         fprintf(stderr, "       mcopy [-tnm] unixfile [unixfiles...] a:msdosdirectory\n");
  48.         exit(1);
  49.     }
  50.                     /* what is the destination drive? */
  51.     destination = NONE;
  52.                     /* first file */
  53.     if (argv[optind][1] == ':') {
  54.         if (argv[optind][0] == 'a' || argv[optind][0] == 'A')
  55.             destination = MREAD;
  56.     }
  57.                     /* last file */
  58.     if (destination == NONE && argv[argc-1][1] == ':') {
  59.         if (argv[argc-1][0] == 'a' || argv[argc-1][0] == 'A')
  60.             destination = MWRITE;
  61.     }
  62.     if (destination == NONE) {
  63.         fprintf(stderr, "mcopy: no 'a:' designation specified\n");
  64.         exit(1);
  65.     }
  66.                     /* strip out the fake "drive code" */
  67.     for (i=optind; i<argc; i++) {
  68.         if (argv[i][1] == ':') {
  69.             switch(argv[i][0]) {
  70.                 case 'a':
  71.                 case 'A':
  72.                     if (argv[i][2] == '\0')
  73.                         strcpy(argv[i], ".");
  74.                     else
  75.                         strcpy(argv[i], &argv[i][2]);
  76.                     msdos_args++;
  77.                     break;
  78.                 case 'c':
  79.                 case 'C':
  80.                     fprintf(stderr, "mcopy: 'c:' is not used to designate Unix files\n");
  81.                     exit(1);
  82.                 default:
  83.                     fprintf(stderr, "mcopy: unknown drive '%c:'\n", argv[i][0]);
  84.                     exit(1);
  85.             }
  86.             continue;
  87.         }
  88.                     /* if no drive code, its a unix file */
  89.         unix_args++;
  90.     }
  91.                     /* sanity checking */
  92.     if (!msdos_args || !unix_args) {
  93.         fprintf(stderr, "mcopy: unresloved destination\n");
  94.         exit(1);
  95.     }
  96.     if ((destination == MWRITE && msdos_args > 1) || (destination == MREAD && unix_args > 1)) {
  97.         fprintf(stderr, "mcopy: duplicate destination files\n");
  98.         exit(1);
  99.     }
  100.     /*
  101.      * Copy the *argv[] array in case your Unix doesn't end the array
  102.      * with a null when it passes it to main()
  103.      */
  104.     nargv = (char **) malloc((argc+1) * sizeof(*argv));
  105.     nargv[argc] = NULL;
  106.     for(;--argc>=0;)
  107.         nargv[argc] = argv[argc];
  108.  
  109.     if (destination == MWRITE) {
  110.         nargv[0] = "mwrite";
  111.         execvp("mwrite", nargv);
  112.         perror("execvp: mwrite") ;
  113.     }
  114.     else {
  115.         nargv[0] = "mread";
  116.         execvp("mread", nargv);
  117.         perror("execvp: mmread") ;
  118.     }
  119. }
  120.