getline.c 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * getline.c --- replacement for GNU getline if not available in libc
 *
 * Copyright  (C)  2011  Thomas Jahns <jahns@dkrz.de>
 *
 * Version: 0.0.1
 * Author: Thomas Jahns <jahns@dkrz.de>
 * Maintainer: Thomas Jahns <jahns@dkrz.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
33
#include <stdint.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <unistd.h>
#include <string.h>

/* read until end of line or end of file */
ssize_t
getline(char **linebuf, size_t *linebuf_size, FILE *fp)
{
  size_t len, old_len, buf_size;
  char *buf;
  ssize_t status = 0;

  if ( feof(fp) ) return -1;

  if (!linebuf_size || !linebuf)
  {
    errno = EINVAL;
    return -1;
  }
  if (!*linebuf)
  {
    buf_size = 80;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
55
    buf = malloc(buf_size);
56
57
58
59
60
61
62
    if (!buf) return -1;
  }
  else
  {
    buf_size = *linebuf_size;
    buf = *linebuf;
  }
63
64
  /* initially buf_size is guaranteed < INT_MAX */
  if ((status = (fgets(buf, (int)buf_size, fp) != NULL)))
65
66
67
68
69
  {
    len = strlen(buf);
    if (buf[len-1] != '\n')
    {
      do {
70
71
        size_t increment = 2 * buf_size - len <= INT_MAX
          ? 2 * buf_size - len : INT_MAX - (buf_size - len);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
72
        void *temp = realloc(buf, buf_size + increment);
73
74
75
76
77
78
        if (!temp)
        {
          status = -1;
          break;
        }
        buf = temp;
79
        buf_size += increment;
80
        old_len = len;
81
      } while(fgets(buf + len, (int)(buf_size - len), fp)
82
83
84
              && (len += strlen(buf + len)) > old_len
              && buf[len-1] != '\n');
    }
85
    status = len > SIZE_MAX ? SIZE_MAX : (ssize_t)len;
86
87
88
89
90
91
92
  }
  if (linebuf_size)
    *linebuf_size = buf_size;
  *linebuf = buf;
  return status;
}

93
94
95
96
97
98
99
100
101
/*
 * Local Variables:
 * c-file-style: "Java"
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * show-trailing-whitespace: t
 * require-trailing-newline: t
 * End:
 */