view src/ggets.c @ 44:f0073a47c31d

Fix various warnings.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Aug 2013 00:04:37 +0300
parents 785057719d9b
children
line wrap: on
line source

/* File ggets.c  - goodgets is a safe alternative to gets */
/* By C.B. Falconer.  Public domain 2002-06-22            */
/*    attribution appreciated.                            */

/* Revised 2002-06-26  New prototype.
           2002-06-27  Incomplete final lines
           2006-06-14  Simplified, using getc, not fgets
           2006-06-15  Fixed memory leak at EOF
 */

/* fggets and ggets [which is fggets(ln, stdin)] set *ln to
   a buffer filled with the next complete line from the text
   stream f.  The storage has been allocated within fggets,
   and is normally reduced to be an exact fit.  The trailing
   \n has been removed, so the resultant line is ready for
   dumping with puts.  The buffer will be as large as is
   required to hold the complete line.

   Note: this means a final file line without a \n terminator
   has an effective \n appended, as EOF occurs within the read.

   If no error occurs fggets returns 0.  If an EOF occurs on
   the input file, EOF is returned.  For memory allocation
   errors some positive value is returned.  In this case *ln
   may point to a partial line.  For other errors memory is
   freed and *ln is set to NULL.

   Freeing of assigned storage is the callers responsibility
 */

#include <stdio.h>
#include <stdlib.h>
#include "ggets.h"

#define INITSIZE   112  /* power of 2 minus 16, helps malloc */
#define DELTASIZE (INITSIZE + 16)

enum {OK = 0, NOMEM};

int fggets(char* *ln, FILE *f)
{
   int     cursize, ch, ix;
   char   *buffer, *temp;

   *ln = NULL; /* default */
   if ((buffer = (char *) malloc(INITSIZE)) == NULL) return NOMEM;
   cursize = INITSIZE;

   ix = 0;
   while ((ch = getc(f)) != EOF && ch != '\n') {
      if (ix >= (cursize - 1)) { /* extend buffer */
         cursize += DELTASIZE;
         if ((temp = (char *) realloc(buffer, (size_t)cursize)) == NULL) {
            /* ran out of memory, return partial line */
            buffer[ix] = '\0';
            *ln = buffer;
            return NOMEM;
         }
         buffer = temp;
      }
      buffer[ix++] = ch;
   }

   if (ch == EOF && ix == 0) {
      free(buffer);
      return EOF;
   }

   buffer[ix] = '\0';
   if ((temp = (char *) realloc(buffer, (size_t)ix + 1)) == NULL) {
      *ln = buffer;  /* without reducing it */
   }
   else *ln = temp;
   return OK;
} /* fggets */
/* End of ggets.c */