-
Notifications
You must be signed in to change notification settings - Fork 41
/
stubs.c
133 lines (112 loc) · 2.88 KB
/
stubs.c
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libiberty.h"
#include "stubs.h"
#ifndef HAVE_DCGETTEXT
const char *dcgettext(const char *domain, const char *msg, int category)
{
return msg;
}
#endif /* !HAVE_DCGETTEXT */
#ifndef HAVE_LIBINTL_DGETTEXT
const char *libintl_dgettext(const char *domain, const char *msg)
{
return msg;
}
#endif /* !HAVE_LIBINTL_DGETTEXT */
#ifndef HAVE_GETLINE
/* Read a line from IN. LINE points to a malloc'd buffer that is extended as
necessary. ALLOC points to the allocated length of LINE. Returns
the length of the string read (including any trailing \n) */
ssize_t getline(char **line, size_t *alloc, FILE *in)
{
size_t len = 0;
if (!*alloc) {
*alloc = 200;
*line = xmalloc(*alloc);
}
while (1) {
if (!fgets(*line + len, *alloc - len, in))
return 0;
len += strlen(*line + len);
if (len && (*line)[len - 1] == '\n')
return len;
*alloc *= 2;
*line = xrealloc(*line, *alloc);
}
}
#endif
/* fatal error & exit */
void fatal(const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s: ", elf2flt_progname);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
/* fatal error, perror & exit */
void fatal_perror(const char *format, ...)
{
int e = errno;
va_list args;
va_start(args, format);
fprintf(stderr, "%s: ", elf2flt_progname);
vfprintf(stderr, format, args);
fprintf(stderr, ": %s\n", strerror(e));
va_end(args);
exit(1);
}
/* open a file or fail */
FILE *xfopen(const char *path, const char *mode)
{
FILE *ret = fopen(path, mode);
if (!ret)
fatal_perror("Unable to open '%s'", path);
return ret;
}
/* Append a string SRC to an options array DST */
void append_option(options_t *dst, const char *src)
{
if (dst->alloc == dst->num) {
size_t a = (dst->num + 2) * 2;
void *o = xmalloc(sizeof(*dst->options) * a);
memcpy(o, dst->options, sizeof(*dst->options) * dst->num);
free(dst->options);
dst->options = o;
dst->alloc = a;
}
dst->options[dst->num] = src;
dst->num++;
}
/* Split and append a string SRC to an options array DST */
void append_option_str(options_t *dst, const char *src, const char *delim)
{
char *tok_src = xstrdup(src);
char *tok = strtok(tok_src, delim);
while (tok) {
append_option(dst, tok);
tok = strtok(NULL, delim);
}
/* don't free tok_src since options_t now points to it */
}
/* Append an options array SRC to another options array DST */
void append_options(options_t *dst, const options_t *src)
{
if (dst->alloc < dst->num + src->num) {
size_t a = (dst->num + src->num + 2) * 2;
void *o = xmalloc(sizeof(*dst->options) * a);
memcpy(o, dst->options, sizeof(*dst->options) * dst->num);
free(dst->options);
dst->options = o;
dst->alloc = a;
}
memcpy(&dst->options[dst->num], &src->options[0],
sizeof(*dst->options) * src->num);
dst->num += src->num;
}