]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
process_headers_fd: use buffered I/O
authorBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 10 Mar 2026 20:55:56 +0000 (21:55 +0100)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Tue, 10 Mar 2026 20:55:56 +0000 (21:55 +0100)
src/dumpfd2fd.c

index c252d2b5070283f15ec602a9207a77aa127d7860..b788ca8416a1d6d6307ee6ad0b465080adf807eb 100644 (file)
 #include <limits.h>
 #include <ctype.h>
 
+#include <stdio.h>
+#include <string.h>
+
 #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;
 }