diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-07-10 20:18:28 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-07-10 20:18:28 -0400 |
commit | 537092cdbb7f4be0e6d68f4e5d65ca2a403375f9 (patch) | |
tree | 71b283d62b9babf05d38bc105f3f623e2c564b63 /src/lib/fs | |
parent | c08b7b10c520db08e20e6ac5630f0a668d5ecf3a (diff) | |
parent | c90961a9233e7287605886585503ee94d13a4592 (diff) | |
download | tor-537092cdbb7f4be0e6d68f4e5d65ca2a403375f9.tar.gz tor-537092cdbb7f4be0e6d68f4e5d65ca2a403375f9.zip |
Merge branch 'ticket26223'
Diffstat (limited to 'src/lib/fs')
-rw-r--r-- | src/lib/fs/.may_include | 3 | ||||
-rw-r--r-- | src/lib/fs/files.c | 4 | ||||
-rw-r--r-- | src/lib/fs/files.h | 39 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/lib/fs/.may_include b/src/lib/fs/.may_include index 6c9ce6ca04..b1e49fc891 100644 --- a/src/lib/fs/.may_include +++ b/src/lib/fs/.may_include @@ -1,4 +1,7 @@ orconfig.h + +ext/getdelim.c + lib/cc/*.h lib/container/*.h lib/encoding/*.h diff --git a/src/lib/fs/files.c b/src/lib/fs/files.c index c71e4d2a37..43dcbad333 100644 --- a/src/lib/fs/files.c +++ b/src/lib/fs/files.c @@ -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 diff --git a/src/lib/fs/files.h b/src/lib/fs/files.h index 5a12eb8215..2ee1b20149 100644 --- a/src/lib/fs/files.h +++ b/src/lib/fs/files.h @@ -103,4 +103,43 @@ 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) +/** Internal back-end function to implement getdelim(): only exists when + * Tor is built for unit tests, or when Tor is built on an operating system + * without its own getdelim(). */ +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. + * + * See `getdelim(3)` for more information. + * + * 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 |