common/src/uncygdrive.c
author minqi
Mon, 24 Sep 2012 20:04:19 -0700
changeset 13874 a93c03275528
parent 12258 6ec26f6cc53e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12258
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     1
/*
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     4
 *
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    10
 *
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    15
 * accompanied this code).
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    16
 *
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    20
 *
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    23
 * questions.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    24
 */
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    25
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    26
#include <Windows.h>
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    27
#include <io.h>
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    28
#include <stdio.h>
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    29
#include <string.h>
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    30
#include <malloc.h>
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    31
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    32
/*
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    33
 * Test if pos points to /cygdrive/_/ where _ can
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    34
 * be any character.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    35
 */
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    36
int is_cygdrive_here(int pos, char *in, int len)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    37
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    38
  // Length of /cygdrive/c/ is 12
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    39
  if (pos+12 > len) return 0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    40
  if (in[pos+11]=='/' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    41
      in[pos+9]=='/' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    42
      in[pos+8]=='e' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    43
      in[pos+7]=='v' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    44
      in[pos+6]=='i' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    45
      in[pos+5]=='r' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    46
      in[pos+4]=='d' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    47
      in[pos+3]=='g' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    48
      in[pos+2]=='y' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    49
      in[pos+1]=='c' &&
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    50
      in[pos+0]=='/') {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    51
    return 1;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    52
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    53
  return 0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    54
}
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    55
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    56
/*
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    57
 * Replace /cygdrive/_/ with _:/
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    58
 * Works in place since drive letter is always
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    59
 * shorter than /cygdrive/
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    60
 */
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    61
char *replace_cygdrive(char *in)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    62
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    63
  int len = strlen(in);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    64
  char *out = malloc(len+1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    65
  int i,j;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    66
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    67
  if (len < 12) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    68
    strcpy(out, in);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    69
    return out;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    70
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    71
  for (i = 0, j = 0; i<len;) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    72
    if (is_cygdrive_here(i, in, len)) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    73
      out[j++] = in[i+10];
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    74
      out[j++] = ':';
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    75
      i+=11;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    76
    } else {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    77
      out[j] = in[i];
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    78
      i++;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    79
      j++;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    80
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    81
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    82
  out[j] = 0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    83
  return out;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    84
}
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    85
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    86
void append(char **b, size_t *bl, size_t *u, char *add, size_t addlen)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    87
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    88
  while ( (addlen+*u+1) > *bl) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    89
    *bl *= 2;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    90
    *b = realloc(*b, *bl);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    91
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    92
  memcpy(*b+*u, add, addlen);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    93
  *u += addlen;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    94
}
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    95
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    96
/*
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    97
 * Creates a new string from in where the first occurance of sub is
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    98
 * replaced by rep.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
    99
 */
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   100
char *replace_substring(char *in, char *sub, char *rep)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   101
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   102
  int in_len = strlen(in);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   103
  int sub_len = strlen(sub);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   104
  int rep_len = strlen(rep);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   105
  char *out = malloc(in_len - sub_len + rep_len + 1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   106
  char *p;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   107
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   108
  if (!(p = strstr(in, sub))) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   109
    // If sub isn't a substring of in, just return in.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   110
    return in;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   111
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   112
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   113
  // Copy characters from beginning of in to start of sub.
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   114
  strncpy(out, in, p - in);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   115
  out[p - in] = '\0';
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   116
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   117
  sprintf(out + (p - in), "%s%s", rep, p + sub_len);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   118
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   119
  return out;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   120
}
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   121
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   122
char *files_to_delete[1024];
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   123
int num_files_to_delete = 0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   124
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   125
char *fix_at_file(char *in)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   126
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   127
  char *tmpdir;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   128
  char name[2048];
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   129
  char *atname;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   130
  char *buffer;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   131
  size_t buflen=65536;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   132
  size_t used=0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   133
  size_t len;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   134
  int rc;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   135
  FILE *atout;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   136
  FILE *atin;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   137
  char block[2048];
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   138
  size_t blocklen;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   139
  char *fixed;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   140
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   141
  atin = fopen(in+1, "r");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   142
  if (atin == NULL) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   143
    fprintf(stderr, "Could not read at file %s\n", in+1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   144
    exit(-1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   145
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   146
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   147
  tmpdir = getenv("TMP");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   148
  if (tmpdir == NULL) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   149
    tmpdir = "c:/cygwin/tmp";
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   150
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   151
  _snprintf(name, sizeof(name), "%s\\atfile_XXXXXX", tmpdir);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   152
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   153
  rc = _mktemp_s(name, strlen(name)+1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   154
  if (rc) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   155
    fprintf(stderr, "Could not create temporary file name for at file!\n");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   156
    exit(-1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   157
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   158
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   159
  atout = fopen(name, "w");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   160
  if (atout == NULL) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   161
    fprintf(stderr, "Could open temporary file for writing! %s\n", name);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   162
    exit(-1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   163
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   164
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   165
  buffer = malloc(buflen);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   166
  while((blocklen = fread(block,1,sizeof(block),atin)) > 0) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   167
    append(&buffer, &buflen, &used, block, blocklen);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   168
  }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   169
  buffer[used] = 0;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   170
  fixed = replace_cygdrive(buffer);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   171
  fwrite(fixed, strlen(fixed), 1, atout);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   172
  fclose(atin);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   173
  fclose(atout);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   174
  free(fixed);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   175
  free(buffer);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   176
  files_to_delete[num_files_to_delete] = malloc(strlen(name)+1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   177
  strcpy(files_to_delete[num_files_to_delete], name);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   178
  num_files_to_delete++;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   179
  atname = malloc(strlen(name)+2);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   180
  atname[0] = '@';
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   181
  strcpy(atname+1, name);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   182
  return atname;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   183
}
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   184
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   185
int main(int argc, char **argv)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   186
{
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   187
    STARTUPINFO si;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   188
    PROCESS_INFORMATION pi;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   189
    unsigned short rc;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   190
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   191
    char *new_at_file;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   192
    char *old_at_file;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   193
    char *line;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   194
    int i;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   195
    DWORD exitCode;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   196
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   197
    if (argc<2) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   198
        fprintf(stderr, "Usage: uncygdrive.exe /cygdrive/c/WINDOWS/notepad.exe /cygdrive/c/x/test.txt");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   199
        exit(0);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   200
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   201
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   202
    line = replace_cygdrive(strstr(GetCommandLine(), argv[1]));
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   203
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   204
    for (i=1; i<argc; ++i) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   205
       if (argv[i][0] == '@') {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   206
          // Found at-file! Fix it!
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   207
          old_at_file = replace_cygdrive(argv[i]);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   208
          new_at_file = fix_at_file(old_at_file);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   209
          line = replace_substring(line, old_at_file, new_at_file);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   210
       }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   211
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   212
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   213
    if (getenv("DEBUG_UNCYGDRIVE") != NULL) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   214
      fprintf(stderr, "uncygdrive >%s<\n", line);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   215
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   216
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   217
    ZeroMemory(&si,sizeof(si));
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   218
    si.cb=sizeof(si);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   219
    ZeroMemory(&pi,sizeof(pi));
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   220
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   221
    rc = CreateProcess(NULL,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   222
                       line,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   223
                       0,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   224
                       0,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   225
                       TRUE,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   226
                       0,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   227
                       0,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   228
                       0,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   229
                       &si,
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   230
                       &pi);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   231
    if(!rc)
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   232
    {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   233
      //Could not start process;
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   234
      fprintf(stderr, "Could not start process!\n");
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   235
      exit(-1);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   236
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   237
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   238
    WaitForSingleObject(pi.hProcess,INFINITE);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   239
    GetExitCodeProcess(pi.hProcess,&exitCode);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   240
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   241
    if (getenv("DEBUG_UNCYGDRIVE") != NULL) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   242
      for (i=0; i<num_files_to_delete; ++i) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   243
        fprintf(stderr, "Not deleting temporary uncygdrive file %s\n",
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   244
                files_to_delete[i]);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   245
      }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   246
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   247
    else {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   248
      for (i=0; i<num_files_to_delete; ++i) {
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   249
        remove(files_to_delete[i]);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   250
      }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   251
    }
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   252
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   253
    exit(exitCode);
6ec26f6cc53e 7074397: Build infrastructure changes (makefile re-write)
ohair
parents:
diff changeset
   254
}