|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <errno.h> |
| 5 | +#include <limits.h> |
| 6 | + |
| 7 | +#include "file_read.h" |
| 8 | + |
| 9 | +char *parse_name(char *line) |
| 10 | +{ |
| 11 | + char *word; |
| 12 | + char *delims = " \t\n"; /* space, tab, newline */ |
| 13 | + word = strtok(line,delims); |
| 14 | + return word; |
| 15 | +} |
| 16 | + |
| 17 | +#define BUFSIZE 80 |
| 18 | +int getl(char **lineptr, FILE *fp) { |
| 19 | + char ch; |
| 20 | + ssize_t buf_pos = 0; |
| 21 | + ssize_t count = 2; /* Always buf_pos + 2 (see below). */ |
| 22 | + size_t new_length = 0; |
| 23 | + size_t n = BUFSIZE; |
| 24 | + char *temp; |
| 25 | + |
| 26 | + if ((lineptr == NULL) || (fp == NULL)) { |
| 27 | + errno = EINVAL; |
| 28 | + return -1; |
| 29 | + } |
| 30 | + |
| 31 | + if (errno != 0) |
| 32 | + errno = 0; |
| 33 | + |
| 34 | + if ((*lineptr == NULL)) { |
| 35 | + *lineptr = malloc(n * sizeof(char)); |
| 36 | + |
| 37 | + if (*lineptr == NULL) { |
| 38 | + return -1; /* Out of memory. */ |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /* |
| 43 | + * There are buf_pos characters in the buffer. When we read another |
| 44 | + * character, we want to store it, and we also need enough |
| 45 | + * room for a nul string. So we need to realloc as soon as our capacity |
| 46 | + * becomes less than buf_pos + 2. |
| 47 | + * Hence the variable "count" which always equals buf_pos + 2. |
| 48 | + */ |
| 49 | + |
| 50 | + while ((ch = getc(fp)) != EOF) { |
| 51 | + if (errno != 0) |
| 52 | + return -1; |
| 53 | + |
| 54 | + if (count > n) { /* current chars read is going to blow our buffer - add more */ |
| 55 | + new_length = n * 2; /* double the current buffer size */ |
| 56 | + if (new_length <= n) { /* Overflow. */ |
| 57 | + errno = ENOMEM; |
| 58 | + /* We couldn't store the character, */ |
| 59 | + /* so put it back on the stream. */ |
| 60 | + ungetc(ch, fp); |
| 61 | + return -1; |
| 62 | + } |
| 63 | + temp = (char *)realloc(*lineptr, new_length * sizeof(char)); /* realloc to a temp */ |
| 64 | + if (temp == NULL) { |
| 65 | + ungetc(ch, fp); |
| 66 | + return -1; |
| 67 | + } |
| 68 | + n = new_length; /* set n to the new length we were able to get from system */ |
| 69 | + *lineptr = temp; /* set line to this new temp string */ |
| 70 | + } |
| 71 | + |
| 72 | + (*lineptr)[buf_pos++] = ch; /* set this char in the string at buf_pos and THEN increment buf_pos */ |
| 73 | + |
| 74 | + if (ch == '\n') /* eol */ |
| 75 | + break; |
| 76 | + |
| 77 | + if (count == SSIZE_MAX) { /* SSIZE_MAX is 32767 - posix def */ |
| 78 | + /* We'll overflow ssize_t on the next round, since the return |
| 79 | + * type is SSIZE_T */ |
| 80 | + errno = ENOMEM; |
| 81 | + return -1; |
| 82 | + } |
| 83 | + count++; /* increment ch count */ |
| 84 | + } |
| 85 | + |
| 86 | + (*lineptr)[buf_pos] = '\0'; /* set last position to \0 */ |
| 87 | + |
| 88 | + if (buf_pos == 0) { /* nothing in the file? */ |
| 89 | + buf_pos = -1; |
| 90 | + } |
| 91 | + return buf_pos; |
| 92 | +} |
| 93 | + |
| 94 | +int getlx(char **iline,FILE *fp) |
| 95 | +{ |
| 96 | + char *line = *iline; |
| 97 | + char *newline = NULL; |
| 98 | + char *buf = NULL; |
| 99 | + char *eol = NULL; |
| 100 | + size_t capacity = sizeof(line); /* reasonable starting point for line length */ |
| 101 | + size_t remaining = capacity; |
| 102 | + size_t used = 0; |
| 103 | + |
| 104 | + buf = line; /* point buf -> line */ |
| 105 | + line[0] = '\0'; |
| 106 | + |
| 107 | + /* read file into buf */ |
| 108 | + while (fgets(buf, remaining, fp)) { |
| 109 | + eol = strchr(buf, '\n'); /* locate first occurrence of '\n' */ |
| 110 | + if (eol) { /* found a newline in the string */ |
| 111 | + *eol = '\0'; /* replace the newline with the null character */ |
| 112 | + break; |
| 113 | + } else { |
| 114 | + /* buffer was too small - enlarge it */ |
| 115 | + used = buf + remaining - line; |
| 116 | + |
| 117 | + newline = realloc(line, capacity * 2); |
| 118 | + if (!newline) { |
| 119 | + fprintf(stderr, "getl - realloc: %s\n", strerror(errno)); |
| 120 | + return -1; |
| 121 | + } else { |
| 122 | + line = newline; |
| 123 | + } |
| 124 | + |
| 125 | + buf = line + used - 1; |
| 126 | + capacity *= 2; |
| 127 | + remaining = capacity - used; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (errno) { |
| 132 | + fprintf(stderr, "getl - fgets: %s\n", strerror(errno)); |
| 133 | + } else if (line[0]) { |
| 134 | + char *eol = strchr(buf, '\n'); |
| 135 | + if (eol) |
| 136 | + *eol = '\0'; |
| 137 | + /*buf = line;*/ |
| 138 | + return strlen(line); |
| 139 | + } |
| 140 | + return -1; |
| 141 | +} |
0 commit comments