libctf: compilation failure on MinGW due to missing errno values
[deliverable/binutils-gdb.git] / libctf / ctf-subr.c
CommitLineData
60da9d95 1/* Simple subrs.
b3adc24a 2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
60da9d95
NA
3
4 This file is part of libctf.
5
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
19
20#include <ctf-impl.h>
21#ifdef HAVE_MMAP
22#include <sys/mman.h>
23#endif
24#include <sys/types.h>
25#include <stdarg.h>
26#include <string.h>
27#include <unistd.h>
28
555adca2
EZ
29#ifndef ENOTSUP
30#define ENOTSUP ENOSYS
31#endif
32
6c33b742 33int _libctf_version = CTF_VERSION; /* Library client version. */
60da9d95
NA
34int _libctf_debug = 0; /* Debugging messages enabled. */
35
60da9d95
NA
36/* Private, read-only mmap from a file, with fallback to copying.
37
38 No handling of page-offset issues at all: the caller must allow for that. */
39
40_libctf_malloc_ void *
41ctf_mmap (size_t length, size_t offset, int fd)
42{
43 void *data;
44
45#ifdef HAVE_MMAP
46 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
47 if (data == MAP_FAILED)
48 data = NULL;
49#else
50 if ((data = malloc (length)) != NULL)
51 {
52 if (ctf_pread (fd, data, length, offset) <= 0)
53 {
54 free (data);
55 data = NULL;
56 }
57 }
58#endif
59 return data;
60}
61
62void
63ctf_munmap (void *buf, size_t length _libctf_unused_)
64{
65#ifdef HAVE_MMAP
66 (void) munmap (buf, length);
67#else
68 free (buf);
69#endif
70}
71
60da9d95
NA
72ssize_t
73ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
74{
75 ssize_t len;
76 size_t acc = 0;
77 char *data = (char *) buf;
78
79#ifdef HAVE_PREAD
80 while (count > 0)
81 {
82 errno = 0;
83 if (((len = pread (fd, data, count, offset)) < 0) &&
84 errno != EINTR)
85 return len;
86 if (errno == EINTR)
87 continue;
88
89 acc += len;
90 if (len == 0) /* EOF. */
91 return acc;
92
93 count -= len;
94 offset += len;
95 data += len;
96 }
97 return acc;
98#else
99 off_t orig_off;
100
101 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
102 return -1;
103 if ((lseek (fd, offset, SEEK_SET)) < 0)
104 return -1;
105
106 while (count > 0)
107 {
108 errno = 0;
109 if (((len = read (fd, data, count)) < 0) &&
110 errno != EINTR)
111 return len;
112 if (errno == EINTR)
113 continue;
114
115 acc += len;
116 if (len == 0) /* EOF. */
117 break;
118
119 count -= len;
120 data += len;
121 }
122 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
123 return -1; /* offset is smashed. */
124#endif
125
126 return acc;
127}
128
6c33b742
NA
129/* Set the CTF library client version to the specified version. If version is
130 zero, we just return the default library version number. */
131int
132ctf_version (int version)
133{
134 if (version < 0)
135 {
136 errno = EINVAL;
137 return -1;
138 }
139
140 if (version > 0)
141 {
142 /* Dynamic version switching is not presently supported. */
143 if (version != CTF_VERSION)
144 {
145 errno = ENOTSUP;
146 return -1;
147 }
148 ctf_dprintf ("ctf_version: client using version %d\n", version);
149 _libctf_version = version;
150 }
151
152 return _libctf_version;
153}
154
60da9d95
NA
155void
156libctf_init_debug (void)
157{
158 static int inited;
159 if (!inited)
160 {
161 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
162 inited = 1;
163 }
164}
165
166void ctf_setdebug (int debug)
167{
168 /* Ensure that libctf_init_debug() has been called, so that we don't get our
169 debugging-on-or-off smashed by the next call. */
170
171 libctf_init_debug();
172 _libctf_debug = debug;
173 ctf_dprintf ("CTF debugging set to %i\n", debug);
174}
175
176int ctf_getdebug (void)
177{
178 return _libctf_debug;
179}
180
181_libctf_printflike_ (1, 2)
182void ctf_dprintf (const char *format, ...)
183{
5ec7465f 184 if (_libctf_unlikely_ (_libctf_debug))
60da9d95
NA
185 {
186 va_list alist;
187
188 va_start (alist, format);
189 fflush (stdout);
190 (void) fputs ("libctf DEBUG: ", stderr);
191 (void) vfprintf (stderr, format, alist);
192 va_end (alist);
193 }
194}
8b37e7b6
NA
195
196/* Errors and warnings. */
197_libctf_printflike_ (3, 4)
198extern void
199ctf_err_warn (ctf_file_t *fp, int is_warning, const char *format, ...)
200{
201 va_list alist;
202 ctf_err_warning_t *cew;
203
204 /* Don't bother reporting errors here: we can't do much about them if they
205 happen. If we're so short of memory that a tiny malloc doesn't work, a
206 vfprintf isn't going to work either and the caller will have to rely on the
207 ENOMEM return they'll be getting in short order anyway. */
208
209 if ((cew = malloc (sizeof (ctf_err_warning_t))) == NULL)
210 return;
211
212 cew->cew_is_warning = is_warning;
213 va_start (alist, format);
214 if (vasprintf (&cew->cew_text, format, alist) < 0)
215 {
216 free (cew);
217 va_end (alist);
218 return;
219 }
220 va_end (alist);
221
222 ctf_dprintf ("%s: %s\n", is_warning ? "error" : "warning", cew->cew_text);
223
224 ctf_list_append (&fp->ctf_errs_warnings, cew);
225}
226
227/* Error-warning reporting: an 'iterator' that returns errors and warnings from
228 the error/warning list, in order of emission. Errors and warnings are popped
229 after return: the caller must free the returned error-text pointer. */
230char *
231ctf_errwarning_next (ctf_file_t *fp, ctf_next_t **it, int *is_warning)
232{
233 ctf_next_t *i = *it;
234 char *ret;
235 ctf_err_warning_t *cew;
236
237 if (!i)
238 {
239 if ((i = ctf_next_create ()) == NULL)
240 {
241 ctf_set_errno (fp, ENOMEM);
242 return NULL;
243 }
244
245 i->cu.ctn_fp = fp;
246 i->ctn_iter_fun = (void (*) (void)) ctf_errwarning_next;
247 *it = i;
248 }
249
250 if ((void (*) (void)) ctf_errwarning_next != i->ctn_iter_fun)
251 {
252 ctf_set_errno (fp, ECTF_NEXT_WRONGFUN);
253 return NULL;
254 }
255
256 if (fp != i->cu.ctn_fp)
257 {
258 ctf_set_errno (fp, ECTF_NEXT_WRONGFP);
259 return NULL;
260 }
261
262 cew = ctf_list_next (&fp->ctf_errs_warnings);
263
264 if (!cew)
265 {
266 ctf_next_destroy (i);
267 *it = NULL;
268 ctf_set_errno (fp, ECTF_NEXT_END);
269 return NULL;
270 }
271
272 if (is_warning)
273 *is_warning = cew->cew_is_warning;
274 ret = cew->cew_text;
275 ctf_list_delete (&fp->ctf_errs_warnings, cew);
276 free (cew);
277 return ret;
278}
279
280void
281ctf_assert_fail_internal (ctf_file_t *fp, const char *file, size_t line,
282 const char *exprstr)
283{
284 ctf_err_warn (fp, 0, "%s: %lu: libctf assertion failed: %s", file,
285 (long unsigned int) line, exprstr);
286 ctf_set_errno (fp, ECTF_INTERNAL);
287}
This page took 0.120544 seconds and 4 git commands to generate.