aboutsummaryrefslogtreecommitdiff
path: root/relocation.c
blob: 13ae7993cf1c8b7c882e89ce5d55fd7a62ae9295 (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "spm.h"

int replace_text(char *data, const char *spattern, const char *sreplacement) {
    char *tmp = data;
    size_t data_len = strlen(data);
    size_t spattern_len = strlen(spattern);
    size_t sreplacement_len = strlen(sreplacement);

    if (sreplacement_len > spattern_len) {
        fprintf(stderr, "replacement string too long\n");
        return -1;
    }

    while (*tmp != '\0') {
        if (strncmp(tmp, spattern, spattern_len) == 0) {
            memmove(tmp, sreplacement, sreplacement_len);
            memmove(tmp + sreplacement_len, tmp + spattern_len, data_len - spattern_len);
        }
        tmp++;
    }
    return 0;
}

/**
 * Replace all occurrences of `oldstr` in file `path` with `newstr`
 * @param filename
 * @param oldstr
 * @param newstr
 * @return success=0, failure=-1, or value of `ferror()`
 */
int file_replace_text(char *filename, const char *spattern, const char *sreplacement) {
    char data[BUFSIZ];
    char tempfile[PATH_MAX];
    FILE *fp = NULL;
    if ((fp = fopen(filename, "r")) == NULL) {
        perror(filename);
        return -1;
    }

    sprintf(tempfile, "%s.spmfrt", filename);
    FILE *tfp = NULL;
    if ((tfp = fopen(tempfile, "w+")) == NULL) {
        perror(tempfile);
        return -1;
    }

    // Zero the data buffer
    memset(data, '\0', BUFSIZ);
    while(fgets(data, BUFSIZ, fp) != NULL) {
        replace_text(data, spattern, sreplacement);
        fprintf(tfp, "%s", data);
    }
    fclose(fp);
    rewind(tfp);

    // Truncate the original file
    if ((fp = fopen(filename, "w+")) == NULL) {
        perror(filename);
        return -1;
    }
    // Zero the data buffer once more
    memset(data, '\0', BUFSIZ);
    // Dump the contents of the temporary file into the original file
    while(fgets(data, BUFSIZ, tfp) != NULL) {
        fprintf(fp, "%s", data);
    }
    fclose(fp);
    fclose(tfp);

    // Remove temporary file
    unlink(tempfile);
    return 0;
}

/**
 * Free memory allocated by `prefixes_read` function
 * @param entry array of RelocationEntry
 */
void prefixes_free(RelocationEntry **entry) {
    if (!entry) {
        return;
    }
    for (int i = 0; entry[i] != NULL; i++) {
        if (entry[i]->prefix) free(entry[i]->prefix);
        if (entry[i]->path) free(entry[i]->path);
        if (entry[i]) free(entry[i]);
    }
    free(entry);
}

/**
 * Parse a prefix file
 *
 * The file format is as follows:
 *
 * ~~~
 * #prefix
 * path
 * #prefix
 * path
 * #...N
 * ...N
 * ~~~
 * @param filename
 * @return success=array of RelocationEntry, failure=NULL
 */
RelocationEntry **prefixes_read(const char *filename) {
    size_t i = 0;
    int record_count = 0;
    int parity = 0;
    FILE *fp = fopen(filename, "r");
    if (!fp) {
        fprintf(SYSERROR);
        return NULL;
    }
    RelocationEntry **entry = NULL;
    char line[BUFSIZ];
    memset(line, '\0', BUFSIZ);

    while (fgets(line, BUFSIZ, fp) != NULL) {
        if (isempty(line)) {
            continue;
        }
        record_count++;
    }
    rewind(fp);

    // Initialize the relocation entry array
    if (record_count == 0) {
        return NULL;
    }

    parity = record_count % 2;
    if (parity != 0) {
        fprintf(stderr, "%s: records are not divisible by 2 (got: %d %% 2 = %d)\n", filename, record_count, parity);
        return NULL;
    }
    record_count /= 2;

    entry = (RelocationEntry **)calloc(record_count + 1, sizeof(RelocationEntry *));
    if (!entry) {
        return NULL;
    }
    for (int i = 0; i < record_count; i++) {
        entry[i] = (RelocationEntry *) calloc(1, sizeof(RelocationEntry));
        if (!entry[i]) {
            return NULL;
        }
    }

    int do_prefix = 0;
    int do_path = 0;
    while (fgets(line, BUFSIZ, fp) != NULL) {
        char *wtf = line;
        if (isempty(line)) {
            continue;
        }
        if (startswith(line, "#") == 0) {
            do_prefix = 1;
        }
        else {
            do_path = 1;
        }

        // Allocate a relocation record
        if (!entry[i]) {
            fclose(fp);
            return NULL;
        }


        if (do_prefix) {
            // Populate prefix data (a prefix starts with a #)
            entry[i]->prefix = (char *) calloc(strlen(line) + 1, sizeof(char));
            if (!entry[i]->prefix) {
                fclose(fp);
                return NULL;
            }
            strncpy(entry[i]->prefix, line, strlen(line));
            // Remove prefix delimiter and whitespace
            strchrdel(entry[i]->prefix, "#");
            entry[i]->prefix = strip(entry[i]->prefix);
            do_prefix = 0;
            continue;
        }

        else if (do_path) {
            // Populate path data
            entry[i]->path = (char *) calloc(strlen(line) + 1, sizeof(char));
            if (!entry[i]->path) {
                fclose(fp);
                return NULL;
            }
            strncpy(entry[i]->path, line, strlen(line));
            entry[i]->path = strip(entry[i]->path);
            do_path = 0;
        }
        i++;
    }
    fclose(fp);
    return entry;
}

int relocate(const char *_filename, const char *_oldstr, const char *_newstr) {
    int returncode;
    Process *proc = NULL;
    char *oldstr = strdup(_oldstr);
    char *newstr = strdup(_newstr);
    char *filename = strdup(_filename);
    char cmd[PATH_MAX];

    memset(cmd, '\0', sizeof(cmd));
    sprintf(cmd, "reloc \"%s\" \"%s\" \"%s\" \"%s\"", oldstr, newstr, filename, filename);

    // sanitize command
    strchrdel(cmd, "&;|");

    shell(&proc, SHELL_OUTPUT, cmd);
    if (!proc) {
        free(oldstr);
        free(newstr);
        free(filename);
        return -1;
    }

    returncode = proc->returncode;
    if (returncode != 0 && proc->output) {
        fprintf(stderr, proc->output);
    }

    shell_free(proc);
    free(oldstr);
    free(newstr);
    free(filename);
    return returncode;
}