Implement bt_getline
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 13 Oct 2015 18:56:44 +0000 (14:56 -0400)
committerMichael Jeanson <mjeanson@efficios.com>
Fri, 16 Oct 2015 19:40:33 +0000 (15:40 -0400)
Implement our own getline to remove dependency from glibc 2.10+ and to
increase portability to other operating systems.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
converter/babeltrace-log.c
include/Makefile.am
include/babeltrace/compat/stdio.h [new file with mode: 0644]
tests/lib/test_ctf_writer.c

index bdb01f16f4b6e7d18f2f642ba3c3c5224072177a..db7f60f1d2b60b2eeccf114ab24e2965a5641caa 100644 (file)
@@ -24,8 +24,6 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
- *
- * Depends on glibc 2.10 for getline().
  */
 
 #include <sys/types.h>
@@ -45,6 +43,7 @@
 #include <babeltrace/ctf/types.h>
 #include <babeltrace/compat/uuid.h>
 #include <babeltrace/compat/utc.h>
+#include <babeltrace/compat/stdio.h>
 #include <babeltrace/endian.h>
 
 #define NSEC_PER_USEC 1000UL
@@ -329,7 +328,7 @@ void trace_text(FILE *input, int output)
        write_packet_header(&pos, s_uuid);
        write_packet_context(&pos);
        for (;;) {
-               len = getline(&line, &linesize, input);
+               len = bt_getline(&line, &linesize, input);
                if (len < 0)
                        break;
                nl = strrchr(line, '\n');
index 9884ac2cbb5dd376b70b03841daabbdcfb6fecf9..6b81b234e3fe6dec5361f5027a4b01fa1f0191db 100644 (file)
@@ -57,5 +57,6 @@ noinst_HEADERS = \
        babeltrace/compat/fcntl.h \
        babeltrace/compat/stdlib.h \
        babeltrace/compat/dirent.h \
+       babeltrace/compat/stdio.h \
        babeltrace/endian.h \
        babeltrace/mmap-align.h
diff --git a/include/babeltrace/compat/stdio.h b/include/babeltrace/compat/stdio.h
new file mode 100644 (file)
index 0000000..0301e28
--- /dev/null
@@ -0,0 +1,124 @@
+#ifndef _BABELTRACE_COMPAT_STDIO_H
+#define _BABELTRACE_COMPAT_STDIO_H
+
+/*
+ * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <assert.h>
+
+#define BT_GETLINE_MINBUFLEN   64
+
+static inline
+char * _bt_getline_bufalloc(char **lineptr, size_t *n, size_t linelen)
+{
+       size_t buflen = *n;
+       char *buf = *lineptr;
+
+       if (buflen >= linelen && buf != NULL) {
+               return buf;
+       }
+       if (buf == NULL) {
+               buflen = BT_GETLINE_MINBUFLEN;
+       } else {
+               buflen = buflen << 1;
+               if (buflen < BT_GETLINE_MINBUFLEN) {
+                       buflen = BT_GETLINE_MINBUFLEN;
+               }
+       }
+       /* Check below not strictly needed, extra safety. */
+       if (buflen < linelen) {
+               buflen = linelen;
+       }
+       buf = realloc(buf, buflen);
+       if (!buf) {
+               errno = ENOMEM;
+               return NULL;
+       }
+       *n = buflen;
+       *lineptr = buf;
+       return buf;
+}
+
+/*
+ * Returns line length (including possible final \n, excluding final
+ * \0). On end of file, returns -1 with nonzero feof(stream) and errno
+ * set to 0. On error, returns -1 with errno set.
+ *
+ * This interface is similar to the getline(3) man page part of the
+ * Linux man-pages project, release 3.74. One major difference from the
+ * Open Group POSIX specification is that this implementation does not
+ * necessarily set the ferror() flag on error (because it is internal to
+ * libc).
+ */
+static inline
+ssize_t bt_getline(char **lineptr, size_t *n, FILE *stream)
+{
+       size_t linelen = 0;
+       char *buf;
+       int found_eof = 0;
+
+       if (lineptr == NULL || n == NULL) {
+               errno = EINVAL;
+               return -1;
+       }
+       for (;;) {
+               char c;
+
+               c = fgetc(stream);
+               if (c == EOF) {
+                       if (ferror(stream)) {
+                               /* ferror() is set, errno set by fgetc(). */
+                               return -1;
+                       }
+                       assert(feof(stream));
+                       found_eof = 1;
+                       break;
+               }
+               if (linelen == SSIZE_MAX) {
+                       errno = EOVERFLOW;
+                       return -1;
+               }
+               buf = _bt_getline_bufalloc(lineptr, n, ++linelen);
+               if (!buf) {
+                       return -1;
+               }
+               buf[linelen - 1] = c;
+               if (c == '\n') {
+                       break;
+               }
+       }
+       if (!linelen && found_eof) {
+               /* feof() is set. */
+               errno = 0;
+               return -1;
+       }
+       buf = _bt_getline_bufalloc(lineptr, n, ++linelen);
+       if (!buf) {
+               return -1;
+       }
+       buf[linelen - 1] = '\0';
+       return linelen - 1;     /* Count don't include final \0. */
+}
+
+#endif /* _BABELTRACE_COMPAT_STDIO_H */
index e0236a380e3aa563762354d144ea20096496e0dc..68bb2705ed42c13a7254e754eb981deb899dadbb 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdio.h>
 #include <sys/utsname.h>
 #include <babeltrace/compat/limits.h>
+#include <babeltrace/compat/stdio.h>
 #include <string.h>
 #include <assert.h>
 #include <unistd.h>
@@ -127,12 +128,12 @@ result:
                rewind(metadata_fp);
 
                /* Output the metadata and parser output as diagnostic */
-               while (getline(&line, &len, metadata_fp) > 0) {
+               while (bt_getline(&line, &len, metadata_fp) > 0) {
                        diag("%s", line);
                }
 
                rewind(parser_output_fp);
-               while (getline(&line, &len, parser_output_fp) > 0) {
+               while (bt_getline(&line, &len, parser_output_fp) > 0) {
                        diag("%s", line);
                }
 
@@ -224,7 +225,7 @@ result:
                        diag("malloc error");
                }
                rewind(babeltrace_output_fp);
-               while (getline(&line, &len, babeltrace_output_fp) > 0) {
+               while (bt_getline(&line, &len, babeltrace_output_fp) > 0) {
                        diag("%s", line);
                }
 
This page took 0.028389 seconds and 4 git commands to generate.