common/src/fixpath.c

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <Windows.h>
  27 #include <io.h>
  28 #include <stdio.h>
  29 #include <string.h>
  30 #include <malloc.h>
  31 























  32 /*
  33  * Test if pos points to /cygdrive/_/ where _ can
  34  * be any character.
  35  */
  36 int is_cygdrive_here(int pos, char *in, int len)
  37 {
  38   // Length of /cygdrive/c/ is 12
  39   if (pos+12 > len) return 0;
  40   if (in[pos+11]=='/' &&
  41       in[pos+9]=='/' &&
  42       in[pos+8]=='e' &&
  43       in[pos+7]=='v' &&
  44       in[pos+6]=='i' &&
  45       in[pos+5]=='r' &&
  46       in[pos+4]=='d' &&
  47       in[pos+3]=='g' &&
  48       in[pos+2]=='y' &&
  49       in[pos+1]=='c' &&
  50       in[pos+0]=='/') {
  51     return 1;


 239   num_files_to_delete++;
 240   atname = malloc(strlen(name)+2);
 241   atname[0] = '@';
 242   strcpy(atname+1, name);
 243   return atname;
 244 }
 245 
 246 int main(int argc, char **argv)
 247 {
 248     STARTUPINFO si;
 249     PROCESS_INFORMATION pi;
 250     unsigned short rc;
 251 
 252     char *new_at_file;
 253     char *old_at_file;
 254     char *line;
 255     int i;
 256     DWORD exitCode;
 257 
 258     if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
 259         fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt");
 260         exit(0);
 261     }
 262 
 263     if (getenv("DEBUG_FIXPATH") != NULL) {
 264       fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
 265     }
 266 
 267     if (argv[1][1] == 'c' && argv[1][2] == '\0') {
 268       if (getenv("DEBUG_FIXPATH") != NULL) {
 269         fprintf(stderr, "using cygwin mode\n");
 270       }
 271       replace_cygdrive = replace_cygdrive_cygwin;
 272     } else if (argv[1][1] == 'm') {
 273       if (getenv("DEBUG_FIXPATH") != NULL) {
 274         fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
 275       }
 276       setup_msys_path_list(argv[1]);
 277       replace_cygdrive = replace_cygdrive_msys;
 278     } else {
 279       fprintf(stderr, "Unknown mode: %s\n", argv[1]);


 291     }
 292 
 293     if (getenv("DEBUG_FIXPATH") != NULL) {
 294       fprintf(stderr, "fixpath converted line >%s<\n", line);
 295     }
 296 
 297     ZeroMemory(&si,sizeof(si));
 298     si.cb=sizeof(si);
 299     ZeroMemory(&pi,sizeof(pi));
 300 
 301     rc = CreateProcess(NULL,
 302                        line,
 303                        0,
 304                        0,
 305                        TRUE,
 306                        0,
 307                        0,
 308                        0,
 309                        &si,
 310                        &pi);
 311     if(!rc)
 312     {
 313       //Could not start process;
 314       fprintf(stderr, "Could not start process!\n");
 315       exit(-1);
 316     }
 317 
 318     WaitForSingleObject(pi.hProcess,INFINITE);
 319     GetExitCodeProcess(pi.hProcess,&exitCode);
 320 
 321     if (getenv("DEBUG_FIXPATH") != NULL) {
 322       for (i=0; i<num_files_to_delete; ++i) {
 323         fprintf(stderr, "Not deleting temporary fixpath file %s\n",
 324                 files_to_delete[i]);
 325       }
 326     }
 327     else {
 328       for (i=0; i<num_files_to_delete; ++i) {
 329         remove(files_to_delete[i]);
 330       }
 331     }
 332 
 333     exit(exitCode);
 334 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <Windows.h>
  27 #include <io.h>
  28 #include <stdio.h>
  29 #include <string.h>
  30 #include <malloc.h>
  31 
  32 void report_error()
  33 {
  34   LPVOID lpMsgBuf;
  35   DWORD dw = GetLastError();
  36 
  37   FormatMessage(
  38       FORMAT_MESSAGE_ALLOCATE_BUFFER |
  39       FORMAT_MESSAGE_FROM_SYSTEM | 
  40       FORMAT_MESSAGE_IGNORE_INSERTS,
  41       NULL,
  42       dw,
  43       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  44       (LPTSTR) &lpMsgBuf,
  45       0,
  46       NULL);
  47 
  48   fprintf(stderr, 
  49           "Could not start process!  Failed with error %d: %s\n",
  50           dw, lpMsgBuf);
  51 
  52   LocalFree(lpMsgBuf);
  53 }
  54 
  55 /*
  56  * Test if pos points to /cygdrive/_/ where _ can
  57  * be any character.
  58  */
  59 int is_cygdrive_here(int pos, char *in, int len)
  60 {
  61   // Length of /cygdrive/c/ is 12
  62   if (pos+12 > len) return 0;
  63   if (in[pos+11]=='/' &&
  64       in[pos+9]=='/' &&
  65       in[pos+8]=='e' &&
  66       in[pos+7]=='v' &&
  67       in[pos+6]=='i' &&
  68       in[pos+5]=='r' &&
  69       in[pos+4]=='d' &&
  70       in[pos+3]=='g' &&
  71       in[pos+2]=='y' &&
  72       in[pos+1]=='c' &&
  73       in[pos+0]=='/') {
  74     return 1;


 262   num_files_to_delete++;
 263   atname = malloc(strlen(name)+2);
 264   atname[0] = '@';
 265   strcpy(atname+1, name);
 266   return atname;
 267 }
 268 
 269 int main(int argc, char **argv)
 270 {
 271     STARTUPINFO si;
 272     PROCESS_INFORMATION pi;
 273     unsigned short rc;
 274 
 275     char *new_at_file;
 276     char *old_at_file;
 277     char *line;
 278     int i;
 279     DWORD exitCode;
 280 
 281     if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
 282         fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt\n");
 283         exit(0);
 284     }
 285 
 286     if (getenv("DEBUG_FIXPATH") != NULL) {
 287       fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
 288     }
 289 
 290     if (argv[1][1] == 'c' && argv[1][2] == '\0') {
 291       if (getenv("DEBUG_FIXPATH") != NULL) {
 292         fprintf(stderr, "using cygwin mode\n");
 293       }
 294       replace_cygdrive = replace_cygdrive_cygwin;
 295     } else if (argv[1][1] == 'm') {
 296       if (getenv("DEBUG_FIXPATH") != NULL) {
 297         fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
 298       }
 299       setup_msys_path_list(argv[1]);
 300       replace_cygdrive = replace_cygdrive_msys;
 301     } else {
 302       fprintf(stderr, "Unknown mode: %s\n", argv[1]);


 314     }
 315 
 316     if (getenv("DEBUG_FIXPATH") != NULL) {
 317       fprintf(stderr, "fixpath converted line >%s<\n", line);
 318     }
 319 
 320     ZeroMemory(&si,sizeof(si));
 321     si.cb=sizeof(si);
 322     ZeroMemory(&pi,sizeof(pi));
 323 
 324     rc = CreateProcess(NULL,
 325                        line,
 326                        0,
 327                        0,
 328                        TRUE,
 329                        0,
 330                        0,
 331                        0,
 332                        &si,
 333                        &pi);
 334     if(!rc) {
 335       // Could not start process for some reason.  Try to report why:
 336       report_error();
 337       exit(rc);

 338     }
 339 
 340     WaitForSingleObject(pi.hProcess,INFINITE);
 341     GetExitCodeProcess(pi.hProcess,&exitCode);
 342 
 343     if (getenv("DEBUG_FIXPATH") != NULL) {
 344       for (i=0; i<num_files_to_delete; ++i) {
 345         fprintf(stderr, "Not deleting temporary fixpath file %s\n",
 346                 files_to_delete[i]);
 347       }
 348     }
 349     else {
 350       for (i=0; i<num_files_to_delete; ++i) {
 351         remove(files_to_delete[i]);
 352       }
 353     }
 354 
 355     exit(exitCode);
 356 }