]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
unzip: add NetBSD implementation of getline() if not supported
authorMartin Matuska <martin@matuska.de>
Mon, 24 Jul 2023 11:35:50 +0000 (13:35 +0200)
committerMartin Matuska <martin@matuska.de>
Mon, 24 Jul 2023 11:43:48 +0000 (13:43 +0200)
Fixes #1933

CMakeLists.txt
Makefile.am
build/cmake/config.h.in
configure.ac
unzip/CMakeLists.txt
unzip/la_getline.c [new file with mode: 0644]

index 646484b79ed358dd9e82c33d12c1c3bad71a886a..909fef70838e78c4974826e7b5ebfa394839836b 100644 (file)
@@ -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)
index b93f1232003d1d96a6507ddfd44c484eb0b75f8a..e8b676443f1182f8386c241e9d28643af025e999 100644 (file)
@@ -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+=
index 3de2ebd8c0ac31a7b1e2bec445f4e4899dd4aba0..daa5e387ae704eb277a4be845d080322573cd288 100644 (file)
@@ -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
 
index 435c2e70272e152fd212c04296e69945fbc3728e..d4377f05435503d0d02beafa4af1ab3fc790f1a7 100644 (file)
@@ -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])
index 13b983d89db7032cb377395448dec5289a432df9..0e3de35231df0a5d77d08a8b948af22992c0e162 100644 (file)
@@ -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 (file)
index 0000000..94ac1ce
--- /dev/null
@@ -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 <stdlib.h>
+#endif
+#ifdef HAVE_STDIO_H
+#include <stdio.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#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