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:  * fthood!egray@uxc.cso.uiuc.edu	Directorate of Engineering & Housing
   7:  * 					Environmental Management Office
   8:  * 					Fort Hood, TX 76544-5057
   9:  */
  10: 
  11: #include <stdio.h>
  12: #include <ctype.h>
  13: #include "patchlevel.h"
  14: 
  15: #define NONE    0
  16: #define MREAD   1
  17: #define MWRITE  2
  18: #define MKDIR
  19: 
  20: main(argc, argv)
  21: int argc;
  22: char *argv[];
  23: {
  24:     extern int optind;
  25:     extern char *optarg;
  26:     int i, oops, msdos_args, unix_args, destination;
  27:     char **nargv, **malloc();
  28:     void exit();
  29:                     /* get command line options */
  30:     msdos_args = 0;
  31:     unix_args = 0;
  32:     oops = 0;
  33:     while ((i = getopt(argc, argv, "tnvm")) != EOF) {
  34:         switch (i) {
  35:             case 't':
  36:             case 'n':
  37:             case 'v':
  38:             case 'm':
  39:                 break;
  40:             default:
  41:                 oops = 1;
  42:                 break;
  43:         }
  44:     }
  45: 
  46:     if (oops || (argc - optind) < 2) {
  47:         fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  48:         fprintf(stderr, "Usage: %s [-tnvm] sourcefile targetfile\n", argv[0]);
  49:         fprintf(stderr, "       %s [-tnvm] sourcefile [sourcefiles...] targetdirectory\n", argv[0]);
  50:         exit(1);
  51:     }
  52:                     /* last file determines the direction */
  53:     if (argv[argc - 1][1] == ':')
  54:         destination = MWRITE;
  55:     else
  56:         destination = MREAD;
  57: 
  58:                     /* count the arguments */
  59:     for (i = optind; i < argc; i++) {
  60:         if (argv[i][1] == ':')
  61:             msdos_args++;
  62:         else
  63:             unix_args++;
  64:     }
  65: 
  66:     if (destination == MREAD && unix_args > 1) {
  67:         fprintf(stderr, "%s: Duplicate destination files\n", argv[0]);
  68:         exit(1);
  69:     }
  70:                     /* chaining of mread and mwrite */
  71:     if (destination == MWRITE && msdos_args > 1)
  72:         chain(argc, argv);
  73: 
  74:     /*
  75: 	 * Copy the *argv[] array in case your Unix doesn't end the array
  76: 	 * with a null when it passes it to main()
  77: 	 */
  78:     nargv = (char **) malloc((unsigned int) (argc + 1) * sizeof(*argv));
  79:     nargv[0] = "mcopy";
  80:     for (i = 1; i < argc; i++)
  81:         nargv[i] = argv[i];
  82:     nargv[argc] = NULL;
  83: 
  84:     if (destination == MWRITE)
  85:         execvp("mwrite", nargv);
  86:     else
  87:         execvp("mread", nargv);
  88: }
  89: 
  90: chain(argc, argv)
  91: int argc;
  92: char *argv[];
  93: {
  94:     extern int optind;
  95:     int i, j, pid, status;
  96:     char *tmpdir, *mktemp(), **nargv, **malloc(), buf[256], *strcpy();
  97:     char *unixname(), *realloc();
  98:     void exit();
  99: 
 100:     nargv = (char **) malloc((unsigned int) (argc + 4) * sizeof(*argv));
 101:     nargv[0] = "mread";
 102:     nargv[1] = "-n";
 103:                     /* copy only the msdos arguments */
 104:     j = 2;
 105:     for (i = optind; i < argc -1; i++) {
 106:         if (argv[i][1] == ':')
 107:             nargv[j++] = argv[i];
 108:     }
 109:                     /* create a temp directory */
 110:     tmpdir = mktemp("/tmp/mtoolsXXXXXX");
 111:     if (mkdir(tmpdir, 0777) < 0)
 112:         perror("mkdir");
 113: 
 114:     nargv[j++] = tmpdir;
 115:     nargv[j] = NULL;
 116: 
 117:     printf("reading...\n");
 118:     if (!(pid = fork()))
 119:         execvp("mread", nargv);
 120: 
 121:     while (wait(&status) != pid)
 122:         ;
 123:                     /* reconstruct the argv[] */
 124:     nargv[0] = "sh";
 125:     nargv[1] = "-c";
 126:     nargv[2] = (char *) malloc(7);
 127:     strcpy(nargv[2], "mwrite");
 128: 
 129:     j = 3;
 130:     for (i = 1; i < argc -1; i++) {
 131:         /*
 132: 		 * Substitute the msdos arguments for their unix
 133: 		 * counterparts that have already been copied to tmpdir.
 134: 		 */
 135:         if (argv[i][1] == ':')
 136:             sprintf(buf, "%s/%s", tmpdir, unixname(argv[i]));
 137:         else
 138:             strcpy(buf, argv[i]);
 139: 
 140:         nargv[2] = (char *) realloc(nargv[2], sizeof(nargv[2]) + sizeof(buf));
 141:         strcat(nargv[2], " ");
 142:         strcat(nargv[2], buf);
 143:     }
 144:                     /* protect last arg from expansion */
 145:     sprintf(buf, "'%s'", argv[i]);
 146:     nargv[2] = (char *) realloc(nargv[2], sizeof(nargv[2]) + sizeof(buf));
 147:     strcat(nargv[2], " ");
 148:     strcat(nargv[2], buf);
 149: 
 150:     nargv[3] = NULL;
 151: 
 152:     printf("writing...\n");
 153:     if (!(pid = fork()))
 154:         execvp("sh", nargv);
 155: 
 156:     while (wait(&status) != pid)
 157:         ;
 158:                     /* clobber the directory */
 159:     sprintf(buf, "rm -fr %s", tmpdir);
 160:     system(buf);
 161:     exit(0);
 162: }
 163: 
 164: char *
 165: unixname(filename)
 166: char *filename;
 167: {
 168:     char *s, *temp, *strcpy(), *strrchr(), buf[256];
 169:     static char ans[13];
 170: 
 171:     strcpy(buf, filename);
 172:     temp = buf;
 173:                     /* skip drive letter */
 174:     if (buf[0] && buf[1] == ':')
 175:         temp = &buf[2];
 176:                     /* find the last separator */
 177:     if (s = strrchr(temp, '/'))
 178:         temp = s + 1;
 179:     if (s = strrchr(temp, '\\'))
 180:         temp = s + 1;
 181:                     /* xlate to lower case */
 182:     for (s = temp; *s; ++s) {
 183:         if (isupper(*s))
 184:             *s = tolower(*s);
 185:     }
 186: 
 187:     strcpy(ans, temp);
 188:     return(ans);
 189: }
 190: 
 191: #ifdef MKDIR
 192: mkdir(path, mode)
 193: char *path;
 194: int mode;
 195: {
 196:     char buf[256];
 197:     sprintf(buf, "mkdir %s", path);
 198:     return(system(buf));
 199: }
 200: #endif /* MKDIR */

Defined functions

chain defined in line 90; used 1 times
  • in line 72
main defined in line 20; never used
mkdir defined in line 192; used 1 times
unixname defined in line 164; used 2 times

Defined macros

MKDIR defined in line 18; used 1 times
MREAD defined in line 16; used 2 times
MWRITE defined in line 17; used 3 times
NONE defined in line 15; never used
Last modified: 1992-06-24
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2764
Valid CSS Valid XHTML 1.0 Strict