From 16527d165de55f2727f55b752b30d44a7e61e927 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 06:23:18 +0200 Subject: Merging upstream version 2.1.148. Signed-off-by: Daniel Baumann --- compat/compat.c | 60 ++++++++++++++++++-------------------------- compat/compat.h.in | 18 ++++++++++--- compat/posix-shims/strings.h | 8 ++++++ 3 files changed, 46 insertions(+), 40 deletions(-) (limited to 'compat') diff --git a/compat/compat.c b/compat/compat.c index 5fb2be8..24f235c 100644 --- a/compat/compat.c +++ b/compat/compat.c @@ -86,9 +86,9 @@ vasprintf(char **strp, const char *fmt, va_list ap) ssize_t getline(char **lineptr, size_t *n, FILE *stream) { - static char line[256]; + static char chunk[256]; char *ptr; - ssize_t len; + ssize_t len, written; if (!lineptr || !n) { errno = EINVAL; @@ -99,28 +99,31 @@ getline(char **lineptr, size_t *n, FILE *stream) return -1; } - if (!fgets(line, 256, stream)) { - return -1; - } - - ptr = strchr(line, '\n'); - if (ptr) { - *ptr = '\0'; - } - - len = strlen(line); - - if (len + 1 < 256) { - ptr = realloc(*lineptr, 256); - if (!ptr) { - return -1; + *n = *lineptr ? *n : 0; + written = 0; + while (fgets(chunk, sizeof(chunk), stream)) { + len = strlen(chunk); + if (written + len > *n) { + ptr = realloc(*lineptr, *n + sizeof(chunk)); + if (!ptr) { + return -1; + } + *lineptr = ptr; + *n = *n + sizeof(chunk); } - *lineptr = ptr; - *n = 256; + memcpy(*lineptr + written, &chunk, len); + written += len; + if ((*lineptr)[written - 1] == '\n') { + break; + } + } + if (written) { + (*lineptr)[written] = '\0'; + } else { + written = -1; } - strcpy(*lineptr, line); - return len; + return written; } #endif @@ -322,21 +325,6 @@ gmtime_r(const time_t *timep, struct tm *result) #endif #endif -#ifndef HAVE_DIRNAME -#ifdef _WIN32 -#include -char * -dirname(char *path) -{ - PathRemoveFileSpecA(path); - return path; -} - -#else -#error No dirname() implementation for this platform is available. -#endif -#endif - #ifndef HAVE_SETENV #ifdef _WIN32 int diff --git a/compat/compat.h.in b/compat/compat.h.in index c697d6c..3baa489 100644 --- a/compat/compat.h.in +++ b/compat/compat.h.in @@ -3,7 +3,7 @@ * @author Michal Vasko * @brief compatibility functions header * - * Copyright (c) 2021 CESNET, z.s.p.o. + * Copyright (c) 2021 - 2023 CESNET, z.s.p.o. * * This source code is licensed under BSD 3-Clause License (the "License"). * You may not use this file except in compliance with the License. @@ -65,14 +65,12 @@ #cmakedefine HAVE_STRCHRNUL #cmakedefine HAVE_GET_CURRENT_DIR_NAME #cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK -#cmakedefine HAVE_TM_GMTOFF -#cmakedefine HAVE_TIME_H_TIMEZONE #cmakedefine HAVE_REALPATH #cmakedefine HAVE_LOCALTIME_R #cmakedefine HAVE_GMTIME_R +#cmakedefine HAVE_TIMEGM #cmakedefine HAVE_STRPTIME #cmakedefine HAVE_MMAP -#cmakedefine HAVE_DIRNAME #cmakedefine HAVE_STRCASECMP #cmakedefine HAVE_SETENV @@ -188,6 +186,18 @@ char *realpath(const char *path, char *resolved_path); struct tm *localtime_r(const time_t *timep, struct tm *result); #endif +#ifndef HAVE_GMTIME_R +struct tm *gmtime_r(const time_t *timep, struct tm *result); +#endif + +#ifndef HAVE_TIMEGM +# if defined (_WIN32) +# define timegm _mkgmtime +# else +# error No timegm() implementation for this platform is available. +# endif +#endif + #ifndef HAVE_STRPTIME char *strptime(const char *s, const char *format, struct tm *tm); #endif diff --git a/compat/posix-shims/strings.h b/compat/posix-shims/strings.h index c917a66..1ac0519 100644 --- a/compat/posix-shims/strings.h +++ b/compat/posix-shims/strings.h @@ -7,3 +7,11 @@ #error No strcasecmp() implementation for this platform is available. #endif #endif + +#ifndef HAVE_STRNCASECMP +#ifdef _MSC_VER +#define strncasecmp _strnicmp +#else +#error No strncasecmp() implementation for this platform is available. +#endif +#endif -- cgit v1.2.3