diff options
author | Guillem Jover <guillem@hadrons.org> | 2009-05-15 21:23:03 +0200 |
---|---|---|
committer | Guillem Jover <guillem@hadrons.org> | 2009-05-15 21:23:03 +0200 |
commit | deb9f56ceb560f96448ff9bfa1befdf7ed9849de (patch) | |
tree | 1d19276539c75801de99280a4319621ab62fce0f | |
parent | aefc6f441aa1ae51135fea33d25572e39222cce7 (diff) | |
download | libbsd-deb9f56ceb560f96448ff9bfa1befdf7ed9849de.tar.gz |
Change fgetln to return the correct length value
Set len to 0 on error conditions to mimmic FreeBSD behaviour, and return
the amount of read characters on success, instead of the allocated size
by getline.
Reported-by: Jief L. <jief1.l@gmail.com>
-rw-r--r-- | src/fgetln.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/fgetln.c b/src/fgetln.c index e59028b..2e6eac9 100644 --- a/src/fgetln.c +++ b/src/fgetln.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2005 Hector Garcia Alvarez - * Copyright (C) 2005, 2008 Guillem Jover + * Copyright (C) 2005, 2008, 2009 Guillem Jover * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -35,16 +35,17 @@ char * fgetln (FILE *stream, size_t *len) { char *line = NULL; + size_t line_len = 0; ssize_t nread; - nread = getline (&line, len, stream); - if (nread == -1) + nread = getline(&line, &line_len, stream); + if (nread == -1) { + *len = 0; return NULL; - - /* Get rid of the trailing \0, fgetln does not have it. */ - (*len)--; - - return line; + } else { + *len = (size_t)nread; + return line; + } } #endif |