| 1 | #ifdef HAVE_CONFIG_H | 
|---|
| 2 | #include "config.h" | 
|---|
| 3 | #endif | 
|---|
| 4 |  | 
|---|
| 5 | #undef _GNU_SOURCE | 
|---|
| 6 |  | 
|---|
| 7 | #include <sys/types.h> | 
|---|
| 8 | #define getline stdio_getline   /* bird */ | 
|---|
| 9 | #include <stdio.h> | 
|---|
| 10 | #undef getline                  /* bird */ | 
|---|
| 11 |  | 
|---|
| 12 | #ifdef HAVE_STRINGS_H | 
|---|
| 13 | # include <strings.h> | 
|---|
| 14 | #else | 
|---|
| 15 | # include <string.h> | 
|---|
| 16 | #endif /* HAVE_STRINGS_H */ | 
|---|
| 17 |  | 
|---|
| 18 | #ifdef HAVE_STDLIB_H | 
|---|
| 19 | # include <stdlib.h> | 
|---|
| 20 | #endif /* HAVE_STDLIB_H */ | 
|---|
| 21 |  | 
|---|
| 22 | #ifdef HAVE_UNISTD_H | 
|---|
| 23 | # include <unistd.h> | 
|---|
| 24 | #endif /* HAVE_UNISTD_H */ | 
|---|
| 25 |  | 
|---|
| 26 | #include <limits.h> | 
|---|
| 27 | #include <errno.h> | 
|---|
| 28 |  | 
|---|
| 29 | /* Read up to (and including) a '\n' from STREAM into *LINEPTR | 
|---|
| 30 | (and null-terminate it). *LINEPTR is a pointer returned from malloc (or | 
|---|
| 31 | NULL), pointing to *N characters of space.  It is realloc'd as | 
|---|
| 32 | necessary.  Returns the number of characters read (not including the | 
|---|
| 33 | null terminator), or -1 on error or EOF.  */ | 
|---|
| 34 |  | 
|---|
| 35 | size_t | 
|---|
| 36 | getline (lineptr, n, stream) | 
|---|
| 37 | char **lineptr; | 
|---|
| 38 | size_t *n; | 
|---|
| 39 | FILE *stream; | 
|---|
| 40 | { | 
|---|
| 41 | char *line, *p; | 
|---|
| 42 | long size, copy; | 
|---|
| 43 |  | 
|---|
| 44 | if (lineptr == NULL || n == NULL) | 
|---|
| 45 | { | 
|---|
| 46 | errno = EINVAL; | 
|---|
| 47 | return (size_t) -1; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | if (ferror (stream)) | 
|---|
| 51 | return (size_t) -1; | 
|---|
| 52 |  | 
|---|
| 53 | /* Make sure we have a line buffer to start with.  */ | 
|---|
| 54 | if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars.  */ | 
|---|
| 55 | { | 
|---|
| 56 | #ifndef MAX_CANON | 
|---|
| 57 | #define MAX_CANON       256 | 
|---|
| 58 | #endif | 
|---|
| 59 | if (!*lineptr) | 
|---|
| 60 | line = (char *) malloc (MAX_CANON); | 
|---|
| 61 | else | 
|---|
| 62 | line = (char *) realloc (*lineptr, MAX_CANON); | 
|---|
| 63 | if (line == NULL) | 
|---|
| 64 | return (size_t) -1; | 
|---|
| 65 | *lineptr = line; | 
|---|
| 66 | *n = MAX_CANON; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | line = *lineptr; | 
|---|
| 70 | size = *n; | 
|---|
| 71 |  | 
|---|
| 72 | copy = size; | 
|---|
| 73 | p = line; | 
|---|
| 74 |  | 
|---|
| 75 | while (1) | 
|---|
| 76 | { | 
|---|
| 77 | long len; | 
|---|
| 78 |  | 
|---|
| 79 | while (--copy > 0) | 
|---|
| 80 | { | 
|---|
| 81 | register int c = getc (stream); | 
|---|
| 82 | if (c == EOF) | 
|---|
| 83 | goto lose; | 
|---|
| 84 | else if ((*p++ = c) == '\n') | 
|---|
| 85 | goto win; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | /* Need to enlarge the line buffer.  */ | 
|---|
| 89 | len = p - line; | 
|---|
| 90 | size *= 2; | 
|---|
| 91 | line = (char *) realloc (line, size); | 
|---|
| 92 | if (line == NULL) | 
|---|
| 93 | goto lose; | 
|---|
| 94 | *lineptr = line; | 
|---|
| 95 | *n = size; | 
|---|
| 96 | p = line + len; | 
|---|
| 97 | copy = size - len; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | lose: | 
|---|
| 101 | if (p == *lineptr) | 
|---|
| 102 | return (size_t) -1; | 
|---|
| 103 |  | 
|---|
| 104 | /* Return a partial line since we got an error in the middle.  */ | 
|---|
| 105 | win: | 
|---|
| 106 | #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__) | 
|---|
| 107 | if (p - 2 >= *lineptr && p[-2] == '\r') | 
|---|
| 108 | p[-2] = p[-1], --p; | 
|---|
| 109 | #endif | 
|---|
| 110 | *p = '\0'; | 
|---|
| 111 | return p - *lineptr; | 
|---|
| 112 | } | 
|---|