getline.c (32785) | getline.c (66525) |
---|---|
1/* getline.c -- Replacement for GNU C library function getline 2 3Copyright (C) 1993 Free Software Foundation, Inc. 4 5This program is free software; you can redistribute it and/or 6modify it under the terms of the GNU General Public License as 7published by the Free Software Foundation; either version 2 of the 8License, or (at your option) any later version. --- 8 unchanged lines hidden (view full) --- 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> | 1/* getline.c -- Replacement for GNU C library function getline 2 3Copyright (C) 1993 Free Software Foundation, Inc. 4 5This program is free software; you can redistribute it and/or 6modify it under the terms of the GNU General Public License as 7published by the Free Software Foundation; either version 2 of the 8License, or (at your option) any later version. --- 8 unchanged lines hidden (view full) --- 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" |
|
25 26#if STDC_HEADERS 27#include <stdlib.h> 28#else 29char *malloc (), *realloc (); 30#endif 31 32/* Always add at least this many bytes when extending the buffer. */ 33#define MIN_CHUNK 64 34 35/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR | 26 27#if STDC_HEADERS 28#include <stdlib.h> 29#else 30char *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 |
36 + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from 37 malloc (or NULL), pointing to *N characters of space. It is realloc'd 38 as necessary. Return the number of characters read (not including the 39 null terminator), or -1 on error or EOF. On a -1 return, the caller 40 should check feof(), if not then errno has been set to indicate 41 the error. */ | 37 + OFFSET (and null-terminate it). If LIMIT is non-negative, then 38 read no more than LIMIT chars. |
42 | 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 |
|
43int | 48int |
44getstr (lineptr, n, stream, terminator, offset) | 49getstr (lineptr, n, stream, terminator, offset, limit) |
45 char **lineptr; 46 size_t *n; 47 FILE *stream; 48 char terminator; 49 int offset; | 50 char **lineptr; 51 size_t *n; 52 FILE *stream; 53 char terminator; 54 int offset; |
55 int limit; |
|
50{ 51 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ 52 char *read_pos; /* Where we're reading into *LINEPTR. */ 53 int ret; 54 55 if (!lineptr || !n || !stream) 56 { 57 errno = EINVAL; --- 12 unchanged lines hidden (view full) --- 70 } 71 72 nchars_avail = *n - offset; 73 read_pos = *lineptr + offset; 74 75 for (;;) 76 { 77 int save_errno; | 56{ 57 int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ 58 char *read_pos; /* Where we're reading into *LINEPTR. */ 59 int ret; 60 61 if (!lineptr || !n || !stream) 62 { 63 errno = EINVAL; --- 12 unchanged lines hidden (view full) --- 76 } 77 78 nchars_avail = *n - offset; 79 read_pos = *lineptr + offset; 80 81 for (;;) 82 { 83 int save_errno; |
78 register int c = getc (stream); | 84 register int c; |
79 | 85 |
86 if (limit == 0) 87 break; 88 else 89 { 90 c = getc (stream); 91 92 /* If limit is negative, then we shouldn't pay attention to 93 it, so decrement only if positive. */ 94 if (limit > 0) 95 limit--; 96 } 97 |
|
80 save_errno = errno; 81 82 /* We always want at least one char left in the buffer, since we 83 always (unless we get an error while reading the first char) 84 NUL-terminate the line buffer. */ 85 86 assert((*lineptr + *n) == (read_pos + nchars_avail)); 87 if (nchars_avail < 2) --- 48 unchanged lines hidden (view full) --- 136} 137 138int 139getline (lineptr, n, stream) 140 char **lineptr; 141 size_t *n; 142 FILE *stream; 143{ | 98 save_errno = errno; 99 100 /* We always want at least one char left in the buffer, since we 101 always (unless we get an error while reading the first char) 102 NUL-terminate the line buffer. */ 103 104 assert((*lineptr + *n) == (read_pos + nchars_avail)); 105 if (nchars_avail < 2) --- 48 unchanged lines hidden (view full) --- 154} 155 156int 157getline (lineptr, n, stream) 158 char **lineptr; 159 size_t *n; 160 FILE *stream; 161{ |
144 return getstr (lineptr, n, stream, '\n', 0); | 162 return getstr (lineptr, n, stream, '\n', 0, GETLINE_NO_LIMIT); |
145} | 163} |
164 165int 166getline_safe (lineptr, n, stream, limit) 167 char **lineptr; 168 size_t *n; 169 FILE *stream; 170 int limit; 171{ 172 return getstr (lineptr, n, stream, '\n', 0, limit); 173} |
|