Release 2.33.1
[external/binutils.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 int _libctf_version = CTF_VERSION;            /* Library client version.  */
30 int _libctf_debug = 0;                        /* Debugging messages enabled.  */
31
32 /* Private, read-only mmap from a file, with fallback to copying.
33
34    No handling of page-offset issues at all: the caller must allow for that. */
35
36 _libctf_malloc_ void *
37 ctf_mmap (size_t length, size_t offset, int fd)
38 {
39   void *data;
40
41 #ifdef HAVE_MMAP
42   data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
43   if (data == MAP_FAILED)
44     data = NULL;
45 #else
46   if ((data = malloc (length)) != NULL)
47     {
48       if (ctf_pread (fd, data, length, offset) <= 0)
49         {
50           free (data);
51           data = NULL;
52         }
53     }
54 #endif
55   return data;
56 }
57
58 void
59 ctf_munmap (void *buf, size_t length _libctf_unused_)
60 {
61 #ifdef HAVE_MMAP
62   (void) munmap (buf, length);
63 #else
64   free (buf);
65 #endif
66 }
67
68 _libctf_malloc_ void *
69 ctf_alloc (size_t size)
70 {
71   return (malloc (size));
72 }
73
74 void
75 ctf_free (void *buf)
76 {
77   free (buf);
78 }
79
80 ssize_t
81 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
82 {
83   ssize_t len;
84   size_t acc = 0;
85   char *data = (char *) buf;
86
87 #ifdef HAVE_PREAD
88   while (count > 0)
89     {
90       errno = 0;
91       if (((len = pread (fd, data, count, offset)) < 0) &&
92           errno != EINTR)
93           return len;
94       if (errno == EINTR)
95         continue;
96
97       acc += len;
98       if (len == 0)                             /* EOF.  */
99         return acc;
100
101       count -= len;
102       offset += len;
103       data += len;
104     }
105   return acc;
106 #else
107   off_t orig_off;
108
109   if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
110     return -1;
111   if ((lseek (fd, offset, SEEK_SET)) < 0)
112     return -1;
113
114   while (count > 0)
115     {
116       errno = 0;
117       if (((len = read (fd, data, count)) < 0) &&
118           errno != EINTR)
119           return len;
120       if (errno == EINTR)
121         continue;
122
123       acc += len;
124       if (len == 0)                             /* EOF.  */
125         break;
126
127       count -= len;
128       data += len;
129     }
130   if ((lseek (fd, orig_off, SEEK_SET)) < 0)
131     return -1;                                  /* offset is smashed.  */
132 #endif
133
134   return acc;
135 }
136
137 const char *
138 ctf_strerror (int err)
139 {
140   return (const char *) (strerror (err));
141 }
142
143 /* Set the CTF library client version to the specified version.  If version is
144    zero, we just return the default library version number.  */
145 int
146 ctf_version (int version)
147 {
148   if (version < 0)
149     {
150       errno = EINVAL;
151       return -1;
152     }
153
154   if (version > 0)
155     {
156       /*  Dynamic version switching is not presently supported. */
157       if (version != CTF_VERSION)
158         {
159           errno = ENOTSUP;
160           return -1;
161         }
162       ctf_dprintf ("ctf_version: client using version %d\n", version);
163       _libctf_version = version;
164     }
165
166   return _libctf_version;
167 }
168
169 void
170 libctf_init_debug (void)
171 {
172   static int inited;
173   if (!inited)
174     {
175       _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
176       inited = 1;
177     }
178 }
179
180 void ctf_setdebug (int debug)
181 {
182   /* Ensure that libctf_init_debug() has been called, so that we don't get our
183      debugging-on-or-off smashed by the next call.  */
184
185   libctf_init_debug();
186   _libctf_debug = debug;
187   ctf_dprintf ("CTF debugging set to %i\n", debug);
188 }
189
190 int ctf_getdebug (void)
191 {
192   return _libctf_debug;
193 }
194
195 _libctf_printflike_ (1, 2)
196 void ctf_dprintf (const char *format, ...)
197 {
198   if (_libctf_debug)
199     {
200       va_list alist;
201
202       va_start (alist, format);
203       fflush (stdout);
204       (void) fputs ("libctf DEBUG: ", stderr);
205       (void) vfprintf (stderr, format, alist);
206       va_end (alist);
207     }
208 }