home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / getline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  1.8 KB  |  75 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* CHANGED FOR VMS */
  20.  
  21. /*
  22.  * <getline.c>
  23.  */
  24.  
  25. #include "HTUtils.h"
  26. #include "tcp.h"
  27. #include <stddef.h>
  28.  
  29. #include "LYLeaks.h"
  30.  
  31. /* Read up to (and including) a newline from STREAM into *LINEPTR
  32.    (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
  33.    NULL), pointing to *N characters of space.  It is realloc'd as
  34.    necessary.  Returns the number of characters read (not including the
  35.    null terminator), or -1 on error or EOF.  */
  36.  
  37. int getline(char **lineptr, size_t *n, FILE *stream)
  38. {
  39. static char line[256];
  40. char *ptr;
  41. unsigned int len;
  42.  
  43.    if (lineptr == NULL || n == NULL)
  44.    {
  45.       SOCKET_ERRNO = EINVAL;
  46.       return -1;
  47.    }
  48.  
  49.    if (ferror (stream))
  50.       return -1;
  51.  
  52.    if (feof(stream))
  53.       return -1;
  54.      
  55.    fgets(line,256,stream);
  56.  
  57.    ptr = strchr(line,'\n');   
  58.    if (ptr)
  59.       *ptr = '\0';
  60.  
  61.    len = strlen(line);
  62.    
  63.    if ((len+1) < 256)
  64.    {
  65.       ptr = realloc(*lineptr, 256);
  66.       if (ptr == NULL)
  67.          return(-1);
  68.       *lineptr = ptr;
  69.       *n = 256;
  70.    }
  71.  
  72.    strcpy(*lineptr,line); 
  73.    return(len);
  74. }
  75.