1 /* getline.c -- Replacement for GNU C library function getline 2 3 Copyright (C) 1993 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 2 of the 8 License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. */ 14 15 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */ 16 17 #ifdef HAVE_CONFIG_H 18 #include <config.h> 19 #endif 20 21 #include <sys/types.h> 22 #include <stdio.h> 23 #include <assert.h> 24 #include <errno.h> 25 #include "getline.h" 26 27 #if STDC_HEADERS 28 #include <stdlib.h> 29 #else 30 char *malloc (), *realloc (); 31 #endif 32 33 /* Always add at least this many bytes when extending the buffer. */ 34 #define MIN_CHUNK 64 35 36 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR 37 + OFFSET (and null-terminate it). If LIMIT is non-negative, then 38 read no more than LIMIT chars. 39 40 *LINEPTR is a pointer returned from malloc (or NULL), pointing to 41 *N characters of space. It is realloc'd as necessary. 42 43 Return the number of characters read (not including the null 44 terminator), or -1 on error or EOF. On a -1 return, the caller 45 should check feof(), if not then errno has been set to indicate the 46 error. */ 47 48 int 49 getstr (char **lineptr, size_t *n, FILE *stream, char terminator, int offset, 50 int limit) 51 { 52 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ 53 char *read_pos; /* Where we're reading into *LINEPTR. */ 54 int ret; 55 56 if (!lineptr || !n || !stream) 57 { 58 errno = EINVAL; 59 return -1; 60 } 61 62 if (!*lineptr) 63 { 64 *n = MIN_CHUNK; 65 *lineptr = malloc (*n); 66 if (!*lineptr) 67 { 68 errno = ENOMEM; 69 return -1; 70 } 71 } 72 73 nchars_avail = *n - offset; 74 read_pos = *lineptr + offset; 75 76 for (;;) 77 { 78 int save_errno; 79 register int c; 80 81 if (limit == 0) 82 break; 83 else 84 { 85 c = getc (stream); 86 87 /* If limit is negative, then we shouldn't pay attention to 88 it, so decrement only if positive. */ 89 if (limit > 0) 90 limit--; 91 } 92 93 save_errno = errno; 94 95 /* We always want at least one char left in the buffer, since we 96 always (unless we get an error while reading the first char) 97 NUL-terminate the line buffer. */ 98 99 assert((*lineptr + *n) == (read_pos + nchars_avail)); 100 if (nchars_avail < 2) 101 { 102 if (*n > MIN_CHUNK) 103 *n *= 2; 104 else 105 *n += MIN_CHUNK; 106 107 nchars_avail = *n + *lineptr - read_pos; 108 *lineptr = realloc (*lineptr, *n); 109 if (!*lineptr) 110 { 111 errno = ENOMEM; 112 return -1; 113 } 114 read_pos = *n - nchars_avail + *lineptr; 115 assert((*lineptr + *n) == (read_pos + nchars_avail)); 116 } 117 118 if (ferror (stream)) 119 { 120 /* Might like to return partial line, but there is no 121 place for us to store errno. And we don't want to just 122 lose errno. */ 123 errno = save_errno; 124 return -1; 125 } 126 127 if (c == EOF) 128 { 129 /* Return partial line, if any. */ 130 if (read_pos == *lineptr) 131 return -1; 132 else 133 break; 134 } 135 136 *read_pos++ = c; 137 nchars_avail--; 138 139 if (c == terminator) 140 /* Return the line. */ 141 break; 142 } 143 144 /* Done - NUL terminate and return the number of chars read. */ 145 *read_pos = '\0'; 146 147 ret = read_pos - (*lineptr + offset); 148 return ret; 149 } 150 151 int 152 get_line (char **lineptr, size_t *n, FILE *stream) 153 { 154 return getstr (lineptr, n, stream, '\n', 0, GETLINE_NO_LIMIT); 155 } 156 157 int 158 getline_safe (char **lineptr, size_t *n, FILE *stream, int limit) 159 { 160 return getstr (lineptr, n, stream, '\n', 0, limit); 161 } 162