3 /* getline -- reimplementation of the GNU's getline() for other platforms */
5 /* Copyright (C) 2007 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 /** @brief Reads an entire line.
12 * Reads a line from the file specified by the file pointer passed
13 * as parameter. This function is intead to replace the non-portable
14 * GNU getline() function.
16 * @param buf address to a string buffer. If null, it will be allocated to receive the read data. If not and if too short, it will be reallocated. Must be freed by caller.
17 * @param n size of the buffer. Updated accordingly by the function.
18 * @param stream File pointer to the file to read from.
19 * @return The amount of chars read (or -1 on failure, ie on end of file condition)
22 #include "xbt/sysdep.h" /* headers of this function */
26 ssize_t getline(char **buf, size_t *n, FILE *stream) {
31 *buf = xbt_malloc(512);
38 for (i=0; (ch = fgetc(stream)) != EOF; i++) {
41 *buf = xbt_realloc(*buf, *n += 512);
45 if ((*buf)[i] == '\n') {
53 *buf = xbt_realloc(*buf, *n += 1);
60 #endif /* HAVE_GETLINE */