| 445 |
theseven |
1 |
#include <errno.h>
|
|
|
2 |
#include <stdio.h>
|
|
|
3 |
#include <stdlib.h>
|
|
|
4 |
#include <string.h>
|
|
|
5 |
#include <unistd.h>
|
|
|
6 |
|
|
|
7 |
#include "libiberty.h"
|
|
|
8 |
|
|
|
9 |
#include "stubs.h"
|
|
|
10 |
|
|
|
11 |
#ifndef HAVE_DCGETTEXT
|
|
|
12 |
const char *dcgettext(const char *domain, const char *msg, int category)
|
|
|
13 |
{
|
|
|
14 |
return msg;
|
|
|
15 |
}
|
|
|
16 |
#endif /* !HAVE_DCGETTEXT */
|
|
|
17 |
|
|
|
18 |
#ifndef HAVE_LIBINTL_DGETTEXT
|
|
|
19 |
const char *libintl_dgettext(const char *domain, const char *msg)
|
|
|
20 |
{
|
|
|
21 |
return msg;
|
|
|
22 |
}
|
|
|
23 |
#endif /* !HAVE_LIBINTL_DGETTEXT */
|
|
|
24 |
|
|
|
25 |
#ifndef HAVE_GETLINE
|
|
|
26 |
/* Read a line from IN. LINE points to a malloc'd buffer that is extended as
|
|
|
27 |
necessary. ALLOC points to the allocated length of LINE. Returns
|
|
|
28 |
the length of the string read (including any trailing \n) */
|
|
|
29 |
|
|
|
30 |
ssize_t getline(char **line, size_t *alloc, FILE *in)
|
|
|
31 |
{
|
|
|
32 |
size_t len = 0;
|
|
|
33 |
|
|
|
34 |
if (!*alloc) {
|
|
|
35 |
*alloc = 200;
|
|
|
36 |
*line = xmalloc(*alloc);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
while (1) {
|
|
|
40 |
if (!fgets(*line + len, *alloc - len, in))
|
|
|
41 |
return 0;
|
|
|
42 |
len += strlen(*line + len);
|
|
|
43 |
if (len && (*line)[len - 1] == '\n')
|
|
|
44 |
return len;
|
|
|
45 |
|
|
|
46 |
*alloc *= 2;
|
|
|
47 |
*line = xrealloc(*line, *alloc);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
#endif
|
|
|
51 |
|
|
|
52 |
/* fatal error & exit */
|
|
|
53 |
void fatal(const char *format, ...)
|
|
|
54 |
{
|
|
|
55 |
va_list args;
|
|
|
56 |
|
|
|
57 |
va_start(args, format);
|
|
|
58 |
fprintf(stderr, "%s: ", elf2flt_progname);
|
|
|
59 |
vfprintf(stderr, format, args);
|
|
|
60 |
fprintf(stderr, "\n");
|
|
|
61 |
va_end(args);
|
|
|
62 |
exit(1);
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/* fatal error, perror & exit */
|
|
|
66 |
void fatal_perror(const char *format, ...)
|
|
|
67 |
{
|
|
|
68 |
int e = errno;
|
|
|
69 |
va_list args;
|
|
|
70 |
|
|
|
71 |
va_start(args, format);
|
|
|
72 |
fprintf(stderr, "%s: ", elf2flt_progname);
|
|
|
73 |
vfprintf(stderr, format, args);
|
|
|
74 |
fprintf(stderr, ": %s\n", strerror(e));
|
|
|
75 |
va_end(args);
|
|
|
76 |
exit(1);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/* open a file or fail */
|
|
|
80 |
FILE *xfopen(const char *path, const char *mode)
|
|
|
81 |
{
|
|
|
82 |
FILE *ret = fopen(path, mode);
|
|
|
83 |
if (!ret)
|
|
|
84 |
fatal_perror("Unable to open '%s'", path);
|
|
|
85 |
return ret;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/* Append a string SRC to an options array DST */
|
|
|
89 |
void append_option(options_t *dst, const char *src)
|
|
|
90 |
{
|
|
|
91 |
if (dst->alloc == dst->num) {
|
|
|
92 |
size_t a = (dst->num + 2) * 2;
|
|
|
93 |
void *o = xmalloc(sizeof(*dst->options) * a);
|
|
|
94 |
|
|
|
95 |
memcpy(o, dst->options, sizeof(*dst->options) * dst->num);
|
|
|
96 |
free(dst->options);
|
|
|
97 |
dst->options = o;
|
|
|
98 |
dst->alloc = a;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
dst->options[dst->num] = src;
|
|
|
102 |
dst->num++;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/* Split and append a string SRC to an options array DST */
|
|
|
106 |
void append_option_str(options_t *dst, const char *src, const char *delim)
|
|
|
107 |
{
|
|
|
108 |
char *tok_src = xstrdup(src);
|
|
|
109 |
char *tok = strtok(tok_src, delim);
|
|
|
110 |
while (tok) {
|
|
|
111 |
append_option(dst, tok);
|
|
|
112 |
tok = strtok(NULL, delim);
|
|
|
113 |
}
|
|
|
114 |
/* don't free tok_src since options_t now points to it */
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/* Append an options array SRC to another options array DST */
|
|
|
118 |
void append_options(options_t *dst, const options_t *src)
|
|
|
119 |
{
|
|
|
120 |
if (dst->alloc < dst->num + src->num) {
|
|
|
121 |
size_t a = (dst->num + src->num + 2) * 2;
|
|
|
122 |
void *o = xmalloc(sizeof(*dst->options) * a);
|
|
|
123 |
|
|
|
124 |
memcpy(o, dst->options, sizeof(*dst->options) * dst->num);
|
|
|
125 |
free(dst->options);
|
|
|
126 |
dst->options = o;
|
|
|
127 |
dst->alloc = a;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
memcpy(&dst->options[dst->num], &src->options[0],
|
|
|
131 |
sizeof(*dst->options) * src->num);
|
|
|
132 |
dst->num += src->num;
|
|
|
133 |
}
|