mirror of
https://github.com/bminor/binutils-gdb.git
synced 2025-12-05 23:23:09 +00:00
This file is a bit of a grab-bag of dict-wide API functions like ctf_set_open_errno or ctf_errwarning_next and portabilty functions and wrappers like ctf_mmap or ctf_pread. Split the latter out, and move other dict-wide functions that got stuck in ctf-util.c (because it was so hard to tell the two files apart) into ctf-api.c where they belong.
123 lines
2.6 KiB
C
123 lines
2.6 KiB
C
/* Portability and helper wrappers.
|
|
Copyright (C) 2019-2024 Free Software Foundation, Inc.
|
|
|
|
This file is part of libctf.
|
|
|
|
libctf is free software; you can redistribute it and/or modify it under
|
|
the terms of the GNU General Public License as published by the Free
|
|
Software Foundation; either version 3, or (at your option) any later
|
|
version.
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; see the file COPYING. If not see
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
#include <ctf-impl.h>
|
|
#ifdef HAVE_MMAP
|
|
#include <sys/mman.h>
|
|
#endif
|
|
#include <sys/types.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
/* Private, read-only mmap from a file, with fallback to copying.
|
|
|
|
No handling of page-offset issues at all: the caller must allow for that. */
|
|
|
|
_libctf_malloc_ void *
|
|
ctf_mmap (size_t length, size_t offset, int fd)
|
|
{
|
|
void *data;
|
|
|
|
#ifdef HAVE_MMAP
|
|
data = mmap (NULL, length, PROT_READ, MAP_PRIVATE, fd, offset);
|
|
if (data == MAP_FAILED)
|
|
data = NULL;
|
|
#else
|
|
if ((data = malloc (length)) != NULL)
|
|
{
|
|
if (ctf_pread (fd, data, length, offset) <= 0)
|
|
{
|
|
free (data);
|
|
data = NULL;
|
|
}
|
|
}
|
|
#endif
|
|
return data;
|
|
}
|
|
|
|
void
|
|
ctf_munmap (void *buf, size_t length _libctf_unused_)
|
|
{
|
|
#ifdef HAVE_MMAP
|
|
(void) munmap (buf, length);
|
|
#else
|
|
free (buf);
|
|
#endif
|
|
}
|
|
|
|
/* pread() implementation, retrying on short reads. */
|
|
|
|
ssize_t
|
|
ctf_pread (int fd, void *buf, ssize_t count, off_t offset)
|
|
{
|
|
ssize_t len;
|
|
size_t acc = 0;
|
|
char *data = (char *) buf;
|
|
|
|
#ifdef HAVE_PREAD
|
|
while (count > 0)
|
|
{
|
|
errno = 0;
|
|
if (((len = pread (fd, data, count, offset)) < 0) &&
|
|
errno != EINTR)
|
|
return len;
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
acc += len;
|
|
if (len == 0) /* EOF. */
|
|
return acc;
|
|
|
|
count -= len;
|
|
offset += len;
|
|
data += len;
|
|
}
|
|
return acc;
|
|
#else
|
|
off_t orig_off;
|
|
|
|
if ((orig_off = lseek (fd, 0, SEEK_CUR)) < 0)
|
|
return -1;
|
|
if ((lseek (fd, offset, SEEK_SET)) < 0)
|
|
return -1;
|
|
|
|
while (count > 0)
|
|
{
|
|
errno = 0;
|
|
if (((len = read (fd, data, count)) < 0) &&
|
|
errno != EINTR)
|
|
return len;
|
|
if (errno == EINTR)
|
|
continue;
|
|
|
|
acc += len;
|
|
if (len == 0) /* EOF. */
|
|
break;
|
|
|
|
count -= len;
|
|
data += len;
|
|
}
|
|
if ((lseek (fd, orig_off, SEEK_SET)) < 0)
|
|
return -1; /* offset is smashed. */
|
|
#endif
|
|
|
|
return acc;
|
|
}
|