version 1.32, 2018/11/14 15:16:09 |
version 1.33, 2020/07/23 20:13:01 |
|
|
#include <unistd.h> |
#include <unistd.h> |
#include <wchar.h> |
#include <wchar.h> |
|
|
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) |
|
|
|
/* |
/* |
* There's a structure per input file which encapsulates the state of the |
* There's a structure per input file which encapsulates the state of the |
* file. We repeatedly read lines from each file until we've read in all |
* file. We repeatedly read lines from each file until we've read in all |
|
|
*/ |
*/ |
typedef struct { |
typedef struct { |
char *line; /* line */ |
char *line; /* line */ |
u_long linealloc; /* line allocated count */ |
size_t linealloc; /* bytes allocated for line */ |
char **fields; /* line field(s) */ |
char **fields; /* line field(s) */ |
u_long fieldcnt; /* line field(s) count */ |
u_long fieldcnt; /* line field(s) count */ |
u_long fieldalloc; /* line field(s) allocated count */ |
u_long fieldalloc; /* line field(s) allocated count */ |
|
|
{ |
{ |
LINE *lp, *lastlp, tmp; |
LINE *lp, *lastlp, tmp; |
ssize_t len; |
ssize_t len; |
size_t linesize; |
|
u_long cnt; |
u_long cnt; |
char *bp, *fieldp, *line; |
char *bp, *fieldp; |
|
|
/* |
/* |
* Read all of the lines from an input file that have the same |
* Read all of the lines from an input file that have the same |
|
|
*/ |
*/ |
|
|
F->setcnt = 0; |
F->setcnt = 0; |
line = NULL; |
|
linesize = 0; |
|
for (lastlp = NULL; ; ++F->setcnt) { |
for (lastlp = NULL; ; ++F->setcnt) { |
/* |
/* |
* If we're out of space to hold line structures, allocate |
* If we're out of space to hold line structures, allocate |
|
|
F->pushbool = 0; |
F->pushbool = 0; |
continue; |
continue; |
} |
} |
if ((len = getline(&line, &linesize, F->fp)) == -1) |
if ((len = getline(&(lp->line), &(lp->linealloc), F->fp)) == -1) |
break; |
break; |
|
|
/* Remove trailing newline, if it exists, and copy line. */ |
/* Remove the trailing newline, if any. */ |
if (line[len - 1] == '\n') |
if (lp->line[len - 1] == '\n') |
len--; |
lp->line[--len] = '\0'; |
if (lp->linealloc <= len + 1) { |
|
char *p; |
|
u_long newsize = lp->linealloc + |
|
MAXIMUM(100, len + 1 - lp->linealloc); |
|
if ((p = realloc(lp->line, newsize)) == NULL) |
|
err(1, NULL); |
|
lp->line = p; |
|
lp->linealloc = newsize; |
|
} |
|
memcpy(lp->line, line, len); |
|
lp->line[len] = '\0'; |
|
|
|
/* Split the line into fields, allocate space as necessary. */ |
/* Split the line into fields, allocate space as necessary. */ |
lp->fieldcnt = 0; |
lp->fieldcnt = 0; |
|
|
break; |
break; |
} |
} |
} |
} |
free(line); |
|
} |
} |
|
|
char * |
char * |