From 3dbe468fd6071adfba5f608da788502f1840af36 Mon Sep 17 00:00:00 2001 From: Dan Amelang Date: Fri, 1 Dec 2006 14:12:16 -0800 Subject: [perf] Provide watered-down implementations of getline and strndup for building on non-GNU toolchains. --- perf/cairo-perf-diff-files.c | 58 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/perf/cairo-perf-diff-files.c b/perf/cairo-perf-diff-files.c index 520ba3578..ee9a71c4b 100644 --- a/perf/cairo-perf-diff-files.c +++ b/perf/cairo-perf-diff-files.c @@ -27,9 +27,7 @@ #include "cairo-perf.h" -/* We use _GNU_SOURCE for getline. If someone wants to avoid that - * dependence they could conditionally provide a custom implementation - * of getline instead. */ +/* We use _GNU_SOURCE for getline and strndup. */ #ifndef _GNU_SOURCE # define _GNU_SOURCE #endif @@ -40,6 +38,60 @@ #include #include +/* We conditionally provide a custom implementation of getline and strndup + * as needed. These aren't necessary full-fledged general purpose + * implementations. They just get the job done for our purposes. + */ +#ifndef __USE_GNU + +#define POORMANS_GETLINE_BUFFER_SIZE (65536) +ssize_t +getline (char **lineptr, size_t *n, FILE *stream) +{ + if (!*lineptr) + { + *n = POORMANS_GETLINE_BUFFER_SIZE; + *lineptr = (char *) malloc (*n); + } + + if (!fgets (*lineptr, *n, stream)) + return -1; + + if (!feof (stream) && !strchr (*lineptr, '\n')) + { + fprintf (stderr, "The poor man's implementation of getline in " + __FILE__ " needs a bigger buffer. Perhaps it's " + "time for a complete implementation of getline.\n"); + exit (0); + } + + return strlen (*lineptr); +} +#undef POORMANS_GETLINE_BUFFER_SIZE + +char * +strndup (const char *s, size_t n) +{ + size_t len; + char *sdup; + + if (!s) + return NULL; + + len = strlen (s); + len = (n < len ? n : len); + sdup = (char *) malloc (len + 1); + if (sdup) + { + memcpy (sdup, s, len); + sdup[len] = '\0'; + } + + return sdup; +} + +#endif /* ifndef __USE_GNU */ + typedef struct _test_report { int id; char *backend; -- cgit v1.2.3