2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
/* Copyright 2002, Red Hat Inc. - all rights reserved */
|
2002-06-22 02:29:23 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
2005-10-29 05:21:08 +08:00
|
|
|
<<getdelim>>---read a line up to a specified line delimiter
|
2002-06-22 02:29:23 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
getdelim
|
|
|
|
|
|
|
|
ANSI_SYNOPSIS
|
|
|
|
#include <stdio.h>
|
|
|
|
int getdelim(char **<[bufptr]>, size_t *<[n]>,
|
|
|
|
int <[delim]>, FILE *<[fp]>);
|
|
|
|
|
|
|
|
TRAD_SYNOPSIS
|
|
|
|
#include <stdio.h>
|
|
|
|
int getdelim(<[bufptr]>, <[n]>, <[delim]>, <[fp]>)
|
|
|
|
char **<[bufptr]>;
|
|
|
|
size_t *<[n]>;
|
|
|
|
int <[delim]>;
|
|
|
|
FILE *<[fp]>;
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
<<getdelim>> reads a file <[fp]> up to and possibly including a specified
|
2005-10-29 05:21:08 +08:00
|
|
|
delimiter <[delim]>. The line is read into a buffer pointed to
|
2002-06-22 02:29:23 +08:00
|
|
|
by <[bufptr]> and designated with size *<[n]>. If the buffer is
|
|
|
|
not large enough, it will be dynamically grown by <<getdelim>>.
|
|
|
|
As the buffer is grown, the pointer to the size <[n]> will be
|
|
|
|
updated.
|
|
|
|
|
|
|
|
RETURNS
|
2005-10-29 05:21:08 +08:00
|
|
|
<<getdelim>> returns <<-1>> if no characters were successfully read;
|
2002-06-22 02:29:23 +08:00
|
|
|
otherwise, it returns the number of bytes successfully read.
|
2005-10-29 05:21:08 +08:00
|
|
|
At end of file, the result is nonzero.
|
2002-06-22 02:29:23 +08:00
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<getdelim>> is a glibc extension.
|
|
|
|
|
|
|
|
No supporting OS subroutines are directly required.
|
|
|
|
*/
|
|
|
|
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
#include <_ansi.h>
|
2002-06-22 02:29:23 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "local.h"
|
|
|
|
|
|
|
|
#define MIN_LINE_SIZE 4
|
|
|
|
#define DEFAULT_LINE_SIZE 128
|
|
|
|
|
|
|
|
ssize_t
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
_DEFUN(__getdelim, (bufptr, n, delim, fp),
|
|
|
|
char **bufptr _AND
|
|
|
|
size_t *n _AND
|
|
|
|
int delim _AND
|
|
|
|
FILE *fp)
|
2002-06-22 02:29:23 +08:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
char *ptr;
|
|
|
|
size_t newsize, numbytes;
|
|
|
|
int pos;
|
|
|
|
int ch;
|
|
|
|
int cont;
|
|
|
|
|
|
|
|
if (fp == NULL || bufptr == NULL || n == NULL)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = *bufptr;
|
|
|
|
if (buf == NULL || *n < MIN_LINE_SIZE)
|
|
|
|
{
|
|
|
|
buf = (char *)realloc (*bufptr, DEFAULT_LINE_SIZE);
|
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*bufptr = buf;
|
|
|
|
*n = DEFAULT_LINE_SIZE;
|
|
|
|
}
|
|
|
|
|
2006-09-27 05:22:19 +08:00
|
|
|
CHECK_INIT (_REENT, fp);
|
2002-06-22 02:29:23 +08:00
|
|
|
|
2005-02-08 09:33:19 +08:00
|
|
|
_flockfile (fp);
|
2002-06-22 02:29:23 +08:00
|
|
|
|
|
|
|
numbytes = *n;
|
|
|
|
ptr = buf;
|
|
|
|
|
|
|
|
cont = 1;
|
|
|
|
|
|
|
|
while (cont)
|
|
|
|
{
|
|
|
|
/* fill buffer - leaving room for nul-terminator */
|
|
|
|
while (--numbytes > 0)
|
|
|
|
{
|
|
|
|
if ((ch = getc_unlocked (fp)) == EOF)
|
|
|
|
{
|
|
|
|
cont = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*ptr++ = ch;
|
|
|
|
if (ch == delim)
|
|
|
|
{
|
|
|
|
cont = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-23 09:56:05 +08:00
|
|
|
if (cont)
|
2002-06-22 02:29:23 +08:00
|
|
|
{
|
2002-08-23 09:56:05 +08:00
|
|
|
/* Buffer is too small so reallocate a larger buffer. */
|
|
|
|
pos = ptr - buf;
|
|
|
|
newsize = (*n << 1);
|
|
|
|
buf = realloc (buf, newsize);
|
|
|
|
if (buf == NULL)
|
|
|
|
{
|
|
|
|
cont = 0;
|
|
|
|
break;
|
|
|
|
}
|
2002-06-22 02:29:23 +08:00
|
|
|
|
2002-08-23 09:56:05 +08:00
|
|
|
/* After reallocating, continue in new buffer */
|
|
|
|
*bufptr = buf;
|
|
|
|
*n = newsize;
|
|
|
|
ptr = buf + pos;
|
|
|
|
numbytes = newsize - pos;
|
|
|
|
}
|
2002-06-22 02:29:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_funlockfile (fp);
|
|
|
|
|
|
|
|
/* if no input data, return failure */
|
|
|
|
if (ptr == buf)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* otherwise, nul-terminate and return number of bytes read */
|
|
|
|
*ptr = '\0';
|
|
|
|
return (ssize_t)(ptr - buf);
|
|
|
|
}
|
|
|
|
|