From: Baptiste Daroussin Date: Sat, 11 Feb 2023 06:38:36 +0000 (+0100) Subject: prepstdreply: use getline(3) X-Git-Tag: RELEASE_1_4_0b1~172 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=83d36de2a284ec62cf66ed7f128ee44fad66c65a;p=thirdparty%2Fmlmmj.git prepstdreply: use getline(3) --- diff --git a/src/prepstdreply.c b/src/prepstdreply.c index d72ad8a4..ba72ed7e 100644 --- a/src/prepstdreply.c +++ b/src/prepstdreply.c @@ -39,7 +39,6 @@ #include "strgen.h" #include "chomp.h" #include "log_error.h" -#include "mygetline.h" #include "wrappers.h" #include "getlistaddr.h" #include "mlmmj.h" @@ -70,10 +69,12 @@ struct source { int prefixlen; int prefixwidth; char *suffix; - int fd; + FILE *fp; struct formatted *fmt; int transparent; int limit; + char *line; + size_t linecap; }; @@ -386,7 +387,7 @@ text *open_text_fd(int fd) txt->src->limit = -1; txt->wrapmode = WRAP_WORD; txt->widthreckoning = WIDTH_THIN; - txt->src->fd = fd; + txt->src->fp = fdopen(fd, "r"); return (txt); } @@ -438,9 +439,10 @@ text *open_text(int listfd, const char *purpose, const char *action, void close_source(text *txt) { source *tmp; - if (txt->src->fd != -1) close(txt->src->fd); + if (txt->src->fp) fclose(txt->src->fp); if (txt->src->prefix != NULL) free(txt->src->prefix); if (txt->src->suffix != NULL) free(txt->src->suffix); + free(txt->src->line); tmp = txt->src; txt->src = txt->src->prev; free(tmp); @@ -547,36 +549,31 @@ static void begin_new_source_file(text *txt, char **line_p, char **pos_p, return; } - src = xmalloc(sizeof(source)); + src = xcalloc(1, sizeof(source)); src->prev = txt->src; - src->upcoming = NULL; src->prefixlen = strlen(line); src->prefixwidth = *width_p; src->prefix = xmalloc((*width_p + 1) * sizeof(char)); for (tmp = src->prefix, i = 0; i < *width_p; tmp++, i++) *tmp = ' '; *tmp = '\0'; - src->suffix = NULL; - src->fd = fd; - src->fmt = NULL; + src->fp = fdopen(fd, "r"); src->transparent = transparent; src->limit = -1; txt->src = src; - tmp = mygetline(fd); - if (tmp == NULL) { + if (getline(&txt->src->line, &txt->src->linecap, txt->src->fp) <= 0) { close_source(txt); **pos_p = '\0'; return; } + tmp = txt->src->line; if (!transparent) { esc = unistr_escaped_to_utf8(tmp); - free(tmp); tmp = esc; } - line = concatstr(2, line, tmp); + line = concatstr(2, line, txt->src->line); *pos_p = line + (*pos_p - *line_p); free(*line_p); *line_p = line; - free(tmp); } @@ -614,7 +611,7 @@ static void begin_new_formatted_source(text *txt, char **line_p, char **pos_p, } else { src->suffix = xstrdup(suffix); } - src->fd = -1; + src->fp = NULL; src->fmt = fmt; src->transparent = transparent; src->limit = -1; @@ -1086,15 +1083,16 @@ char *get_processed_text_line(text *txt, int headers, break; } if (txt->src->limit != 0) { - if (txt->src->fd != -1) { - tmp = mygetline(txt->src->fd); + tmp = NULL; + if (txt->src->fp != NULL) { + tmp = NULL; + if (getline(&txt->src->line, &txt->src->linecap, txt->src->fp) > 0) + tmp = xstrdup(txt->src->line); } else if (txt->src->fmt != NULL) { item = (*txt->src->fmt->get)( txt->src->fmt->state); if (item==NULL) tmp = NULL; else tmp = xstrdup(item); - } else { - tmp = NULL; } if (txt->src->limit > 0) txt->src->limit--; if (tmp == NULL) {