Annotation of embedaddon/coova-chilli/src/util.c, revision 1.1

1.1     ! misho       1: /* This file is free software; you can redistribute it and/or modify */
        !             2: /* it under the terms of the GNU General Public License as published by */
        !             3: /* the Free Software Foundation; either version 2, or (at your option) */
        !             4: /* any later version. */
        !             5: 
        !             6: /* This file is distributed in the hope that it will be useful, */
        !             7: /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
        !             8: /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the */
        !             9: /* GNU General Public License for more details. */
        !            10: 
        !            11: /* You should have received a copy of the GNU General Public License */
        !            12: /* along with GNU Emacs; see the file COPYING.  If not, write to */
        !            13: /* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, */
        !            14: /* Boston, MA 02111-1307, USA. */
        !            15: 
        !            16: /* Copyright (C) 2004 Ian Zimmerman */
        !            17: 
        !            18: /* $Id: getline.c,v 1.3 2004/05/18 22:45:18 summerisle Exp $ */
        !            19: 
        !            20: #include "../config.h"
        !            21: #ifndef HAVE_GETLINE
        !            22: #include <stdio.h>
        !            23: #include <sys/types.h>
        !            24: #include <stdlib.h>
        !            25: 
        !            26: 
        !            27: #define GETLINE_BUFSIZE 4096
        !            28: 
        !            29: ssize_t
        !            30: getline (char** lineptr, size_t* n, FILE* stream)
        !            31: {
        !            32:   char* lptr1;
        !            33:   size_t nn;
        !            34:   int c;
        !            35: 
        !            36:   if (*lineptr == NULL && n == NULL)
        !            37:     {
        !            38:       lptr1 = malloc (GETLINE_BUFSIZE);
        !            39:       if (lptr1 == NULL) return EOF;
        !            40:       nn = GETLINE_BUFSIZE;
        !            41:     }
        !            42:   else
        !            43:     {
        !            44:       lptr1 = *lineptr;
        !            45:       nn = *n;
        !            46:     }
        !            47:   c = fgetc (stream);
        !            48:   if (c == EOF) return EOF;
        !            49:   {
        !            50:     size_t offset;
        !            51: 
        !            52:     offset = 0;
        !            53:     while (c != EOF)
        !            54:       {
        !            55:         if (offset >= nn - 1)
        !            56:           {
        !            57:             char* lptr2;
        !            58:             lptr2 = realloc (lptr1, 2 * nn);
        !            59:             if (lptr2 == NULL) return EOF;
        !            60:             lptr1 = lptr2;
        !            61:             nn *= 2;
        !            62:           }
        !            63:         lptr1[offset++] = (char)c;
        !            64:         if (c == '\n') break;
        !            65:         c = fgetc (stream);
        !            66:       }
        !            67:     lptr1[offset] = '\0';
        !            68:     *lineptr = lptr1;
        !            69:     *n = nn;
        !            70:     return offset;
        !            71:   }  
        !            72: }
        !            73: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>