[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: libbsd



jief1.l dixit:

>but getline store the allocated size in len, not the line length as it
>should.
>
>I correct with :

RETURN VALUE
       On success, getline() and getdelim() return the  number  of  characters
       read,  including  the delimiter character, but not including the termi-
       nating null byte.  This value can be used to handle embedded null bytes
       in the line read.

Your correction is correct, however, I believe I do have a *much*
shorter, simpler and faster solution handy:

http://cvs.mirbsd.de/contrib/code/mirmake/dist/contrib/fgetln.c

The file is also attached to this eMail for convenience.
I originally wrote it for FreeWRT Embedded GNU/Linux because
all existing variants, even the GPL’d ones, were buggy.

bye,
//mirabilos
-- 
“It is inappropriate to require that a time represented as
 seconds since the Epoch precisely represent the number of
 seconds between the referenced time and the Epoch.”
	-- IEEE Std 1003.1b-1993 (POSIX) Section B.2.2.2
/* $MirOS: contrib/code/mirmake/dist/contrib/fgetln.c,v 1.3 2008/04/06 22:35:24 tg Exp $ */

/*-
 * Copyright (c) 2007
 *	Thorsten Glaser <tg@mirbsd.de>
 *
 * Provided that these terms and disclaimer and all copyright notices
 * are retained or reproduced in an accompanying document, permission
 * is granted to deal in this work without restriction, including un-
 * limited rights to use, publicly perform, distribute, sell, modify,
 * merge, give away, or sublicence.
 *
 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
 * the utmost extent permitted by applicable law, neither express nor
 * implied; without malicious intent or gross negligence. In no event
 * may a licensor, author or contributor be held liable for indirect,
 * direct, other damage, loss, or other issues arising in any way out
 * of dealing in the work, even if advised of the possibility of such
 * damage or existence of a defect, except proven that it results out
 * of said person's immediate fault when using the work as intended.
 *-
 * fgetln() wrapper for operating systems with getline()
 */

#define _GNU_SOURCE		/* for getline() */
#include <sys/types.h>
#include <stdio.h>
#include <string.h>

__RCSID("$MirOS: contrib/code/mirmake/dist/contrib/fgetln.c,v 1.3 2008/04/06 22:35:24 tg Exp $");

#if !defined(_MIRMAKE_H) || !defined(_MIRMAKE_DEFNS)
char *fgetln(FILE *, size_t *);
#endif

char *
fgetln(FILE *stream, size_t *len)
{
	char *lb = NULL;
	size_t lbsz = 0;

	*len = getline(&lb, &lbsz, stream);
	return ((*len == (size_t)-1) ? NULL : lb);
}

Reply to: