2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _BABELTRACE_COMPAT_STDIO_H
8 #define _BABELTRACE_COMPAT_STDIO_H
14 #include "common/assert.h"
16 #define BT_GETLINE_MINBUFLEN 64
19 char * _bt_getline_bufalloc(char **lineptr
, size_t *n
, size_t linelen
)
24 if (buflen
>= linelen
&& buf
) {
28 buflen
= BT_GETLINE_MINBUFLEN
;
31 if (buflen
< BT_GETLINE_MINBUFLEN
) {
32 buflen
= BT_GETLINE_MINBUFLEN
;
35 /* Check below not strictly needed, extra safety. */
36 if (buflen
< linelen
) {
39 buf
= realloc(buf
, buflen
);
50 * Returns line length (including possible final \n, excluding final
51 * \0). On end of file, returns -1 with nonzero feof(stream) and errno
52 * set to 0. On error, returns -1 with errno set.
54 * This interface is similar to the getline(3) man page part of the
55 * Linux man-pages project, release 3.74. One major difference from the
56 * Open Group POSIX specification is that this implementation does not
57 * necessarily set the ferror() flag on error (because it is internal to
61 ssize_t
bt_getline(char **lineptr
, size_t *n
, FILE *stream
)
78 /* ferror() is set, errno set by fgetc(). */
81 BT_ASSERT_DBG(feof(stream
));
86 if (linelen
== SSIZE_MAX
) {
90 buf
= _bt_getline_bufalloc(lineptr
, n
, ++linelen
);
99 if (!linelen
&& found_eof
) {
104 buf
= _bt_getline_bufalloc(lineptr
, n
, ++linelen
);
108 buf
[linelen
- 1] = '\0';
109 return linelen
- 1; /* Count don't include final \0. */
112 #endif /* _BABELTRACE_COMPAT_STDIO_H */
This page took 0.031009 seconds and 4 git commands to generate.