File: hardcode.c

package info (click to toggle)
clisp 1:2.41-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 49,804 kB
  • ctags: 16,291
  • sloc: lisp: 75,912; ansic: 49,247; xml: 24,289; asm: 21,993; sh: 11,234; fortran: 6,692; cpp: 2,660; objc: 2,481; makefile: 2,355; perl: 164; sed: 55
file content (191 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download | duplicates (8)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Hardcode some pathnames into an existing executable.
 *
 * This program is used when installing clisp binaries on a system which
 * lacks an ANSI C compiler in its default configuration (such as Solaris
 * or HP-UX).
 *
 * Usage: hardcode [-Dname=value]... input.exe output.exe
 *
 * Bruno Haible 17.5.1999
 */


/* Declare strchr(), strcmp(), strcpy(). */
#include <string.h>

/* Declare malloc(). */
#include <stdlib.h>

/* Declare stderr, fprintf(). */
#include <stdio.h>

/* Declare open(), read(), creat(), write(), chmod(), close(). */
#include <unistd.h>
#include <fcntl.h>

/* Declare fstat(). */
#include <sys/types.h>
#include <sys/stat.h>

/* Declare perror() on some systems. Define EINTR. */
#include <errno.h>


struct substitution {
  struct substitution* next;
  const char* name;
  const char* value;
};

int main (int argc, char* argv[])
{
  struct substitution* defs = NULL;
  const char* input = NULL;
  const char* output = NULL;
  const char* program_name = argv[0];

  /* Check arguments. */
  {
    int i;
    for (i = 1; i < argc; i++) {
      char* arg = argv[i];
      if (arg[0] == '-') {
        if (arg[1] == 'D') {
          char* equals = strchr(&arg[2],'=');
          if (equals) {
            struct substitution* newdef = (struct substitution*) malloc(sizeof(struct substitution));
            newdef->name = &arg[2];
            *equals = '\0';
            newdef->value = equals+1;
            newdef->next = defs;
            defs = newdef;
            continue;
          }
        }
      } else {
        if (!input) {
          input = arg;
          continue;
        }
        if (!output) {
          output = arg;
          continue;
        }
      }
      goto usage;
    }
  }
  if (!input || !output) goto usage;

  /* Eat the file. */
  {
    int inputfd;
    int outputfd;
    struct stat inputstat;
    int buflen;
    char* buf;
    
    /* Read the input file. */
    inputfd = open(input,O_RDONLY);
    if (inputfd < 0) {
      perror(input);
      exit(1);
    }
    if (fstat(inputfd,&inputstat) < 0) {
      perror(input);
      exit(1);
    }
    buflen = inputstat.st_size;
    buf = (char*) malloc(buflen+1);
    if (!buf) {
      errno = ENOMEM;
      perror(program_name);
      exit(1);
    }
    {
      int count = 0;
      while (count < buflen) {
        int n = read(inputfd,buf+count,buflen-count);
        if (n < 0) {
          if (errno != EINTR) {
            perror(input);
            exit(1);
          }
        } else if (n == 0) {
          fprintf(stderr,"Cannot read all of %s\n",input);
          exit(1);
        } else
          count += n;
      }
    }
    buf[buflen] = '\0'; /* so we can use strchr() and strcmp() in the buffer */
    close(inputfd);
    /* Replace all values. */
    {
      char* ptr;
      for (ptr = buf; ptr+7 < buf+buflen; ptr++) {
        if (ptr[0] == '%'
            && ptr[1] == 'M'
            && ptr[2] == 'A'
            && ptr[3] == 'G'
            && ptr[4] == 'I'
            && ptr[5] == 'C'
            && ptr[6] == '%') {
          char* assignment = ptr+7;
          char* equals = strchr(ptr,'=');
          if (!equals) {
            fprintf(stderr,"Malformed contents in %s\n",input);
            exit(1);
          }
          *equals = '\0';
          {
            struct substitution* def;
            for (def = defs; def != NULL; def = def->next) {
              if (!strcmp(def->name,assignment)) {
                strcpy(equals+1,def->value);
                break;
              }
            }
            if (def == NULL) {
              fprintf(stderr,"Warning: No replacement given for %s\n",assignment);
            }
          }
          *equals = '=';
        }
      }
    }
    /* Write the output file. */
    outputfd = creat(output,0644);
    if (outputfd < 0) {
      perror(output);
      exit(1);
    }
    {
      int count = 0;
      while (count < buflen) {
        int n = write(outputfd,buf+count,buflen-count);
        if (n < 0) {
          if (errno != EINTR) {
            perror(output);
            exit(1);
          }
        } else if (n == 0) {
          fprintf(stderr,"Disk full while writing to %s\n",output);
          exit(1);
        } else
          count += n;
      }
    }
    close(outputfd);
    if (chmod(output,0755) < 0) {
      perror(output);
      exit(1);
    }
  }
  return 0;

 usage:
  fprintf(stderr, "Usage: %s [-Dname=value]... input.exe output.exe\n", program_name);
  exit(1);
}