getline.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:2k
源码类别:

浏览器

开发平台:

Unix_Linux

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