2 Copyright (C) 2019 Free Software Foundation, Inc.
4 This file is part of libctf.
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
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.
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/>. */
24 #include <sys/types.h>
29 int _libctf_version = CTF_VERSION; /* Library client version. */
30 int _libctf_debug = 0; /* Debugging messages enabled. */
32 /* Private, read-only mmap from a file, with fallback to copying.
34 No handling of page-offset issues at all: the caller must allow for that. */
36 _libctf_malloc_ void *
37 ctf_mmap (size_t length, size_t offset, int fd)
42 data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
43 if (data == MAP_FAILED)
46 if ((data = malloc (length)) != NULL)
48 if (ctf_pread (fd, data, length, offset) <= 0)
59 ctf_munmap (void *buf, size_t length _libctf_unused_)
62 (void) munmap (buf, length);
68 _libctf_malloc_ void *
69 ctf_alloc (size_t size)
71 return (malloc (size));
81 ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
85 char *data = (char *) buf;
91 if (((len = pread (fd, data, count, offset)) < 0) &&
98 if (len == 0) /* EOF. */
109 if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
111 if ((lseek (fd, offset, SEEK_SET)) < 0)
117 if (((len = read (fd, data, count)) < 0) &&
124 if (len == 0) /* EOF. */
130 if ((lseek (fd, orig_off, SEEK_SET)) < 0)
131 return -1; /* offset is smashed. */
138 ctf_strerror (int err)
140 return (const char *) (strerror (err));
143 /* Set the CTF library client version to the specified version. If version is
144 zero, we just return the default library version number. */
146 ctf_version (int version)
156 /* Dynamic version switching is not presently supported. */
157 if (version != CTF_VERSION)
162 ctf_dprintf ("ctf_version: client using version %d\n", version);
163 _libctf_version = version;
166 return _libctf_version;
170 libctf_init_debug (void)
175 _libctf_debug = getenv ("LIBCTF_DEBUG") != NULL;
180 void ctf_setdebug (int debug)
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. */
186 _libctf_debug = debug;
187 ctf_dprintf ("CTF debugging set to %i\n", debug);
190 int ctf_getdebug (void)
192 return _libctf_debug;
195 _libctf_printflike_ (1, 2)
196 void ctf_dprintf (const char *format, ...)
202 va_start (alist, format);
204 (void) fputs ("libctf DEBUG: ", stderr);
205 (void) vfprintf (stderr, format, alist);