]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Integrate getdelim() and getline() support into Tor.
authorNick Mathewson <nickm@torproject.org>
Tue, 10 Jul 2018 14:23:29 +0000 (10:23 -0400)
committerNick Mathewson <nickm@torproject.org>
Tue, 10 Jul 2018 14:36:49 +0000 (10:36 -0400)
configure.ac
src/ext/getdelim.c
src/lib/fs/.may_include
src/lib/fs/files.c
src/lib/fs/files.h

index 296591f02568d57f676bcb5b2421239ba040cc3c..ff03cf10e91a3c53c12e42018125dc7a6e880174 100644 (file)
@@ -585,7 +585,9 @@ AC_CHECK_FUNCS(
         ftime \
         get_current_dir_name \
         getaddrinfo \
+       getdelim \
         getifaddrs \
+       getline \
         getpass \
         getrlimit \
         gettimeofday \
index 60df7e1b647f4144fd2563f196aac5f3c58924dc..8254103ff90141f94b7f219b6c7db50fbdf6fce8 100644 (file)
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <nbcompat.h>
-#include <nbcompat/stdio.h>
-#include <nbcompat/stdlib.h>
-
-#if !HAVE_GETDELIM
+#ifndef BUFSIZ
+#define BUFSIZ 512
+#endif
 
 ssize_t
-getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
+compat_getdelim_(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
 {
        char *ptr, *eptr;
 
 
        if (*buf == NULL || *bufsiz == 0) {
                *bufsiz = BUFSIZ;
-               if ((*buf = malloc(*bufsiz)) == NULL)
+               if ((*buf = raw_malloc(*bufsiz)) == NULL)
                        return -1;
        }
 
@@ -69,7 +67,7 @@ getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
                        char *nbuf;
                        size_t nbufsiz = *bufsiz * 2;
                        ssize_t d = ptr - *buf;
-                       if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
+                       if ((nbuf = raw_realloc(*buf, nbufsiz)) == NULL)
                                return -1;
                        *buf = nbuf;
                        *bufsiz = nbufsiz;
@@ -78,5 +76,3 @@ getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
                }
        }
 }
-
-#endif
index 6c9ce6ca04aed3b1b251a75e602651e08b0019c3..b1e49fc89132d4ac5c0d36e496e354cca64c67b5 100644 (file)
@@ -1,4 +1,7 @@
 orconfig.h
+
+ext/getdelim.c
+
 lib/cc/*.h
 lib/container/*.h
 lib/encoding/*.h
index 4e0a398baa993a1dd0f8025847d0303fdd14e488..e93d36d86d08b82037bc8862b11b32250d3ff599 100644 (file)
@@ -715,3 +715,7 @@ read_file_to_str, (const char *filename, int flags, struct stat *stat_out))
 
   return string;
 }
+
+#if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
+#include "ext/getdelim.c"
+#endif
index 5a12eb821582288b9c86e748dc4d153b7dc34128..d219e3cf05da91bf2be5f0c5ead42655c2a267ff 100644 (file)
@@ -103,4 +103,38 @@ char *read_file_to_str_until_eof(int fd, size_t max_bytes_to_read,
                                  size_t *sz_out)
   ATTR_MALLOC;
 
+#if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
+ssize_t compat_getdelim_(char **lineptr, size_t *n, int delim, FILE *stream);
+#endif
+
+#ifdef HAVE_GETDELIM
+/**
+ * Cross-platform wrapper for getdelim(): behaves as the POSIX-standard
+ * getdelim() function.
+ *
+ * Note that this function will use the libc memory allocator -- so any memory
+ * passed to this function must come from raw_malloc(), and must be freed by
+ * raw_free() -- don't use tor_malloc() and tor_free() with this.
+ */
+#define tor_getdelim(lineptr, n, delim, stream) \
+  getdelim((lineptr), (n), (delim), (stream))
+#else
+#define tor_getdelim(lineptr, n, delim, stream) \
+  compat_getdelim_((lineptr), (n), (delim), (stream))
+#endif
+
+#ifdef HAVE_GETLINE
+/**
+ * Cross-platform wrapper for getline(): behaves as the POSIX-standard
+ * getline() function.
+ *
+ * See tor_getdelim() for usage notes.
+ */
+#define tor_getline(lineptr, n, stream) \
+  getline((lineptr), (n), (stream))
+#else
+#define tor_getline(lineptr, n, stream) \
+  tor_getdelim((lineptr), (n), '\n', (stream))
+#endif
+
 #endif