libctf: compilation failure on MinGW due to missing errno values
[deliverable/binutils-gdb.git] / libctf / ctf-subr.c
1 /* Simple subrs.
2 Copyright (C) 2019 Free Software Foundation, Inc.
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
29 #ifndef ENOTSUP
30 #define ENOTSUP ENOSYS
31 #endif
32
33 int _libctf_version = CTF_VERSION; /* Library client version. */
34 int _libctf_debug = 0; /* Debugging messages enabled. */
35
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 *
41 ctf_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
62 void
63 ctf_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
72 ssize_t
73 ctf_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
129 const char *
130 ctf_strerror (int err)
131 {
132 return (const char *) (strerror (err));
133 }
134
135 /* Set the CTF library client version to the specified version. If version is
136 zero, we just return the default library version number. */
137 int
138 ctf_version (int version)
139 {
140 if (version < 0)
141 {
142 errno = EINVAL;
143 return -1;
144 }
145
146 if (version > 0)
147 {
148 /* Dynamic version switching is not presently supported. */
149 if (version != CTF_VERSION)
150 {
151 errno = ENOTSUP;
152 return -1;
153 }
154 ctf_dprintf ("ctf_version: client using version %d\n", version);
155 _libctf_version = version;
156 }
157
158 return _libctf_version;
159 }
160
161 void
162 libctf_init_debug (void)
163 {
164 static int inited;
165 if (!inited)
166 {
167 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
168 inited = 1;
169 }
170 }
171
172 void ctf_setdebug (int debug)
173 {
174 /* Ensure that libctf_init_debug() has been called, so that we don't get our
175 debugging-on-or-off smashed by the next call. */
176
177 libctf_init_debug();
178 _libctf_debug = debug;
179 ctf_dprintf ("CTF debugging set to %i\n", debug);
180 }
181
182 int ctf_getdebug (void)
183 {
184 return _libctf_debug;
185 }
186
187 _libctf_printflike_ (1, 2)
188 void ctf_dprintf (const char *format, ...)
189 {
190 if (_libctf_debug)
191 {
192 va_list alist;
193
194 va_start (alist, format);
195 fflush (stdout);
196 (void) fputs ("libctf DEBUG: ", stderr);
197 (void) vfprintf (stderr, format, alist);
198 va_end (alist);
199 }
200 }
This page took 0.033105 seconds and 4 git commands to generate.