From: brarcher Date: Tue, 24 Jun 2014 04:45:07 +0000 (+0000) Subject: Provide getline() if unavailable X-Git-Tag: 0.10.0~46 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=595633fe152e2a4f49b972c9a204e7ae9d35e5d0;p=check Provide getline() if unavailable getline() is POSIX.1-2008, but is unavailable on several platforms, including MinGW and Solaris 10. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1172 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/configure.ac b/configure.ac index a575471..6bd6194 100644 --- a/configure.ac +++ b/configure.ac @@ -295,8 +295,8 @@ AC_FUNC_REALLOC HW_LIBRT_TIMERS # The following checks will replace missing functions from libcompat -AC_REPLACE_FUNCS([alarm clock_gettime gettimeofday localtime_r strdup strsignal]) -AC_CHECK_DECLS([alarm, clock_gettime, gettimeofday, localtime_r, strdup, strsignal]) +AC_REPLACE_FUNCS([alarm clock_gettime getline gettimeofday localtime_r strdup strsignal]) +AC_CHECK_DECLS([alarm, clock_gettime, getline gettimeofday, localtime_r, strdup, strsignal]) # The following checks are to only detect if the functions exist, but # not replace them diff --git a/lib/getline.c b/lib/getline.c new file mode 100644 index 0000000..d649c7d --- /dev/null +++ b/lib/getline.c @@ -0,0 +1,39 @@ +#include "libcompat.h" +#include + +#define INITIAL_SIZE 16 +#define DELIMITER '\n' + +ssize_t getline(char **lineptr, size_t *n, FILE *stream) +{ + ssize_t written = 0; + int character; + + if(*lineptr == NULL || *n < INITIAL_SIZE) + { + free(*lineptr); + *lineptr = (char *)malloc(INITIAL_SIZE); + *n = INITIAL_SIZE; + } + + while( (character = fgetc(stream)) != EOF) + { + written += 1; + if(written >= *n) + { + *n = *n * 2; + *lineptr = realloc(*lineptr, *n); + } + + (*lineptr)[written-1] = character; + + if(character == DELIMITER) + { + break; + } + } + + (*lineptr)[written] = '\0'; + + return written; +} diff --git a/lib/libcompat.h b/lib/libcompat.h index 6d02422..f2ab6cf 100644 --- a/lib/libcompat.h +++ b/lib/libcompat.h @@ -214,6 +214,10 @@ CK_DLL_EXP int rpl_snprintf(char *, size_t, const char *, ...); #endif #endif /* HAVE_STDARG_H */ +#if !HAVE_GETLINE +CK_DLL_EXP ssize_t getline(char **lineptr, size_t *n, FILE *stream); +#endif + /* silence warnings about an empty library */ CK_DLL_EXP void ck_do_nothing(void) CK_ATTRIBUTE_NORETURN;