getdelim.c

Go to the documentation of this file.
00001 /* getdelim.c --- Implementation of replacement getdelim function.
00002    Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005, 2006, 2007 Free
00003    Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU General Public License as
00007    published by the Free Software Foundation; either version 2, or (at
00008    your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful, but
00011    WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018    02110-1301, USA.  */
00019 
00020 /* Ported from glibc by Simon Josefsson. */
00021 
00022 #include <config.h>
00023 
00024 #ifdef FREEBSD
00025 
00026 #include <stdio.h>
00027 
00028 #include <sys/types.h>
00029 #include <limits.h>
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 
00033 #ifndef SIZE_MAX
00034 # define SIZE_MAX ((size_t) -1)
00035 #endif
00036 #ifndef SSIZE_MAX
00037 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
00038 #endif
00039 #if !HAVE_FLOCKFILE
00040 # undef flockfile
00041 # define flockfile(x) ((void) 0)
00042 #endif
00043 #if !HAVE_FUNLOCKFILE
00044 # undef funlockfile
00045 # define funlockfile(x) ((void) 0)
00046 #endif
00047 
00048 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
00049 #ifndef EOVERFLOW
00050 # define EOVERFLOW E2BIG
00051 #endif
00052 
00053 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
00054    NUL-terminate it).  *LINEPTR is a pointer returned from malloc (or
00055    NULL), pointing to *N characters of space.  It is realloc'ed as
00056    necessary.  Returns the number of characters read (not including
00057    the null terminator), or -1 on error or EOF.  */
00058 
00059 ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
00060 {
00061   ssize_t result;
00062   size_t cur_len = 0;
00063 
00064   if (lineptr == NULL || n == NULL || fp == NULL)
00065     {
00066       errno = EINVAL;
00067       return -1;
00068     }
00069 
00070   flockfile (fp);
00071 
00072   if (*lineptr == NULL || *n == 0)
00073     {
00074       *n = 120;
00075       *lineptr = (char *) realloc (*lineptr, *n);
00076       if (*lineptr == NULL)
00077         {
00078           result = -1;
00079           goto unlock_return;
00080         }
00081     }
00082 
00083   for (;;)
00084     {
00085       int i;
00086 
00087       i = getc (fp);
00088       if (i == EOF)
00089         {
00090           result = -1;
00091           break;
00092         }
00093 
00094       /* Make enough space for len+1 (for final NUL) bytes.  */
00095       if (cur_len + 1 >= *n)
00096         {
00097           size_t needed_max =
00098             SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
00099           size_t needed = 2 * *n + 1;   /* Be generous. */
00100           char *new_lineptr;
00101 
00102           if (needed_max < needed)
00103             needed = needed_max;
00104           if (cur_len + 1 >= needed)
00105             {
00106               result = -1;
00107               errno = EOVERFLOW;
00108               goto unlock_return;
00109             }
00110 
00111           new_lineptr = (char *) realloc (*lineptr, needed);
00112           if (new_lineptr == NULL)
00113             {
00114               result = -1;
00115               goto unlock_return;
00116             }
00117 
00118           *lineptr = new_lineptr;
00119           *n = needed;
00120         }
00121 
00122       (*lineptr)[cur_len] = i;
00123       cur_len++;
00124 
00125       if (i == delimiter)
00126         break;
00127     }
00128   (*lineptr)[cur_len] = '\0';
00129   result = cur_len ? (ssize_t)cur_len : result;
00130 
00131  unlock_return:
00132   funlockfile (fp); /* doesn't set errno */
00133 
00134   return result;
00135 }
00136 
00137 #endif /* #ifdef FREEBSD */

Generated on Sat Nov 22 04:00:37 2008 for NuFW by  doxygen 1.4.7