summaryrefslogtreecommitdiff
path: root/etc/patches/wrapper.c
blob: 727bd0cf9f46e26bd911e60bccbf5ce136dca552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char** argv) {
#include "toolname.h"
#include "link_args.h"
  char *s, *path, *newpath, *prgdir, *cwd, **newargv, *tool, *tmp;
  size_t size, s_s, s_cwd, s_prgdir, s_path, s_toolname, s_tmp;
  int i;

  for (link_args_c =0; link_args[link_args_c] != NULL; link_args_c++) {}

  if ((tmp=strdup(argv[0])) == NULL) {
    return 66;
  }
  s = strrchr(tmp, '/');
  if (s == NULL) {
    s = getenv("CC_DIR");
    if (s == NULL) {
      s = getenv("CC");
    }
    if (s == NULL) {
      s = getenv("CXX");
    }
    if (s != NULL) {
      if ((tmp=strdup(s)) == NULL) {
        return 66;
      }
      s = strrchr(tmp, '/');
    }
  }
  if (s != NULL) {
    *s = '\0';
    s_tmp = strlen(tmp);
    if ((prgdir=(char*)malloc(s_tmp + 1 + strlen(dir_offset) + 1)) == NULL) {
      return 66;
    }
    strcpy(prgdir, tmp);
    strcpy(&prgdir[s_tmp], "/");
    strcpy(&prgdir[s_tmp+1], dir_offset);
  } else {
    prgdir = dir_offset; /* last resort */
  }

  cwd = ".";
  if (*prgdir != '/') {
    /* called by non-absolute path, need to prefix with cwd */
    for (size = 1024;; size *= 2) {
      if ((cwd=(char*)malloc(size)) == NULL) {
        return 66;
      }
      if (getcwd(cwd, size) != NULL) {
        break;
      }
      if (errno != ERANGE) {
        return 66;
      }
    }
    s = prgdir;
    s_s = strlen(s);
    s_cwd = strlen(cwd);

    if ((prgdir=(char*)malloc(s_cwd + 1 + s_s + 1))==NULL) {
      return 66;
    }
    strcpy(prgdir, cwd);
    strcpy(&prgdir[s_cwd], "/");
    strcpy(&prgdir[s_cwd+1], s);
  }

  /* prgdir is now our best guess for the directory the real program is located in */
  path = getenv("PATH");
  if (path == NULL) {
    path = prgdir;
  } else {
    s_path = strlen(path);
    s_prgdir = strlen(prgdir);
    if ((newpath=(char*)malloc(s_prgdir + 1 + s_path + 1)) == NULL) {
      return 66;
    }
    strcpy(newpath, prgdir);
    strcpy(&newpath[s_prgdir], ":");
    strcpy(&newpath[s_prgdir+1], path);
    path = newpath;
  }
  setenv("PATH", path, 1);

  if ((newargv=(char**)malloc((argc + link_args_c + 1)*sizeof(char*)))==NULL) {
    return 66;
  }
  s_prgdir = strlen(prgdir);
  s_toolname = strlen(toolname);
  if ((tool=(char*)malloc(s_prgdir+1+s_toolname+1))==NULL) {
    return 66;
  }
  strcpy(tool, prgdir);
  strcpy(&tool[s_prgdir], "/");
  strcpy(&tool[s_prgdir+1], toolname);

  newargv[0] = tool;
  for (i=1; i<argc; i++) {
    newargv[i] = argv[i];
  }
  for (i=0; i < link_args_c; i++) {
    newargv[argc+i] = link_args[i];
  }
  newargv[argc+link_args_c] = NULL;

  execv(tool, newargv);

  return 65; /* exec failed */
}