2014-06-10 03:19:25 +08:00
|
|
|
/* fmemopen implementation.
|
2024-01-02 02:12:26 +08:00
|
|
|
Copyright (C) 2015-2024 Free Software Foundation, Inc.
|
2000-08-29 10:54:55 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-08-29 10:54:55 +08:00
|
|
|
|
|
|
|
The GNU C Library 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
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-10 07:18:22 +08:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
/* fmemopen() from 2.22 and forward works as defined by POSIX. It also
|
|
|
|
provides an older symbol, version 2.2.5, that behaves different regarding
|
|
|
|
SEEK_END (libio/oldfmemopen.c). */
|
|
|
|
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2000-10-31 13:20:53 +08:00
|
|
|
#include <errno.h>
|
2000-08-29 10:54:55 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2005-02-03 03:44:07 +08:00
|
|
|
#include <stdint.h>
|
2000-08-29 10:54:55 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
Update.
2002-08-04 Jakub Jelinek <jakub@redhat.com>
* manual/debug.texi: Fix spelling to programmatically.
Reported by <hayastan132@hotmail.com>.
* include/libio.h: Add libc_hidden_proto for __uflow.
* include/stdio.h: Map fopen, fdopen, fclose, fputs, fsetpos, and
fgetpos to _IO_* names.
Add libc_hidden_proto for fileno, fwrite, fseek, fflush_unlocked,
fread_unlocked, fwrite_unlocked, fgets_unlocked, fputs_unlocked.
* include/wchar.h: Add libc_hidden_proto for fputws_unlocked,
putwc_unlocked, vswscanf.
* libio/iolibio.h: Add libc_hidden_proto for _IO_fputs.
* libio/fileno.c: Use <stdio.h> and libc_hidden_def.
* libio/fseek.c: Likewise.
* libio/fmemopen.c: Include "libioP.h". Call _IO_fopencookie and
not fopencookie.
* libio/genops.c (__uflow): Add libc_hidden_def.
* libio/iofflush_u.c (fflush_unlocked): Likewise.
* libio/iofgets_u.c (fgets_unlocked): Likewise.
* libio/iofputs_u.c (fputs_unlocked): Likewise.
* libio/iofputws_u.c (fputws_unlocked): Likewise.
* libio/iofread_u.c (fread_unlocked): Likewise.
* libio/iofwrite_u.c (fwrite_unlocked): Likewise.
* libio/iovswscanf.c (vswscanf): Likewise.
* libio/putwc_u.c (putwc_unlocked): Likewise.
* libio/iofputs.c: Use libc_hidden_def instead of INTDEF.
* malloc/malloc.c: Redirect fwrite calls to _IO_fwrite.
* malloc/mtrace.c: Likewise.
* sunrpc/clnt_perr.c: Remove fputs macro.
* sunrpc/svc_simple.c: Likewise.
* sunrpc/svc_tcp.c: Likewise.
* sunrpc/svc_udp.c: Likewise.
* sunrpc/xdr_rec.c: Likewise.
* sunrpc/xdr_ref.c: Likewise.
* iconv/Makefile: Add CPPFLAGS definitions with -DNOT_in_libc for
iconv_prog, linereader, and charmap-dir.
* locale/Makefile: Likewise for locale and charmap-dir.
* malloc/Makefile: Likewise for memusagestat.
* nscd/Makefile: Likewise for nscd, nscd_conf, and dbg_log.
* sunrpc/Makefile: Likewise for rpc_main.
* sysdeps/unix/sysv/linux/Makefile: Likewise for lddlibc4.
* timezone/Makefile: Likewise for zic.
* stdio-common/perror.c: Avoid multiple calls to fileno_unlocked.
2002-08-05 04:54:20 +08:00
|
|
|
#include "libioP.h"
|
|
|
|
|
2000-08-29 10:54:55 +08:00
|
|
|
|
|
|
|
typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
|
|
|
|
struct fmemopen_cookie_struct
|
|
|
|
{
|
2014-06-10 03:19:25 +08:00
|
|
|
char *buffer; /* memory buffer. */
|
|
|
|
int mybuffer; /* allocated my buffer? */
|
|
|
|
int append; /* buffer open for append? */
|
|
|
|
size_t size; /* buffer length in bytes. */
|
2018-02-08 07:42:04 +08:00
|
|
|
off64_t pos; /* current position at the buffer. */
|
2014-06-10 03:19:25 +08:00
|
|
|
size_t maxpos; /* max position in buffer. */
|
2000-08-29 10:54:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-16 00:03:57 +08:00
|
|
|
static ssize_t
|
2000-08-29 10:54:55 +08:00
|
|
|
fmemopen_read (void *cookie, char *b, size_t s)
|
|
|
|
{
|
2014-06-10 03:19:25 +08:00
|
|
|
fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
if (c->pos + s > c->maxpos)
|
2000-08-29 10:54:55 +08:00
|
|
|
{
|
2016-04-27 22:51:01 +08:00
|
|
|
s = c->maxpos - c->pos;
|
|
|
|
if ((size_t) c->pos > c->maxpos)
|
|
|
|
s = 0;
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (b, &(c->buffer[c->pos]), s);
|
|
|
|
|
|
|
|
c->pos += s;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-16 00:03:57 +08:00
|
|
|
static ssize_t
|
2000-08-29 10:54:55 +08:00
|
|
|
fmemopen_write (void *cookie, const char *b, size_t s)
|
|
|
|
{
|
2014-06-10 03:19:25 +08:00
|
|
|
fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;;
|
2018-02-08 07:42:04 +08:00
|
|
|
off64_t pos = c->append ? c->maxpos : c->pos;
|
2016-04-27 22:51:01 +08:00
|
|
|
int addnullc = (s == 0 || b[s - 1] != '\0');
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2016-04-27 22:51:01 +08:00
|
|
|
if (pos + s > c->size)
|
2000-08-29 10:54:55 +08:00
|
|
|
{
|
2015-06-25 17:53:06 +08:00
|
|
|
if ((size_t) (c->pos + addnullc) >= c->size)
|
2000-10-31 13:20:53 +08:00
|
|
|
{
|
|
|
|
__set_errno (ENOSPC);
|
2005-09-24 00:36:09 +08:00
|
|
|
return 0;
|
2000-10-31 13:20:53 +08:00
|
|
|
}
|
2016-04-27 22:51:01 +08:00
|
|
|
s = c->size - pos;
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
memcpy (&(c->buffer[pos]), b, s);
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2016-04-27 22:51:01 +08:00
|
|
|
c->pos = pos + s;
|
2016-04-27 04:40:25 +08:00
|
|
|
if ((size_t) c->pos > c->maxpos)
|
2000-08-29 10:54:55 +08:00
|
|
|
{
|
2016-04-27 04:40:25 +08:00
|
|
|
c->maxpos = c->pos;
|
2016-04-27 22:51:01 +08:00
|
|
|
if (c->maxpos < c->size && addnullc)
|
2000-08-29 10:54:55 +08:00
|
|
|
c->buffer[c->maxpos] = '\0';
|
2016-04-27 22:51:01 +08:00
|
|
|
/* A null byte is written in a stream open for update iff it fits. */
|
|
|
|
else if (c->append == 0 && addnullc != 0)
|
|
|
|
c->buffer[c->size-1] = '\0';
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-16 00:03:57 +08:00
|
|
|
static int
|
2018-02-08 07:42:04 +08:00
|
|
|
fmemopen_seek (void *cookie, off64_t *p, int w)
|
2000-08-29 10:54:55 +08:00
|
|
|
{
|
2018-02-08 07:42:04 +08:00
|
|
|
off64_t np;
|
2014-06-10 03:19:25 +08:00
|
|
|
fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
|
|
|
switch (w)
|
|
|
|
{
|
|
|
|
case SEEK_SET:
|
|
|
|
np = *p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
np = c->pos + *p;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
2014-06-10 03:19:25 +08:00
|
|
|
np = c->maxpos + *p;
|
2000-08-29 10:54:55 +08:00
|
|
|
break;
|
|
|
|
|
2000-09-02 05:16:28 +08:00
|
|
|
default:
|
|
|
|
return -1;
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
|
|
|
|
2002-08-25 12:51:08 +08:00
|
|
|
if (np < 0 || (size_t) np > c->size)
|
2016-04-27 22:51:01 +08:00
|
|
|
{
|
|
|
|
__set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2005-01-06 08:42:22 +08:00
|
|
|
*p = c->pos = np;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2005-01-06 08:42:22 +08:00
|
|
|
return 0;
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-16 00:03:57 +08:00
|
|
|
static int
|
2000-08-29 10:54:55 +08:00
|
|
|
fmemopen_close (void *cookie)
|
|
|
|
{
|
2014-06-10 03:19:25 +08:00
|
|
|
fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2000-09-02 23:05:15 +08:00
|
|
|
if (c->mybuffer)
|
|
|
|
free (c->buffer);
|
2000-08-29 10:54:55 +08:00
|
|
|
free (c);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FILE *
|
2014-06-10 03:19:25 +08:00
|
|
|
__fmemopen (void *buf, size_t len, const char *mode)
|
2000-08-29 10:54:55 +08:00
|
|
|
{
|
|
|
|
cookie_io_functions_t iof;
|
|
|
|
fmemopen_cookie_t *c;
|
2015-08-13 14:51:04 +08:00
|
|
|
FILE *result;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2015-07-16 03:15:47 +08:00
|
|
|
c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
|
2000-08-29 10:54:55 +08:00
|
|
|
if (c == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
c->mybuffer = (buf == NULL);
|
|
|
|
|
|
|
|
if (c->mybuffer)
|
|
|
|
{
|
|
|
|
c->buffer = (char *) malloc (len);
|
|
|
|
if (c->buffer == NULL)
|
|
|
|
{
|
|
|
|
free (c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->buffer[0] = '\0';
|
|
|
|
}
|
|
|
|
else
|
2005-01-06 10:09:15 +08:00
|
|
|
{
|
2014-02-10 21:45:42 +08:00
|
|
|
if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
|
2006-04-07 12:29:25 +08:00
|
|
|
{
|
|
|
|
free (c);
|
2014-06-10 03:19:25 +08:00
|
|
|
__set_errno (EINVAL);
|
|
|
|
return NULL;
|
2006-04-07 12:29:25 +08:00
|
|
|
}
|
2005-01-06 10:09:15 +08:00
|
|
|
|
|
|
|
c->buffer = buf;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
/* POSIX states that w+ mode should truncate the buffer. */
|
|
|
|
if (mode[0] == 'w' && mode[1] == '+')
|
2011-03-04 13:49:46 +08:00
|
|
|
c->buffer[0] = '\0';
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2015-07-16 03:15:47 +08:00
|
|
|
if (mode[0] == 'a')
|
|
|
|
c->maxpos = strnlen (c->buffer, len);
|
2011-03-04 13:49:46 +08:00
|
|
|
}
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
|
|
|
|
/* Mode | starting position (cookie::pos) | size (cookie::size)
|
|
|
|
------ |----------------------------------|-----------------------------
|
|
|
|
read | beginning of the buffer | size argument
|
|
|
|
write | beginning of the buffer | zero
|
|
|
|
append | first null or size buffer + 1 | first null or size argument
|
|
|
|
*/
|
|
|
|
|
2011-03-04 13:49:46 +08:00
|
|
|
c->size = len;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
2014-06-10 03:19:25 +08:00
|
|
|
if (mode[0] == 'r')
|
|
|
|
c->maxpos = len;
|
|
|
|
|
|
|
|
c->append = mode[0] == 'a';
|
|
|
|
if (c->append)
|
2000-09-02 05:16:28 +08:00
|
|
|
c->pos = c->maxpos;
|
2000-08-29 10:54:55 +08:00
|
|
|
else
|
2000-09-02 05:16:28 +08:00
|
|
|
c->pos = 0;
|
2000-08-29 10:54:55 +08:00
|
|
|
|
|
|
|
iof.read = fmemopen_read;
|
|
|
|
iof.write = fmemopen_write;
|
|
|
|
iof.seek = fmemopen_seek;
|
|
|
|
iof.close = fmemopen_close;
|
|
|
|
|
2015-08-13 14:51:04 +08:00
|
|
|
result = _IO_fopencookie (c, mode, iof);
|
|
|
|
if (__glibc_unlikely (result == NULL))
|
|
|
|
{
|
|
|
|
if (c->mybuffer)
|
|
|
|
free (c->buffer);
|
|
|
|
|
|
|
|
free (c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2000-08-29 10:54:55 +08:00
|
|
|
}
|
2014-06-10 03:19:25 +08:00
|
|
|
libc_hidden_def (__fmemopen)
|
|
|
|
versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22);
|