xref: /dflybsd-src/contrib/cvs-1.12/lib/getdelim.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* getdelim.c --- Implementation of replacement getdelim function.
286d7f5d3SJohn Marino    Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 Free
386d7f5d3SJohn Marino    Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or
686d7f5d3SJohn Marino    modify it under the terms of the GNU General Public License as
786d7f5d3SJohn Marino    published by the Free Software Foundation; either version 2, or (at
886d7f5d3SJohn Marino    your option) any later version.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful, but
1186d7f5d3SJohn Marino    WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1386d7f5d3SJohn Marino    General Public License for more details.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
1686d7f5d3SJohn Marino    along with this program; if not, write to the Free Software
1786d7f5d3SJohn Marino    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
1886d7f5d3SJohn Marino    02110-1301, USA.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino /* Ported from glibc by Simon Josefsson. */
2186d7f5d3SJohn Marino 
2286d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2386d7f5d3SJohn Marino # include <config.h>
2486d7f5d3SJohn Marino #endif
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino #include <stdlib.h>
2786d7f5d3SJohn Marino #include <errno.h>
2886d7f5d3SJohn Marino 
2986d7f5d3SJohn Marino #include "getdelim.h"
3086d7f5d3SJohn Marino 
3186d7f5d3SJohn Marino #if !HAVE_FLOCKFILE
3286d7f5d3SJohn Marino # undef flockfile
3386d7f5d3SJohn Marino # define flockfile(x) ((void) 0)
3486d7f5d3SJohn Marino #endif
3586d7f5d3SJohn Marino #if !HAVE_FUNLOCKFILE
3686d7f5d3SJohn Marino # undef funlockfile
3786d7f5d3SJohn Marino # define funlockfile(x) ((void) 0)
3886d7f5d3SJohn Marino #endif
3986d7f5d3SJohn Marino 
4086d7f5d3SJohn Marino /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
4186d7f5d3SJohn Marino    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
4286d7f5d3SJohn Marino    NULL), pointing to *N characters of space.  It is realloc'ed as
4386d7f5d3SJohn Marino    necessary.  Returns the number of characters read (not including
4486d7f5d3SJohn Marino    the null terminator), or -1 on error or EOF.  */
4586d7f5d3SJohn Marino 
4686d7f5d3SJohn Marino ssize_t
getdelim(char ** lineptr,size_t * n,int delimiter,FILE * fp)4786d7f5d3SJohn Marino getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
4886d7f5d3SJohn Marino {
4986d7f5d3SJohn Marino   int result = 0;
5086d7f5d3SJohn Marino   ssize_t cur_len = 0;
5186d7f5d3SJohn Marino   ssize_t len;
5286d7f5d3SJohn Marino 
5386d7f5d3SJohn Marino   if (lineptr == NULL || n == NULL || fp == NULL)
5486d7f5d3SJohn Marino     {
5586d7f5d3SJohn Marino       errno = EINVAL;
5686d7f5d3SJohn Marino       return -1;
5786d7f5d3SJohn Marino     }
5886d7f5d3SJohn Marino 
5986d7f5d3SJohn Marino   flockfile (fp);
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino   if (*lineptr == NULL || *n == 0)
6286d7f5d3SJohn Marino     {
6386d7f5d3SJohn Marino       *n = 120;
6486d7f5d3SJohn Marino       *lineptr = (char *) malloc (*n);
6586d7f5d3SJohn Marino       if (*lineptr == NULL)
6686d7f5d3SJohn Marino 	{
6786d7f5d3SJohn Marino 	  result = -1;
6886d7f5d3SJohn Marino 	  goto unlock_return;
6986d7f5d3SJohn Marino 	}
7086d7f5d3SJohn Marino     }
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino   for (;;)
7386d7f5d3SJohn Marino     {
7486d7f5d3SJohn Marino       char *t;
7586d7f5d3SJohn Marino       int i;
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino       i = getc (fp);
7886d7f5d3SJohn Marino       if (i == EOF)
7986d7f5d3SJohn Marino       {
8086d7f5d3SJohn Marino 	result = -1;
8186d7f5d3SJohn Marino 	break;
8286d7f5d3SJohn Marino       }
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino       /* Make enough space for len+1 (for final NUL) bytes.  */
8586d7f5d3SJohn Marino       if (cur_len + 1 >= *n)
8686d7f5d3SJohn Marino 	{
8786d7f5d3SJohn Marino 	  size_t needed = 2 * (cur_len + 1) + 1;   /* Be generous. */
8886d7f5d3SJohn Marino 	  char *new_lineptr;
8986d7f5d3SJohn Marino 
9086d7f5d3SJohn Marino 	  if (needed < cur_len)
9186d7f5d3SJohn Marino 	    {
9286d7f5d3SJohn Marino 	      result = -1;
9386d7f5d3SJohn Marino 	      goto unlock_return;
9486d7f5d3SJohn Marino 	    }
9586d7f5d3SJohn Marino 
9686d7f5d3SJohn Marino 	  new_lineptr = (char *) realloc (*lineptr, needed);
9786d7f5d3SJohn Marino 	  if (new_lineptr == NULL)
9886d7f5d3SJohn Marino 	    {
9986d7f5d3SJohn Marino 	      result = -1;
10086d7f5d3SJohn Marino 	      goto unlock_return;
10186d7f5d3SJohn Marino 	    }
10286d7f5d3SJohn Marino 
10386d7f5d3SJohn Marino 	  *lineptr = new_lineptr;
10486d7f5d3SJohn Marino 	  *n = needed;
10586d7f5d3SJohn Marino 	}
10686d7f5d3SJohn Marino 
10786d7f5d3SJohn Marino       (*lineptr)[cur_len] = i;
10886d7f5d3SJohn Marino       cur_len++;
10986d7f5d3SJohn Marino 
11086d7f5d3SJohn Marino       if (i == delimiter)
11186d7f5d3SJohn Marino 	break;
11286d7f5d3SJohn Marino     }
11386d7f5d3SJohn Marino   (*lineptr)[cur_len] = '\0';
11486d7f5d3SJohn Marino   result = cur_len ? cur_len : result;
11586d7f5d3SJohn Marino 
11686d7f5d3SJohn Marino  unlock_return:
11786d7f5d3SJohn Marino   funlockfile (fp);
11886d7f5d3SJohn Marino   return result;
11986d7f5d3SJohn Marino }
120