From: Baptiste Daroussin Date: Tue, 10 Mar 2026 20:55:56 +0000 (+0100) Subject: process_headers_fd: use buffered I/O X-Git-Tag: RELEASE_1_8_0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ef2abea0b694b367296faf78217cbb257eb47798;p=thirdparty%2Fmlmmj.git process_headers_fd: use buffered I/O --- diff --git a/src/dumpfd2fd.c b/src/dumpfd2fd.c index c252d2b5..b788ca84 100644 --- a/src/dumpfd2fd.c +++ b/src/dumpfd2fd.c @@ -29,10 +29,12 @@ #include #include +#include +#include + #include "config.h" #include "log_error.h" #include "xmalloc.h" -#include "mygetline.h" #define DUMPBUF 4096 @@ -82,32 +84,53 @@ int dumpfd2fd(int from, int to) return (r); } -int process_headers_fd(int infd, int outfd,const char *from) +int process_headers_fd(int infd, int outfd, const char *from) { - char *line; - int r; + char *line = NULL, *p, *to_be_subst, *new_line; + size_t linecap = 0; + int r, dupfd; + FILE *f; + + dupfd = dup(infd); + if (dupfd == -1) + return -1; + f = fdopen(dupfd, "r"); + if (f == NULL) { + close(dupfd); + return -1; + } - while((line = mygetline(infd))) { + while (getline(&line, &linecap, f) > 0) { /* skip empty lines */ - while (isspace(*line)) - line++; - if (line[0] == '\0') + p = line; + while (isspace(*p)) + p++; + if (*p == '\0') continue; - char *to_be_subst ; char *new_line ; - to_be_subst = strstr(line,"$posteraddr$") ; - if ( to_be_subst != NULL) { - *to_be_subst = '\0' ; - to_be_subst = to_be_subst + 12 ; - xasprintf(&new_line, "%s%s%s", line, from , to_be_subst ) ; - free(line); - line=new_line ; - } - if( (r=dprintf(outfd, "%s", line)) < 0) { - log_error(LOG_ARGS, "Could not write headers"); - return r ; + to_be_subst = strstr(p, "$posteraddr$"); + if (to_be_subst != NULL) { + *to_be_subst = '\0'; + to_be_subst += 12; + xasprintf(&new_line, "%s%s%s", p, from, to_be_subst); + if ((r = dprintf(outfd, "%s", new_line)) < 0) { + log_error(LOG_ARGS, "Could not write headers"); + free(new_line); + free(line); + fclose(f); + return r; + } + free(new_line); + } else { + if ((r = dprintf(outfd, "%s", p)) < 0) { + log_error(LOG_ARGS, "Could not write headers"); + free(line); + fclose(f); + return r; + } } - free(line); } - return 0 ; + free(line); + fclose(f); + return 0; }