vfs: check userland buffers before reading them.
[haiku.git] / src / libs / bsd / fgetln.c
blob944d23955b5a6e7810fb285f07a88c65f40c2aa6
1 /*
2 * Copyright 2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
15 #define LINE_LENGTH 4096
18 char *
19 fgetln(FILE *stream, size_t *_length)
21 // TODO: this function is not thread-safe
22 static size_t sBufferSize;
23 static char *sBuffer;
25 size_t length, left;
26 char *line;
28 if (sBuffer == NULL) {
29 sBuffer = (char *)malloc(LINE_LENGTH);
30 if (sBuffer == NULL)
31 return NULL;
33 sBufferSize = LINE_LENGTH;
36 line = sBuffer;
37 left = sBufferSize;
38 if (_length != NULL)
39 *_length = 0;
41 for (;;) {
42 line = fgets(line, left, stream);
43 if (line == NULL) {
44 free(sBuffer);
45 sBuffer = NULL;
46 return NULL;
49 length = strlen(line);
50 if (_length != NULL)
51 *_length += length;
52 if (line[length - 1] != '\n' && length == sBufferSize - 1) {
53 // more data is following, enlarge buffer
54 char *newBuffer = realloc(sBuffer, sBufferSize + LINE_LENGTH);
55 if (newBuffer == NULL) {
56 free(sBuffer);
57 sBuffer = NULL;
58 return NULL;
61 sBuffer = newBuffer;
62 sBufferSize += LINE_LENGTH;
63 line = sBuffer + length;
64 left += 1;
65 } else
66 break;
69 return sBuffer;