1 /* $MirOS: src/lib/libc/stdio/getline.c,v 1.1 2006/10/02 02:56:58 tg Exp $ */
2
3 /*-
4 * Copyright (c) 2006
5 * Thorsten Glaser <tg@mirbsd.de>
6 *
7 * Licensee is hereby permitted to deal in this work without restric-
8 * tion, including unlimited rights to use, publicly perform, modify,
9 * merge, distribute, sell, give away or sublicence, provided all co-
10 * pyright notices above, these terms and the disclaimer are retained
11 * in all redistributions or reproduced in accompanying documentation
12 * or other materials provided with binary redistributions.
13 *
14 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
15 * express, or implied, to the maximum extent permitted by applicable
16 * law, without malicious intent or gross negligence; in no event may
17 * licensor, an author or contributor be held liable for any indirect
18 * or other damage, or direct damage except proven a consequence of a
19 * direct error of said person and intended use of this work, loss or
20 * other issues arising in any way out of its use, even if advised of
21 * the possibility of such damage or existence of a defect.
22 */
23
24 #define _GNU_SOURCE
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 __RCSID("$MirOS: src/lib/libc/stdio/getline.c,v 1.1 2006/10/02 02:56:58 tg Exp $");
31
32 ssize_t
getline(char ** dst,size_t * siz,FILE * f)33 getline(char **dst, size_t *siz, FILE *f)
34 {
35 size_t n;
36 char *buf;
37
38 if ((dst == NULL) || (siz == NULL) || (f == NULL)) {
39 errno = EINVAL;
40 return (-1);
41 }
42
43 if ((buf = fgetln(f, &n)) == NULL)
44 return (-1); /* includes feof(f) */
45
46 if ((*dst == NULL) || (*siz < n + 1)) {
47 char *newdst;
48
49 if ((newdst = realloc(*dst, n + 1)) == NULL)
50 return (-1);
51 *dst = newdst;
52 *siz = n + 1;
53 }
54
55 memcpy(*dst, buf, n);
56 (*dst)[n] = '\0';
57
58 return (n);
59 }
60