From: Martin Matuska Date: Mon, 24 Jul 2023 11:35:50 +0000 (+0200) Subject: unzip: add NetBSD implementation of getline() if not supported X-Git-Tag: v3.7.1~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5397c4ed5c922f07247190f37028582ccf2684e4;p=thirdparty%2Flibarchive.git unzip: add NetBSD implementation of getline() if not supported Fixes #1933 --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 646484b79..909fef708 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1386,6 +1386,7 @@ CHECK_FUNCTION_EXISTS_GLIBC(futimesat HAVE_FUTIMESAT) CHECK_FUNCTION_EXISTS_GLIBC(geteuid HAVE_GETEUID) CHECK_FUNCTION_EXISTS_GLIBC(getgrgid_r HAVE_GETGRGID_R) CHECK_FUNCTION_EXISTS_GLIBC(getgrnam_r HAVE_GETGRNAM_R) +CHECK_FUNCTION_EXISTS_GLIBC(getline HAVE_GETLINE) CHECK_FUNCTION_EXISTS_GLIBC(getpwnam_r HAVE_GETPWNAM_R) CHECK_FUNCTION_EXISTS_GLIBC(getpwuid_r HAVE_GETPWUID_R) CHECK_FUNCTION_EXISTS_GLIBC(getpid HAVE_GETPID) diff --git a/Makefile.am b/Makefile.am index b93f12320..e8b676443 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1451,7 +1451,9 @@ bsdcat_test_EXTRA_DIST= \ bsdunzip_SOURCES= \ unzip/bsdunzip.c \ - unzip/bsdunzip_platform.h + unzip/bsdunzip_platform.h \ + unzip/la_getline.c \ + unzip/la_queue.h if INC_WINDOWS_FILES bsdunzip_SOURCES+= diff --git a/build/cmake/config.h.in b/build/cmake/config.h.in index 3de2ebd8c..daa5e387a 100644 --- a/build/cmake/config.h.in +++ b/build/cmake/config.h.in @@ -642,6 +642,9 @@ typedef uint64_t uintmax_t; /* Define to 1 if you have the `getgrnam_r' function. */ #cmakedefine HAVE_GETGRNAM_R 1 +/* Define to 1 if you have the `getline' function. */ +#cmakedefine HAVE_GETLINE 1 + /* Define to 1 if platform uses `optreset` to reset `getopt` */ #cmakedefine HAVE_GETOPT_OPTRESET 1 diff --git a/configure.ac b/configure.ac index 435c2e702..d4377f054 100644 --- a/configure.ac +++ b/configure.ac @@ -742,7 +742,7 @@ AC_CHECK_FUNCS([arc4random_buf chflags chown chroot ctime_r]) AC_CHECK_FUNCS([fchdir fchflags fchmod fchown fcntl fdopendir fnmatch fork]) AC_CHECK_FUNCS([fstat fstatat fstatfs fstatvfs ftruncate]) AC_CHECK_FUNCS([futimens futimes futimesat]) -AC_CHECK_FUNCS([geteuid getpid getgrgid_r getgrnam_r]) +AC_CHECK_FUNCS([geteuid getline getpid getgrgid_r getgrnam_r]) AC_CHECK_FUNCS([getpwnam_r getpwuid_r getvfsbyname gmtime_r]) AC_CHECK_FUNCS([lchflags lchmod lchown link linkat localtime_r lstat lutimes]) AC_CHECK_FUNCS([mbrtowc memmove memset]) diff --git a/unzip/CMakeLists.txt b/unzip/CMakeLists.txt index 13b983d89..0e3de3523 100644 --- a/unzip/CMakeLists.txt +++ b/unzip/CMakeLists.txt @@ -8,6 +8,8 @@ IF(ENABLE_UNZIP) SET(bsdunzip_SOURCES bsdunzip.c bsdunzip_platform.h + la_getline.c + la_queue.h ../libarchive_fe/err.c ../libarchive_fe/err.h ../libarchive_fe/lafe_platform.h diff --git a/unzip/la_getline.c b/unzip/la_getline.c new file mode 100644 index 000000000..94ac1cef4 --- /dev/null +++ b/unzip/la_getline.c @@ -0,0 +1,99 @@ +/* $NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp $ */ + +/*- + * Copyright (c) 2011 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef HAVE_GETLINE +#include "bsdunzip_platform.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +static ssize_t +la_getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) +{ + char *ptr, *eptr; + + + if (*buf == NULL || *bufsiz == 0) { + *bufsiz = BUFSIZ; + if ((*buf = malloc(*bufsiz)) == NULL) + return -1; + } + + for (ptr = *buf, eptr = *buf + *bufsiz;;) { + int c = fgetc(fp); + if (c == -1) { + if (feof(fp)) { + ssize_t diff = (ssize_t)(ptr - *buf); + if (diff != 0) { + *ptr = '\0'; + return diff; + } + } + return -1; + } + *ptr++ = c; + if (c == delimiter) { + *ptr = '\0'; + return ptr - *buf; + } + if (ptr + 2 >= eptr) { + char *nbuf; + size_t nbufsiz = *bufsiz * 2; + ssize_t d = ptr - *buf; + if ((nbuf = realloc(*buf, nbufsiz)) == NULL) + return -1; + *buf = nbuf; + *bufsiz = nbufsiz; + eptr = nbuf + nbufsiz; + ptr = nbuf + d; + } + } +} + +ssize_t +getline(char **buf, size_t *bufsiz, FILE *fp) +{ + return la_getdelim(buf, bufsiz, '\n', fp); +} +#endif