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