Added new download option to the manual.
[shell-fm.git] / source / getln.c
blob00bea1daa80fb1abc1ae4455094b1ad095376916
1 /*
2 Copyright (C) 2006 by Jonas Kramer
3 Published under the terms of the GNU General Public License (GPL).
4 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <assert.h>
11 #include "getln.h"
14 Reads a line until EOF or a line break occurs, whatever comes first.
15 The first parameter must be a pointer to another pointer which should either
16 be NULL or point to memory area reserved with *alloc. If it's a pointer to a
17 memory area, that memory will be used and resized as needed. In this case,
18 the second argument must point to an unsigned holding the size of the
19 allocated memory area. Otherwise, the memory will be freshly allocated and
20 it's size will be stored in the unsigned pointed to by the second argument.
21 The third argument is a FILE pointer which the line will be read from.
23 unsigned getln(char ** ptr, unsigned * size, FILE * fd) {
24 unsigned length = 0;
25 int ch = 0;
27 assert(size != NULL);
29 if(!(* ptr))
30 * size = 0;
32 while(!feof(fd) && ch != (char) 10) {
33 ch = fgetc(fd);
35 if(ch == -1)
36 ch = 0;
38 if(length + 2 > * size) {
39 * ptr = realloc(* ptr, (* size += 1024));
40 assert(* ptr);
43 (* ptr)[length++] = (char) ch;
44 (* ptr)[length] = 0;
47 * size = length + 1;
48 * ptr = realloc(* ptr, * size);
50 return length;