]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
added getdelim.c because freebsd doesn't have it
authorMiek Gieben <miekg@NLnetLabs.nl>
Mon, 13 Mar 2006 09:47:29 +0000 (09:47 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Mon, 13 Mar 2006 09:47:29 +0000 (09:47 +0000)
pcat/Makefile.in
pcat/getdelim.c [new file with mode: 0644]
pcat/pcat-diff.c
pcat/pcat-print.c
pcat/pcat.c

index 1a220dc6e299e84f75f1284dcec7eb382fcadb11..e7df0009afe877a7ddc4ae879fdde6c3ebf414c7 100644 (file)
@@ -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 (file)
index 0000000..6035c2f
--- /dev/null
@@ -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 */
index e7e534aa8edae7385a59642f14fe3136a4fa3618..e966c14b79b515b8ded3fbeb571910295d668bd9 100644 (file)
@@ -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);
index 19f9c497252a71d0c0b25fbed1d04ce756c8296a..b2ef57330a85695336d1fd693e5728cc43dfc0d5 100644 (file)
@@ -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++;
index 65ffcd1c2eed68fc670d5029f2b2885b545e6011..a1b7cc514466cf1d816e142f9438e2862d1ff981 100644 (file)
@@ -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