From: Miek Gieben Date: Mon, 13 Mar 2006 09:47:29 +0000 (+0000) Subject: added getdelim.c because freebsd doesn't have it X-Git-Tag: release-1.1.0~300 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a047e34b46bc3ddb56cccc606b589f8f8e53965;p=thirdparty%2Fldns.git added getdelim.c because freebsd doesn't have it --- diff --git a/pcat/Makefile.in b/pcat/Makefile.in index 1a220dc6..e7df0009 100644 --- a/pcat/Makefile.in +++ b/pcat/Makefile.in @@ -28,17 +28,18 @@ all: p p: pcat pcat-diff pcat-print -pcat: pcat.o +pcat: pcat.o getdelim.o $(LINK) -o $@ $+ -pcat-diff: pcat-diff.o +pcat-diff: pcat-diff.o getdelim.o $(LINK) -o $@ $+ -pcat-print: pcat-print.o +pcat-print: pcat-print.o getdelim.o $(LINK) -o $@ $+ clean: rm -f pcat pcat-diff pcat-print + rm -f pcat.o pcat-diff.o pcat-print.o realclean: clean rm -f configure config.h config.log config.status diff --git a/pcat/getdelim.c b/pcat/getdelim.c new file mode 100644 index 00000000..6035c2f7 --- /dev/null +++ b/pcat/getdelim.c @@ -0,0 +1,64 @@ +#ifndef HAVE_GETDELIM + +#include "config.h" + +#define GETDELIM_BUFFER 128 + +/* copied from xine-devel */ +ssize_t +getdelim( char **lineptr, size_t *n, int delimiter, FILE *stream ) +{ + char *p; + int c; + size_t len = 0; + + if (!lineptr || !n || (!*lineptr && *n)) + return -1; + + /* allocate initial buffer */ + if (!*lineptr || !*n) { + char *np; + np = realloc( *lineptr, GETDELIM_BUFFER ); + if (!np) + return -1; + *n = GETDELIM_BUFFER; + *lineptr = np; + } + + p = *lineptr; + + /* read characters from stream */ + while ((c = fgetc( stream )) != EOF) { + if (len >= *n) { + char *np = realloc( *lineptr, *n * 2 ); + if (!np) + return -1; + p = np + (p - *lineptr); + *lineptr = np; + *n *= 2; + } + *p++ = (char) c; + len++; + if (delimiter == c) + break; + } + + /* end of file without any bytes read */ + if ((c == EOF) && (len == 0)) + return -1; + + /* trailing "\0" */ + if (len >= *n) { + char *np = realloc( *lineptr, *n + 1 ); + if (!np) + return -1; + p = np + (p - *lineptr); + *lineptr = np; + *n += 1; + } + *p = '\0'; + + return len; +} + +#endif /* !HAVE_GETDELIM */ diff --git a/pcat/pcat-diff.c b/pcat/pcat-diff.c index e7e534aa..e966c14b 100644 --- a/pcat/pcat-diff.c +++ b/pcat/pcat-diff.c @@ -119,8 +119,8 @@ main(int argc, char **argv) i = 1; reread: - read1 = getline(&line1, &len1, trace1); - read2 = getline(&line2, &len2, trace2); + read1 = getdelim(&line1, &len1, '\n', trace1); + read2 = getdelim(&line2, &len2, '\n', trace2); if (read1 == -1 || read2 == -1) { fclose(trace1); fclose(trace2); exit(EXIT_SUCCESS); diff --git a/pcat/pcat-print.c b/pcat/pcat-print.c index 19f9c497..b2ef5733 100644 --- a/pcat/pcat-print.c +++ b/pcat/pcat-print.c @@ -9,6 +9,10 @@ #define ANSWER1 3 #define ANSWER2 0 +#ifndef HAVE_GETDELIM +ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); +#endif + void usage(FILE *fp) { @@ -70,7 +74,7 @@ main(int argc, char **argv) } } - while((read = getline(&line, &len, diff)) != -1) { + while((read = getdelim(&line, &len, '\n', diff)) != -1) { if (read < 2 || read > LDNS_MAX_PACKETLEN) { fprintf(stderr, "Under- or overflow - skipping line %zd\n", i); i++; diff --git a/pcat/pcat.c b/pcat/pcat.c index 65ffcd1c..a1b7cc51 100644 --- a/pcat/pcat.c +++ b/pcat/pcat.c @@ -8,6 +8,10 @@ #endif #define DNS_UDP_OFFSET 42 +#ifndef HAVE_GETDELIM +ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); +#endif + /* output: see usage() */ void