Last active
March 22, 2019 01:51
-
-
Save Amar1729/541e66dff14fec0100931b64f78b8f38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/gnulib/lib/Makefile.am b/gnulib/lib/Makefile.am | |
index 236b996..b881059 100644 | |
--- a/gnulib/lib/Makefile.am | |
+++ b/gnulib/lib/Makefile.am | |
@@ -1502,6 +1502,8 @@ EXTRA_libgnu_la_SOURCES += open.c | |
## end gnulib module open | |
+libgnu_la_SOURCES += open_memstream.c | |
+ | |
## begin gnulib module openat | |
diff --git a/gnulib/lib/error.c b/gnulib/lib/error.c | |
index 81788c7..868fcb0 100644 | |
--- a/gnulib/lib/error.c | |
+++ b/gnulib/lib/error.c | |
@@ -119,7 +119,11 @@ int strerror_r (int errnum, char *buf, size_t buflen); | |
# endif | |
# endif | |
+#if defined __APPLE__ && defined __MACH__ | |
+#define program_name (((char **)*_NSGetArgv())[0]) | |
+#else | |
#define program_name getprogname () | |
+#endif | |
# if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r | |
# define __strerror_r strerror_r | |
diff --git a/gnulib/lib/open_memstream.c b/gnulib/lib/open_memstream.c | |
index e69de29..f1b4338 100644 | |
--- /dev/null | |
+++ b/gnulib/lib/open_memstream.c | |
@@ -0,0 +1,186 @@ | |
+/* Open a write stream around a malloc'd string. | |
+ Copyright (C) 2010 Free Software Foundation, Inc. | |
+ | |
+ This program 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>. */ | |
+ | |
+/* Written by Eric Blake <address@hidden>, 2010. */ | |
+ | |
+#include <config.h> | |
+ | |
+/* Specification. */ | |
+#include <stdio.h> | |
+ | |
+#include <assert.h> | |
+#include <errno.h> | |
+#include <stdlib.h> | |
+#include <string.h> | |
+ | |
+#include "verify.h" | |
+ | |
+# define INITIAL_ALLOC 64 | |
+ | |
+struct data | |
+{ | |
+ char **buf; /* User's argument. */ | |
+ size_t *len; /* User's argument. Smaller of pos or eof. */ | |
+ size_t pos; /* Current position. */ | |
+ size_t eof; /* End-of-file position. */ | |
+ size_t allocated; /* Allocated size of *buf, always > eof. */ | |
+ char c; /* Temporary storage for byte overwritten by NUL, if pos < eof. */ | |
+}; | |
+typedef struct data data; | |
+ | |
+/* Stupid BSD interface uses int/int instead of ssize_t/size_t. */ | |
+verify (sizeof (int) <= sizeof (size_t)); | |
+verify (sizeof (int) <= sizeof (ssize_t)); | |
+ | |
+static int | |
+mem_write (void *c, const char *buf, int n) | |
+{ | |
+ data *cookie = c; | |
+ char *cbuf = *cookie->buf; | |
+ | |
+ /* Be sure we don't overflow. */ | |
+ if ((ssize_t) (cookie->pos + n) < 0) | |
+ { | |
+ errno = EFBIG; | |
+ return EOF; | |
+ } | |
+ /* Grow the buffer, if necessary. Use geometric growth to avoid | |
+ quadratic realloc behavior. Overallocate, to accomodate the | |
+ requirement to always place a trailing NUL not counted by length. | |
+ Thus, we want max(prev_size*1.5, cookie->pos+n+1). */ | |
+ if (cookie->allocated <= cookie->pos + n) | |
+ { | |
+ size_t newsize = cookie->allocated * 3 / 2; | |
+ if (newsize < cookie->pos + n + 1) | |
+ newsize = cookie->pos + n + 1; | |
+ cbuf = realloc (cbuf, newsize); | |
+ if (!cbuf) | |
+ return EOF; | |
+ *cookie->buf = cbuf; | |
+ cookie->allocated = newsize; | |
+ } | |
+ /* If we have previously done a seek beyond eof, ensure all | |
+ intermediate bytges are NUL. */ | |
+ if (cookie->eof < cookie->pos) | |
+ memset (cbuf + cookie->eof, '\0', cookie->pos - cookie->eof); | |
+ memcpy (cbuf + cookie->pos, buf, n); | |
+ cookie->pos += n; | |
+ /* If the user has previously written beyond the current position, | |
+ remember what the trailing NUL is overwriting. Otherwise, | |
+ extend the stream. */ | |
+ if (cookie->eof < cookie->pos) | |
+ cookie->eof = cookie->pos; | |
+ else | |
+ cookie->c = cbuf[cookie->pos]; | |
+ cbuf[cookie->pos] = '\0'; | |
+ *cookie->len = cookie->pos; | |
+ return n; | |
+} | |
+ | |
+static fpos_t | |
+mem_seek (void *c, fpos_t pos, int whence) | |
+{ | |
+ data *cookie = c; | |
+ off_t offset = pos; | |
+ | |
+ if (whence == SEEK_CUR) | |
+ offset += cookie->pos; | |
+ else if (whence == SEEK_END) | |
+ offset += cookie->eof; | |
+ if (offset < 0) | |
+ { | |
+ errno = EINVAL; | |
+ offset = -1; | |
+ } | |
+ else if ((size_t) offset != offset) | |
+ { | |
+ errno = ENOSPC; | |
+ offset = -1; | |
+ } | |
+ else | |
+ { | |
+ if (cookie->pos < cookie->eof) | |
+ { | |
+ (*cookie->buf)[cookie->pos] = cookie->c; | |
+ cookie->c = '\0'; | |
+ } | |
+ cookie->pos = offset; | |
+ if (cookie->pos < cookie->eof) | |
+ { | |
+ cookie->c = (*cookie->buf)[cookie->pos]; | |
+ (*cookie->buf)[cookie->pos] = '\0'; | |
+ *cookie->len = cookie->pos; | |
+ } | |
+ else | |
+ *cookie->len = cookie->eof; | |
+ } | |
+ return offset; | |
+} | |
+ | |
+static int | |
+mem_close (void *c) | |
+{ | |
+ data *cookie = c; | |
+ char *buf; | |
+ | |
+ /* Be nice and try to reduce excess memory. */ | |
+ buf = realloc (*cookie->buf, *cookie->len + 1); | |
+ if (buf) | |
+ *cookie->buf = buf; | |
+ free (cookie); | |
+ return 0; | |
+} | |
+ | |
+FILE * | |
+open_memstream (char **buf, size_t *len) | |
+{ | |
+ FILE *f; | |
+ data *cookie; | |
+ | |
+ if (!buf || !len) | |
+ { | |
+ errno = EINVAL; | |
+ return NULL; | |
+ } | |
+ if (!(cookie = malloc (sizeof *cookie))) | |
+ return NULL; | |
+ if (!(*buf = malloc (INITIAL_ALLOC))) | |
+ { | |
+ free (cookie); | |
+ errno = ENOMEM; | |
+ return NULL; | |
+ } | |
+ **buf = '\0'; | |
+ *len = 0; | |
+ | |
+ f = funopen (cookie, NULL, mem_write, mem_seek, mem_close); | |
+ if (!f) | |
+ { | |
+ int saved_errno = errno; | |
+ free (cookie); | |
+ errno = saved_errno; | |
+ } | |
+ else | |
+ { | |
+ cookie->buf = buf; | |
+ cookie->len = len; | |
+ cookie->pos = 0; | |
+ cookie->eof = 0; | |
+ cookie->c = '\0'; | |
+ cookie->allocated = INITIAL_ALLOC; | |
+ } | |
+ return f; | |
+} | |
diff --git a/gnulib/lib/stdio.in.h b/gnulib/lib/stdio.in.h | |
index b9a43bb..429668c 100644 | |
--- a/gnulib/lib/stdio.in.h | |
+++ b/gnulib/lib/stdio.in.h | |
@@ -798,6 +798,8 @@ _GL_CXXALIAS_SYS (obstack_vprintf, int, | |
_GL_CXXALIASWARN (obstack_vprintf); | |
#endif | |
+_GL_FUNCDECL_SYS (open_memstream, FILE *, (char **, size_t *)); | |
+ | |
#if @GNULIB_PCLOSE@ | |
# if !@HAVE_PCLOSE@ | |
_GL_FUNCDECL_SYS (pclose, int, (FILE *stream) _GL_ARG_NONNULL ((1))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 5 removes changes that were being made to tests: those made
make check
fail weirdly.