common/src/fixpath.c
author ccheung
Thu, 24 Oct 2013 17:20:39 -0400
changeset 21084 31bdb14868f6
parent 15058 61c4ac49cbda
child 23435 098340eccdcb
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     1
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     4
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    10
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    15
 * accompanied this code).
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    16
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    20
 *
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    23
 * questions.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    24
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    25
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    26
#include <Windows.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    27
#include <io.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    28
#include <stdio.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    29
#include <string.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    30
#include <malloc.h>
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    31
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    32
void report_error()
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    33
{
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    34
  LPVOID lpMsgBuf;
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    35
  DWORD dw = GetLastError();
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    36
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    37
  FormatMessage(
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    38
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    39
      FORMAT_MESSAGE_FROM_SYSTEM |
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    40
      FORMAT_MESSAGE_IGNORE_INSERTS,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    41
      NULL,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    42
      dw,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    43
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    44
      (LPTSTR) &lpMsgBuf,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    45
      0,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    46
      NULL);
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    47
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    48
  fprintf(stderr,
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    49
          "Could not start process!  Failed with error %d: %s\n",
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    50
          dw, lpMsgBuf);
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    51
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    52
  LocalFree(lpMsgBuf);
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    53
}
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
    54
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    55
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    56
 * Test if pos points to /cygdrive/_/ where _ can
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    57
 * be any character.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    58
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    59
int is_cygdrive_here(int pos, char *in, int len)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    60
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    61
  // Length of /cygdrive/c/ is 12
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    62
  if (pos+12 > len) return 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    63
  if (in[pos+11]=='/' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    64
      in[pos+9]=='/' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    65
      in[pos+8]=='e' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    66
      in[pos+7]=='v' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    67
      in[pos+6]=='i' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    68
      in[pos+5]=='r' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    69
      in[pos+4]=='d' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    70
      in[pos+3]=='g' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    71
      in[pos+2]=='y' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    72
      in[pos+1]=='c' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    73
      in[pos+0]=='/') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    74
    return 1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    75
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    76
  return 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    77
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    78
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    79
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    80
 * Replace /cygdrive/_/ with _:/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    81
 * Works in place since drive letter is always
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    82
 * shorter than /cygdrive/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    83
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    84
char *replace_cygdrive_cygwin(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    85
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    86
  int len = strlen(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    87
  char *out = malloc(len+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    88
  int i,j;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    89
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    90
  if (len < 12) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    91
    strcpy(out, in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    92
    return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    93
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    94
  for (i = 0, j = 0; i<len;) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    95
    if (is_cygdrive_here(i, in, len)) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    96
      out[j++] = in[i+10];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    97
      out[j++] = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    98
      i+=11;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    99
    } else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   100
      out[j] = in[i];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   101
      i++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   102
      j++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   103
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   104
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   105
  out[j] = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   106
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   107
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   108
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   109
void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   110
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   111
  while ( (addlen+*u+1) > *bl) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   112
    *bl *= 2;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   113
    *b = realloc(*b, *bl);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   114
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   115
  memcpy(*b+*u, add, addlen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   116
  *u += addlen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   117
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   118
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   119
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   120
 * Creates a new string from in where the first occurance of sub is
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   121
 * replaced by rep.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   122
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   123
char *replace_substring(char *in, char *sub, char *rep)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   124
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   125
  int in_len = strlen(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   126
  int sub_len = strlen(sub);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   127
  int rep_len = strlen(rep);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   128
  char *out = malloc(in_len - sub_len + rep_len + 1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   129
  char *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   130
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   131
  if (!(p = strstr(in, sub))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   132
    // If sub isn't a substring of in, just return in.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   133
    return in;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   134
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   135
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   136
  // Copy characters from beginning of in to start of sub.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   137
  strncpy(out, in, p - in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   138
  out[p - in] = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   139
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   140
  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   141
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   142
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   143
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   144
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   145
char* msys_path_list; // @-separated list of paths prefix to look for
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   146
char* msys_path_list_end; // Points to last \0 in msys_path_list.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   147
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   148
void setup_msys_path_list(char* argument)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   149
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   150
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   151
  char* drive_letter_pos;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   152
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   153
  msys_path_list = strdup(&argument[2]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   154
  msys_path_list_end = &msys_path_list[strlen(msys_path_list)];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   155
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   156
  // Convert all at-sign (@) in path list to \0.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   157
  // @ was chosen as separator to minimize risk of other tools messing around with it
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   158
  p = msys_path_list;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   159
  do {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   160
    if (p[1] == ':') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   161
      // msys has mangled our path list, restore it from c:/... to /c/...
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   162
      drive_letter_pos = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   163
      *drive_letter_pos = *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   164
      *p = '/';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   165
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   166
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   167
    // Look for an @ in the list
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   168
    p = strchr(p, '@');
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   169
    if (p != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   170
      *p = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   171
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   172
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   173
  } while (p != NULL);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   174
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   175
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   176
char *replace_cygdrive_msys(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   177
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   178
  char* str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   179
  char* prefix;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   180
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   181
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   182
  str = strdup(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   183
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   184
  // For each prefix in the path list, search for it and replace /c/... with c:/...
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   185
  for (prefix = msys_path_list; prefix < msys_path_list_end && prefix != NULL; prefix += strlen(prefix)+1) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   186
    p=str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   187
    while ((p = strstr(p, prefix))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   188
      char* drive_letter = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   189
      *p = *drive_letter;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   190
      *drive_letter = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   191
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   192
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   193
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   194
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   195
  return str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   196
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   197
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   198
char*(*replace_cygdrive)(char *in) = NULL;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   199
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   200
char *files_to_delete[1024];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   201
int num_files_to_delete = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   202
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   203
char *fix_at_file(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   204
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   205
  char *tmpdir;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   206
  char name[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   207
  char *atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   208
  char *buffer;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   209
  size_t buflen=65536;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   210
  size_t used=0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   211
  size_t len;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   212
  int rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   213
  FILE *atout;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   214
  FILE *atin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   215
  char block[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   216
  size_t blocklen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   217
  char *fixed;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   218
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   219
  atin = fopen(in+1, "r");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   220
  if (atin == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   221
    fprintf(stderr, "Could not read at file %s\n", in+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   222
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   223
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   224
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   225
  tmpdir = getenv("TMP");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   226
  if (tmpdir == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   227
    tmpdir = "c:/cygwin/tmp";
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   228
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   229
  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   230
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   231
  rc = _mktemp_s(name, strlen(name)+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   232
  if (rc) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   233
    fprintf(stderr, "Could not create temporary file name for at file!\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   234
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   235
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   236
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   237
  atout = fopen(name, "w");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   238
  if (atout == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   239
    fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   240
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   241
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   242
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   243
  buffer = malloc(buflen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   244
  while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   245
    append(&buffer, &buflen, &used, block, blocklen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   246
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   247
  buffer[used] = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   248
  if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   249
    fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   250
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   251
  fixed = replace_cygdrive(buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   252
  if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   253
    fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   254
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   255
  fwrite(fixed, strlen(fixed), 1, atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   256
  fclose(atin);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   257
  fclose(atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   258
  free(fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   259
  free(buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   260
  files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   261
  strcpy(files_to_delete[num_files_to_delete], name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   262
  num_files_to_delete++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   263
  atname = malloc(strlen(name)+2);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   264
  atname[0] = '@';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   265
  strcpy(atname+1, name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   266
  return atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   267
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   268
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   269
int main(int argc, char **argv)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   270
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   271
    STARTUPINFO si;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   272
    PROCESS_INFORMATION pi;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   273
    unsigned short rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   274
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   275
    char *new_at_file;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   276
    char *old_at_file;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   277
    char *line;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   278
    int i;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   279
    DWORD exitCode;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   280
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   281
    if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   282
        fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt\n");
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   283
        exit(0);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   284
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   285
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   286
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   287
      fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   288
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   289
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   290
    if (argv[1][1] == 'c' && argv[1][2] == '\0') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   291
      if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   292
        fprintf(stderr, "using cygwin mode\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   293
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   294
      replace_cygdrive = replace_cygdrive_cygwin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   295
    } else if (argv[1][1] == 'm') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   296
      if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   297
        fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   298
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   299
      setup_msys_path_list(argv[1]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   300
      replace_cygdrive = replace_cygdrive_msys;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   301
    } else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   302
      fprintf(stderr, "Unknown mode: %s\n", argv[1]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   303
      exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   304
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   305
    line = replace_cygdrive(strstr(GetCommandLine(), argv[2]));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   306
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   307
    for (i=1; i<argc; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   308
       if (argv[i][0] == '@') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   309
          // Found at-file! Fix it!
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   310
          old_at_file = replace_cygdrive(argv[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   311
          new_at_file = fix_at_file(old_at_file);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   312
          line = replace_substring(line, old_at_file, new_at_file);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   313
       }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   314
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   315
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   316
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   317
      fprintf(stderr, "fixpath converted line >%s<\n", line);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   318
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   319
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   320
    ZeroMemory(&si,sizeof(si));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   321
    si.cb=sizeof(si);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   322
    ZeroMemory(&pi,sizeof(pi));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   323
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   324
    rc = CreateProcess(NULL,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   325
                       line,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   326
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   327
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   328
                       TRUE,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   329
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   330
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   331
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   332
                       &si,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   333
                       &pi);
15058
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   334
    if(!rc) {
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   335
      // Could not start process for some reason.  Try to report why:
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   336
      report_error();
61c4ac49cbda 8005635: build-infra: Support building install in jprt
erikj
parents: 14111
diff changeset
   337
      exit(rc);
14111
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   338
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   339
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   340
    WaitForSingleObject(pi.hProcess,INFINITE);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   341
    GetExitCodeProcess(pi.hProcess,&exitCode);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   342
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   343
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   344
      for (i=0; i<num_files_to_delete; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   345
        fprintf(stderr, "Not deleting temporary fixpath file %s\n",
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   346
                files_to_delete[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   347
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   348
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   349
    else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   350
      for (i=0; i<num_files_to_delete; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   351
        remove(files_to_delete[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   352
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   353
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   354
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   355
    exit(exitCode);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   356
}