common/src/fixpath.c
author duke
Wed, 05 Jul 2017 18:28:52 +0200
changeset 14355 dbff70aaaac3
parent 14111 2a82ecb35fc7
child 15058 61c4ac49cbda
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
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    32
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    33
 * Test if pos points to /cygdrive/_/ where _ can
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    34
 * be any character.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    35
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    36
int is_cygdrive_here(int pos, char *in, int len)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    37
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    38
  // Length of /cygdrive/c/ is 12
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    39
  if (pos+12 > len) return 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    40
  if (in[pos+11]=='/' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    41
      in[pos+9]=='/' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    42
      in[pos+8]=='e' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    43
      in[pos+7]=='v' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    44
      in[pos+6]=='i' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    45
      in[pos+5]=='r' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    46
      in[pos+4]=='d' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    47
      in[pos+3]=='g' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    48
      in[pos+2]=='y' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    49
      in[pos+1]=='c' &&
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    50
      in[pos+0]=='/') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    51
    return 1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    52
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    53
  return 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    54
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    55
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    56
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    57
 * Replace /cygdrive/_/ with _:/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    58
 * Works in place since drive letter is always
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    59
 * shorter than /cygdrive/
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    60
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    61
char *replace_cygdrive_cygwin(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    62
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    63
  int len = strlen(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    64
  char *out = malloc(len+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    65
  int i,j;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    66
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    67
  if (len < 12) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    68
    strcpy(out, in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    69
    return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    70
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    71
  for (i = 0, j = 0; i<len;) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    72
    if (is_cygdrive_here(i, in, len)) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    73
      out[j++] = in[i+10];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    74
      out[j++] = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    75
      i+=11;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    76
    } else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    77
      out[j] = in[i];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    78
      i++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    79
      j++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    80
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    81
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    82
  out[j] = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    83
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    84
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    85
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    86
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
    87
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    88
  while ( (addlen+*u+1) > *bl) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    89
    *bl *= 2;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    90
    *b = realloc(*b, *bl);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    91
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    92
  memcpy(*b+*u, add, addlen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    93
  *u += addlen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    94
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    95
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    96
/*
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    97
 * Creates a new string from in where the first occurance of sub is
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    98
 * replaced by rep.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
    99
 */
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   100
char *replace_substring(char *in, char *sub, char *rep)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   101
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   102
  int in_len = strlen(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   103
  int sub_len = strlen(sub);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   104
  int rep_len = strlen(rep);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   105
  char *out = malloc(in_len - sub_len + rep_len + 1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   106
  char *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   107
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   108
  if (!(p = strstr(in, sub))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   109
    // If sub isn't a substring of in, just return in.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   110
    return in;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   111
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   112
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   113
  // Copy characters from beginning of in to start of sub.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   114
  strncpy(out, in, p - in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   115
  out[p - in] = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   116
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   117
  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   118
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   119
  return out;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   120
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   121
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   122
char* msys_path_list; // @-separated list of paths prefix to look for
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   123
char* msys_path_list_end; // Points to last \0 in msys_path_list.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   124
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   125
void setup_msys_path_list(char* argument)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   126
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   127
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   128
  char* drive_letter_pos;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   129
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   130
  msys_path_list = strdup(&argument[2]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   131
  msys_path_list_end = &msys_path_list[strlen(msys_path_list)];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   132
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   133
  // Convert all at-sign (@) in path list to \0.
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   134
  // @ 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
   135
  p = msys_path_list;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   136
  do {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   137
    if (p[1] == ':') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   138
      // msys has mangled our path list, restore it from c:/... to /c/...
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   139
      drive_letter_pos = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   140
      *drive_letter_pos = *p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   141
      *p = '/';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   142
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   143
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   144
    // Look for an @ in the list
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   145
    p = strchr(p, '@');
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   146
    if (p != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   147
      *p = '\0';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   148
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   149
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   150
  } while (p != NULL);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   151
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   152
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   153
char *replace_cygdrive_msys(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   154
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   155
  char* str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   156
  char* prefix;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   157
  char* p;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   158
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   159
  str = strdup(in);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   160
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   161
  // 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
   162
  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
   163
    p=str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   164
    while ((p = strstr(p, prefix))) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   165
      char* drive_letter = p+1;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   166
      *p = *drive_letter;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   167
      *drive_letter = ':';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   168
      p++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   169
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   170
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   171
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   172
  return str;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   173
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   174
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   175
char*(*replace_cygdrive)(char *in) = NULL;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   176
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   177
char *files_to_delete[1024];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   178
int num_files_to_delete = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   179
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   180
char *fix_at_file(char *in)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   181
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   182
  char *tmpdir;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   183
  char name[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   184
  char *atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   185
  char *buffer;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   186
  size_t buflen=65536;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   187
  size_t used=0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   188
  size_t len;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   189
  int rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   190
  FILE *atout;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   191
  FILE *atin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   192
  char block[2048];
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   193
  size_t blocklen;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   194
  char *fixed;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   195
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   196
  atin = fopen(in+1, "r");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   197
  if (atin == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   198
    fprintf(stderr, "Could not read at file %s\n", in+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   199
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   200
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   201
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   202
  tmpdir = getenv("TMP");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   203
  if (tmpdir == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   204
    tmpdir = "c:/cygwin/tmp";
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   205
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   206
  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   207
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   208
  rc = _mktemp_s(name, strlen(name)+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   209
  if (rc) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   210
    fprintf(stderr, "Could not create temporary file name for at file!\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   211
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   212
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   213
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   214
  atout = fopen(name, "w");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   215
  if (atout == NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   216
    fprintf(stderr, "Could not open temporary file for writing! %s\n", name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   217
    exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   218
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   219
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   220
  buffer = malloc(buflen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   221
  while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   222
    append(&buffer, &buflen, &used, block, blocklen);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   223
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   224
  buffer[used] = 0;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   225
  if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   226
    fprintf(stderr, "fixpath input from @-file %s: %s\n", &in[1], buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   227
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   228
  fixed = replace_cygdrive(buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   229
  if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   230
    fprintf(stderr, "fixpath converted to @-file %s is: %s\n", name, fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   231
  }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   232
  fwrite(fixed, strlen(fixed), 1, atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   233
  fclose(atin);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   234
  fclose(atout);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   235
  free(fixed);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   236
  free(buffer);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   237
  files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   238
  strcpy(files_to_delete[num_files_to_delete], name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   239
  num_files_to_delete++;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   240
  atname = malloc(strlen(name)+2);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   241
  atname[0] = '@';
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   242
  strcpy(atname+1, name);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   243
  return atname;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   244
}
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   245
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   246
int main(int argc, char **argv)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   247
{
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   248
    STARTUPINFO si;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   249
    PROCESS_INFORMATION pi;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   250
    unsigned short rc;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   251
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   252
    char *new_at_file;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   253
    char *old_at_file;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   254
    char *line;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   255
    int i;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   256
    DWORD exitCode;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   257
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   258
    if (argc<3 || argv[1][0] != '-' || (argv[1][1] != 'c' && argv[1][1] != 'm')) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   259
        fprintf(stderr, "Usage: fixpath -c|m<path@path@...> /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   260
        exit(0);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   261
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   262
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   263
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   264
      fprintf(stderr, "fixpath input line >%s<\n", strstr(GetCommandLine(), argv[1]));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   265
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   266
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   267
    if (argv[1][1] == 'c' && argv[1][2] == '\0') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   268
      if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   269
        fprintf(stderr, "using cygwin mode\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   270
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   271
      replace_cygdrive = replace_cygdrive_cygwin;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   272
    } else if (argv[1][1] == 'm') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   273
      if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   274
        fprintf(stderr, "using msys mode, with path list: %s\n", &argv[1][2]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   275
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   276
      setup_msys_path_list(argv[1]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   277
      replace_cygdrive = replace_cygdrive_msys;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   278
    } else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   279
      fprintf(stderr, "Unknown mode: %s\n", argv[1]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   280
      exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   281
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   282
    line = replace_cygdrive(strstr(GetCommandLine(), argv[2]));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   283
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   284
    for (i=1; i<argc; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   285
       if (argv[i][0] == '@') {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   286
          // Found at-file! Fix it!
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   287
          old_at_file = replace_cygdrive(argv[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   288
          new_at_file = fix_at_file(old_at_file);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   289
          line = replace_substring(line, old_at_file, new_at_file);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   290
       }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   291
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   292
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   293
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   294
      fprintf(stderr, "fixpath converted line >%s<\n", line);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   295
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   296
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   297
    ZeroMemory(&si,sizeof(si));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   298
    si.cb=sizeof(si);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   299
    ZeroMemory(&pi,sizeof(pi));
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   300
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   301
    rc = CreateProcess(NULL,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   302
                       line,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   303
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   304
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   305
                       TRUE,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   306
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   307
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   308
                       0,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   309
                       &si,
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   310
                       &pi);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   311
    if(!rc)
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   312
    {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   313
      //Could not start process;
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   314
      fprintf(stderr, "Could not start process!\n");
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   315
      exit(-1);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   316
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   317
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   318
    WaitForSingleObject(pi.hProcess,INFINITE);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   319
    GetExitCodeProcess(pi.hProcess,&exitCode);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   320
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   321
    if (getenv("DEBUG_FIXPATH") != NULL) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   322
      for (i=0; i<num_files_to_delete; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   323
        fprintf(stderr, "Not deleting temporary fixpath file %s\n",
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   324
                files_to_delete[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   325
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   326
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   327
    else {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   328
      for (i=0; i<num_files_to_delete; ++i) {
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   329
        remove(files_to_delete[i]);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   330
      }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   331
    }
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   332
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   333
    exit(exitCode);
2a82ecb35fc7 8000992: Update new build-infra makefiles
ohair
parents:
diff changeset
   334
}