Port: Add missing includes for mingw
[babeltrace.git] / include / babeltrace / compat / stdio-internal.h
CommitLineData
2ca0671b
MD
1#ifndef _BABELTRACE_COMPAT_STDIO_H
2#define _BABELTRACE_COMPAT_STDIO_H
3
4/*
5 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include <stdio.h>
27#include <errno.h>
28#include <assert.h>
cbb430e2
MJ
29#include <malloc.h>
30#include <stdlib.h>
e9383dfd 31#include <limits.h>
2ca0671b
MD
32
33#define BT_GETLINE_MINBUFLEN 64
34
35static inline
36char * _bt_getline_bufalloc(char **lineptr, size_t *n, size_t linelen)
37{
38 size_t buflen = *n;
39 char *buf = *lineptr;
40
41 if (buflen >= linelen && buf != NULL) {
42 return buf;
43 }
44 if (buf == NULL) {
45 buflen = BT_GETLINE_MINBUFLEN;
46 } else {
47 buflen = buflen << 1;
48 if (buflen < BT_GETLINE_MINBUFLEN) {
49 buflen = BT_GETLINE_MINBUFLEN;
50 }
51 }
52 /* Check below not strictly needed, extra safety. */
53 if (buflen < linelen) {
54 buflen = linelen;
55 }
56 buf = realloc(buf, buflen);
57 if (!buf) {
58 errno = ENOMEM;
59 return NULL;
60 }
61 *n = buflen;
62 *lineptr = buf;
63 return buf;
64}
65
66/*
67 * Returns line length (including possible final \n, excluding final
68 * \0). On end of file, returns -1 with nonzero feof(stream) and errno
69 * set to 0. On error, returns -1 with errno set.
70 *
71 * This interface is similar to the getline(3) man page part of the
72 * Linux man-pages project, release 3.74. One major difference from the
73 * Open Group POSIX specification is that this implementation does not
74 * necessarily set the ferror() flag on error (because it is internal to
75 * libc).
76 */
77static inline
78ssize_t bt_getline(char **lineptr, size_t *n, FILE *stream)
79{
80 size_t linelen = 0;
81 char *buf;
82 int found_eof = 0;
83
84 if (lineptr == NULL || n == NULL) {
85 errno = EINVAL;
86 return -1;
87 }
88 for (;;) {
89 char c;
12a08de4 90 int ret;
2ca0671b 91
12a08de4
JG
92 ret = fgetc(stream);
93 if (ret == EOF) {
2ca0671b
MD
94 if (ferror(stream)) {
95 /* ferror() is set, errno set by fgetc(). */
96 return -1;
97 }
98 assert(feof(stream));
99 found_eof = 1;
100 break;
101 }
12a08de4 102 c = (char) ret;
2ca0671b
MD
103 if (linelen == SSIZE_MAX) {
104 errno = EOVERFLOW;
105 return -1;
106 }
107 buf = _bt_getline_bufalloc(lineptr, n, ++linelen);
108 if (!buf) {
109 return -1;
110 }
111 buf[linelen - 1] = c;
112 if (c == '\n') {
113 break;
114 }
115 }
116 if (!linelen && found_eof) {
117 /* feof() is set. */
118 errno = 0;
119 return -1;
120 }
121 buf = _bt_getline_bufalloc(lineptr, n, ++linelen);
122 if (!buf) {
123 return -1;
124 }
125 buf[linelen - 1] = '\0';
126 return linelen - 1; /* Count don't include final \0. */
127}
128
129#endif /* _BABELTRACE_COMPAT_STDIO_H */
This page took 0.036371 seconds and 4 git commands to generate.