/*
 * Copyright (C) 2004 Mads Martin Joergensen <mmj at mmj dot dk>
 * Copyright (C) 2023-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#include <limits.h>
#include <ctype.h>

#include "config.h"
#include "log_error.h"
#include "xmalloc.h"
#include "mygetline.h"

#define DUMPBUF 4096

int
copy_file(int from, int to, size_t bufsiz)
{
	char buf[bufsiz];
	ssize_t r, wresid, w = 0;
	char *bufp;
	r = read(from, buf, bufsiz);
	if (r < 0)
		return (r);
	for (bufp = buf, wresid = r; ; bufp += w, wresid -= w) {
		w = write(to, bufp, wresid);
		if (w <= 0)
			break;
		if (w >= (ssize_t) wresid)
			break;
	}
	return (w < 0 ? w : r);
}

int dumpfd2fd(int from, int to)
{
#ifdef HAVE_COPY_FILE_RANGE
	bool cfr = true;
#endif
	int r;

	do {
#ifdef HAVE_COPY_FILE_RANGE
		if (cfr) {
			r = copy_file_range(from, NULL, to, NULL, SSIZE_MAX, 0);
			if (r < 0 && (errno == EINVAL || errno == EXDEV)) {
				/* probably a non seekable FD */
				cfr = false;
			}
		}
		if (!cfr) {
#endif
			r = copy_file(from, to, DUMPBUF);
#ifdef HAVE_COPY_FILE_RANGE
		}
#endif
	} while (r > 0);

	return (r);
}

int process_headers_fd(int infd, int outfd,const char *from)
{
	char *line;
	int r;

	while((line = mygetline(infd))) {
		/* skip empty lines */
		while (isspace(*line))
			line++;
		if (line[0] == '\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 ;
		}
		free(line);
	}
	return 0 ;
}

