]> granicus.if.org Git - check/commitdiff
Provide getline() if unavailable
authorbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Tue, 24 Jun 2014 04:45:07 +0000 (04:45 +0000)
committerbrarcher <brarcher@64e312b2-a51f-0410-8e61-82d0ca0eb02a>
Tue, 24 Jun 2014 04:45:07 +0000 (04:45 +0000)
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

configure.ac
lib/getline.c [new file with mode: 0644]
lib/libcompat.h

index a575471c8890a60773e4095106c7c20b1dc87f36..6bd619433feb4130bdda6852fcbb803b12fc2c96 100644 (file)
@@ -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 (file)
index 0000000..d649c7d
--- /dev/null
@@ -0,0 +1,39 @@
+#include "libcompat.h"
+#include <stdio.h>
+
+#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;
+}
index 6d02422452abf475faac3fdb0c868b5b6811c9f9..f2ab6cfeceec36213299e0e75f40b232e71383fc 100644 (file)
@@ -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;