Back to index

im-sdk  12.3.91
Functions
util.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * safe_getline (FILE *fp)

Function Documentation

char* safe_getline ( FILE *  fp)

Definition at line 31 of file util.c.

{
  char *buf, *buf2, *ret;
  const size_t bufsize = SAFEBUF_SIZE;
  int n = 1;
  
  assert (fp != NULL);
  if (!fp){
    /* TO DO: log error */
    return NULL;
  }

  if (feof (fp) || ferror (fp)){
    /* TO DO: log error */
    return NULL;
  }

  buf = (char *) calloc (bufsize, sizeof (char));
  buf2 = buf;
  while ((ret = fgets (buf2, bufsize, fp)) != NULL &&
        buf2[strlen (buf2) - 1] != '\n'){
    buf = (char *) realloc (buf, bufsize * ++n);
    buf2 = buf + strlen (buf);
  }
  if (ret == NULL){
    /* TO DO: log error */
    return NULL;
  }
  return buf;
}

Here is the caller graph for this function: