home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / uucp.subproj / getlin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  1.9 KB  |  82 lines

  1. /* getlin.c
  2.    Replacement for getline.
  3.  
  4.    Copyright (C) 1992 Ian Lance Taylor
  5.  
  6.    This file is part of Taylor UUCP.
  7.  
  8.    This library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU Library General Public License
  10.    as published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    This library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    Library General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU Library General Public
  19.    License along with this library; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. /* Read a line from a file, returning the number of characters read.
  29.    This should really return ssize_t.  Returns -1 on error.  */
  30.  
  31. #define CGETLINE_DEFAULT (63)
  32.  
  33. int
  34. getline (pzline, pcline, e)
  35.      char **pzline;
  36.      size_t *pcline;
  37.      FILE *e;
  38. {
  39.   char *zput, *zend;
  40.   int bchar;
  41.  
  42.   if (*pzline == NULL)
  43.     {
  44.       *pzline = (char *) malloc (CGETLINE_DEFAULT);
  45.       if (*pzline == NULL)
  46.     return -1;
  47.       *pcline = CGETLINE_DEFAULT;
  48.     }
  49.  
  50.   zput = *pzline;
  51.   zend = *pzline + *pcline - 1;
  52.  
  53.   while ((bchar = getc (e)) != EOF)
  54.     {
  55.       if (zput >= zend)
  56.     {
  57.       size_t cnew;
  58.       char *znew;
  59.  
  60.       cnew = *pcline * 2 + 1;
  61.       znew = (char *) realloc ((pointer) *pzline, cnew);
  62.       if (znew == NULL)
  63.         return -1;
  64.       zput = znew + *pcline - 1;
  65.       zend = znew + cnew - 1;
  66.       *pzline = znew;
  67.       *pcline = cnew;
  68.     }
  69.  
  70.       *zput++ = bchar;
  71.  
  72.       if (bchar == '\n')
  73.     break;
  74.     }
  75.  
  76.   if (zput == *pzline)
  77.     return -1;
  78.  
  79.   *zput = '\0';
  80.   return zput - *pzline;
  81. }
  82.