common/src/fixpath.c
changeset 23837 6f3e6fee7b44
parent 23435 098340eccdcb
child 34101 a1db3b482137
equal deleted inserted replaced
23836:a199f7dbfbc6 23837:6f3e6fee7b44
   107   return out;
   107   return out;
   108 }
   108 }
   109 
   109 
   110 void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
   110 void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
   111 {
   111 {
   112   while ( (addlen+*u+1) > *bl) {
   112   while ((addlen+*u+1) > *bl) {
   113     *bl *= 2;
   113     *bl *= 2;
   114     *b = (char*) realloc(*b, *bl);
   114     *b = (char*) realloc(*b, *bl);
   115   }
   115   }
   116   memcpy(*b+*u, add, addlen);
   116   memcpy(*b+*u, add, addlen);
   117   *u += addlen;
   117   *u += addlen;
   118 }
   118 }
   119 
   119 
   120 /*
   120 /*
   121  * Creates a new string from in where the first occurance of sub is
   121  * Creates a new string from in where the first occurrence of sub is
   122  * replaced by rep.
   122  * replaced by rep.
   123  */
   123  */
   124 char *replace_substring(char *in, char *sub, char *rep)
   124 char *replace_substring(char *in, char *sub, char *rep)
   125 {
   125 {
   126   int in_len = strlen(in);
   126   int in_len = strlen(in);
   244     fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
   244     fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
   245     exit(-1);
   245     exit(-1);
   246   }
   246   }
   247 
   247 
   248   buffer = (char*) malloc(buflen);
   248   buffer = (char*) malloc(buflen);
   249   while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
   249   while ((blocklen = fread(block, 1, sizeof(block), atin)) > 0) {
   250     append(&buffer, &buflen, &used, block, blocklen);
   250     append(&buffer, &buflen, &used, block, blocklen);
   251   }
   251   }
   252   buffer[used] = 0;
   252   buffer[used] = 0;
   253   if (getenv("DEBUG_FIXPATH") != NULL) {
   253   if (getenv("DEBUG_FIXPATH") != NULL) {
   254     fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
   254     fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
   278 char * quote_arg(char const * in_arg) {
   278 char * quote_arg(char const * in_arg) {
   279   char *quoted = NULL;
   279   char *quoted = NULL;
   280   char *current = quoted;
   280   char *current = quoted;
   281   int pass;
   281   int pass;
   282 
   282 
   283   if(strpbrk(in_arg, " \t\n\v\r\\\"") == NULL) {
   283   if (strlen(in_arg) == 0) {
       
   284      // empty string? explicitly quote it.
       
   285      return _strdup("\"\"");
       
   286   }
       
   287 
       
   288   if (strpbrk(in_arg, " \t\n\v\r\\\"") == NULL) {
   284      return _strdup(in_arg);
   289      return _strdup(in_arg);
   285   }
   290   }
   286 
   291 
   287   // process the arg twice. Once to calculate the size and then to copy it.
   292   // process the arg twice. Once to calculate the size and then to copy it.
   288   for(pass=1; pass<=2; pass++) {
   293   for (pass=1; pass<=2; pass++) {
   289     char const *arg = in_arg;
   294     char const *arg = in_arg;
   290 
   295 
   291     // initial "
   296     // initial "
   292     if(pass == 2) {
   297     if (pass == 2) {
   293       *current = '\"';
   298       *current = '\"';
   294     }
   299     }
   295     current++;
   300     current++;
   296 
   301 
   297     // process string to be quoted until NUL
   302     // process string to be quoted until NUL
   326       // and the current char
   331       // and the current char
   327       if (pass == 2) {
   332       if (pass == 2) {
   328         *current = *arg;
   333         *current = *arg;
   329       }
   334       }
   330       current++;
   335       current++;
   331     } while( *arg++ != '\0');
   336     } while (*arg++ != '\0');
   332 
   337 
   333     // allocate the buffer
   338     // allocate the buffer
   334     if (pass == 1) {
   339     if (pass == 1) {
   335       size_t alloc = (size_t) (current - quoted + (ptrdiff_t) 2);
   340       size_t alloc = (size_t) (current - quoted + (ptrdiff_t) 2);
   336       current = quoted = (char*) calloc(alloc, sizeof(char));
   341       current = quoted = (char*) calloc(alloc, sizeof(char));
   360         exit(0);
   365         exit(0);
   361     }
   366     }
   362 
   367 
   363     if (getenv("DEBUG_FIXPATH") != NULL) {
   368     if (getenv("DEBUG_FIXPATH") != NULL) {
   364       char const * cmdline = GetCommandLine();
   369       char const * cmdline = GetCommandLine();
   365       fprintf(stderr, "fixpath input line >%s<\n", strstr( cmdline , argv[1]));
   370       fprintf(stderr, "fixpath input line >%s<\n", strstr(cmdline, argv[1]));
   366     }
   371     }
   367 
   372 
   368     if (argv[1][1] == 'c' && argv[1][2] == '\0') {
   373     if (argv[1][1] == 'c' && argv[1][2] == '\0') {
   369       if (getenv("DEBUG_FIXPATH") != NULL) {
   374       if (getenv("DEBUG_FIXPATH") != NULL) {
   370         fprintf(stderr, "fixpath using cygwin mode\n");
   375         fprintf(stderr, "fixpath using cygwin mode\n");
   397         if (getenv("DEBUG_FIXPATH") != NULL) {
   402         if (getenv("DEBUG_FIXPATH") != NULL) {
   398           fprintf(stderr, "fixpath setting var >%s< to >%s<\n", var, val);
   403           fprintf(stderr, "fixpath setting var >%s< to >%s<\n", var, val);
   399         }
   404         }
   400 
   405 
   401         rc = SetEnvironmentVariable(var, val);
   406         rc = SetEnvironmentVariable(var, val);
   402         if(!rc) {
   407         if (!rc) {
   403           // Could not set var for some reason.  Try to report why.
   408           // Could not set var for some reason.  Try to report why.
   404           const int msg_len = 80 + var_len + strlen(val);
   409           const int msg_len = 80 + var_len + strlen(val);
   405           char * msg = (char *) alloca(msg_len);
   410           char * msg = (char *) alloca(msg_len);
   406           _snprintf_s(msg, msg_len, _TRUNCATE, "Could not set environment variable [%s=%s]", var, val);
   411           _snprintf_s(msg, msg_len, _TRUNCATE, "Could not set environment variable [%s=%s]", var, val);
   407           report_error(msg);
   412           report_error(msg);
   420     cmd = i;
   425     cmd = i;
   421 
   426 
   422     // handle command and it's args.
   427     // handle command and it's args.
   423     while (i < argc) {
   428     while (i < argc) {
   424       char const *replaced = replace_cygdrive(argv[i]);
   429       char const *replaced = replace_cygdrive(argv[i]);
   425       if(replaced[0] == '@') {
   430       if (replaced[0] == '@') {
   426         // Found at-file! Fix it!
   431         // Found at-file! Fix it!
   427         replaced = fix_at_file(replaced);
   432         replaced = fix_at_file(replaced);
   428       }
   433       }
   429       argv[i] = quote_arg(replaced);
   434       argv[i] = quote_arg(replaced);
   430       i++;
   435       i++;
   431     }
   436     }
   432 
   437 
   433     // determine the length of the line
   438     // determine the length of the line
   434     line = NULL;
   439     line = NULL;
   435     // args
   440     // args
   436     for(i = cmd; i < argc; i++) {
   441     for (i = cmd; i < argc; i++) {
   437       line += (ptrdiff_t) strlen(argv[i]);
   442       line += (ptrdiff_t) strlen(argv[i]);
   438     }
   443     }
   439     // spaces and null
   444     // spaces and null
   440     line += (ptrdiff_t) (argc - cmd + 1);
   445     line += (ptrdiff_t) (argc - cmd + 1);
   441     // allocate
   446     // allocate
   442     line = (char*) calloc(line - (char*) NULL, sizeof(char));
   447     line = (char*) calloc(line - (char*) NULL, sizeof(char));
   443 
   448 
   444     // copy in args.
   449     // copy in args.
   445     current = line;
   450     current = line;
   446     for(i = cmd; i < argc; i++) {
   451     for (i = cmd; i < argc; i++) {
   447       ptrdiff_t len = strlen(argv[i]);
   452       ptrdiff_t len = strlen(argv[i]);
   448       if (i != cmd) {
   453       if (i != cmd) {
   449         *current++ = ' ';
   454         *current++ = ' ';
   450       }
   455       }
   451       memmove(current, argv[i], len);
   456       memmove(current, argv[i], len);
   455 
   460 
   456     if (getenv("DEBUG_FIXPATH") != NULL) {
   461     if (getenv("DEBUG_FIXPATH") != NULL) {
   457       fprintf(stderr, "fixpath converted line >%s<\n", line);
   462       fprintf(stderr, "fixpath converted line >%s<\n", line);
   458     }
   463     }
   459 
   464 
   460     if(cmd == argc) {
   465     if (cmd == argc) {
   461        if (getenv("DEBUG_FIXPATH") != NULL) {
   466        if (getenv("DEBUG_FIXPATH") != NULL) {
   462          fprintf(stderr, "fixpath no command provided!\n");
   467          fprintf(stderr, "fixpath no command provided!\n");
   463        }
   468        }
   464        exit(0);
   469        exit(0);
   465     }
   470     }
   466 
   471 
   467     ZeroMemory(&si,sizeof(si));
   472     ZeroMemory(&si, sizeof(si));
   468     si.cb=sizeof(si);
   473     si.cb=sizeof(si);
   469     ZeroMemory(&pi,sizeof(pi));
   474     ZeroMemory(&pi, sizeof(pi));
   470 
   475 
   471     fflush(stderr);
   476     fflush(stderr);
   472     fflush(stdout);
   477     fflush(stdout);
   473 
   478 
   474     rc = CreateProcess(NULL,
   479     rc = CreateProcess(NULL,
   479                        0,
   484                        0,
   480                        NULL,
   485                        NULL,
   481                        NULL,
   486                        NULL,
   482                        &si,
   487                        &si,
   483                        &pi);
   488                        &pi);
   484     if(!rc) {
   489     if (!rc) {
   485       // Could not start process for some reason.  Try to report why:
   490       // Could not start process for some reason.  Try to report why:
   486       report_error("Could not start process!");
   491       report_error("Could not start process!");
   487       exit(126);
   492       exit(126);
   488     }
   493     }
   489 
   494 
   490     WaitForSingleObject(pi.hProcess,INFINITE);
   495     WaitForSingleObject(pi.hProcess, INFINITE);
   491     GetExitCodeProcess(pi.hProcess,&exitCode);
   496     GetExitCodeProcess(pi.hProcess, &exitCode);
   492 
   497 
   493     if (getenv("DEBUG_FIXPATH") != NULL) {
   498     if (getenv("DEBUG_FIXPATH") != NULL) {
   494       for (i=0; i<num_files_to_delete; ++i) {
   499       for (i=0; i<num_files_to_delete; ++i) {
   495         fprintf(stderr, "fixpath Not deleting temporary file %s\n",
   500         fprintf(stderr, "fixpath Not deleting temporary file %s\n",
   496                 files_to_delete[i]);
   501                 files_to_delete[i]);