aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Uhlin <markus@nifty-networks.net>2025-10-17 02:14:44 +0200
committerMarkus Uhlin <markus@nifty-networks.net>2025-10-17 02:14:44 +0200
commit1e559f41e4175899669f1b624fd950db04f40c4f (patch)
tree7fdcf6305833b43a96bf0444076952c1ce59e5cf
parent97b3ac4e198481ee10a1e43ea6d5a99a6da4a41a (diff)
Added fics_copyfile() -- initial revision
-rw-r--r--FICS/build.mk2
-rw-r--r--FICS/copyfile.c70
-rw-r--r--FICS/copyfile.h13
3 files changed, 85 insertions, 0 deletions
diff --git a/FICS/build.mk b/FICS/build.mk
index 5d3d9b2..5c2e4f7 100644
--- a/FICS/build.mk
+++ b/FICS/build.mk
@@ -9,6 +9,7 @@ OBJS = $(SRC_DIR)adminproc.o\
$(SRC_DIR)board.o\
$(SRC_DIR)command.o\
$(SRC_DIR)comproc.o\
+ $(SRC_DIR)copyfile.o\
$(SRC_DIR)eco.o\
$(SRC_DIR)fics_getsalt.o\
$(SRC_DIR)ficslim.o\
@@ -43,6 +44,7 @@ SRCS = $(SRC_DIR)adminproc.c\
$(SRC_DIR)board.c\
$(SRC_DIR)command.c\
$(SRC_DIR)comproc.c\
+ $(SRC_DIR)copyfile.c\
$(SRC_DIR)eco.c\
$(SRC_DIR)fics_getsalt.cpp\
$(SRC_DIR)ficslim.cpp\
diff --git a/FICS/copyfile.c b/FICS/copyfile.c
new file mode 100644
index 0000000..32c1ce3
--- /dev/null
+++ b/FICS/copyfile.c
@@ -0,0 +1,70 @@
+// SPDX-FileCopyrightText: 2025 Markus Uhlin <maxxe@rpblc.net>
+// SPDX-License-Identifier: ISC
+
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "copyfile.h"
+
+bool
+fics_copyfile(const char *p1, const char *p2)
+{
+ char tmp[2048] = { '\0' };
+ int fd[2];
+ ssize_t ret[2];
+ ssize_t total_read = 0;
+ ssize_t total_written = 0;
+
+ if (p1 == NULL || p2 == NULL ||
+ strcmp(p1, "") == 0 ||
+ strcmp(p2, "") == 0)
+ return false;
+ if ((fd[0] = open(p1, O_RDONLY)) < 0) {
+ warn("%s: open(%s, ...)", __func__, p1);
+ return false;
+ }
+
+ fd[1] = open(p2, (O_RDWR|O_APPEND|O_CREAT|O_TRUNC),
+ (S_IRUSR|S_IWUSR|S_IRGRP));
+ if (fd[1] < 0) {
+ warn("%s: open(%s, ...)", __func__, p2);
+ close(fd[0]);
+ return false;
+ }
+
+ while ((ret[0] = read(fd[0], tmp, sizeof tmp)) != -1 && ret[0] != 0) {
+ total_read += ret[0];
+
+ if ((ret[1] = write(fd[1], tmp, ret[0])) != ret[0]) {
+ warnx("%s: written mismatch read", __func__);
+ break;
+ }
+
+ total_written += ret[1];
+ }
+
+ close(fd[0]);
+ close(fd[1]);
+
+ if (total_read != total_written) {
+ warnx("%s: total written mismatch total read", __func__);
+ return false;
+ }
+
+ return true;
+}
+
+bool
+is_regular_file(const char *path)
+{
+ struct stat sb = { 0 };
+
+ if (path == NULL || strcmp(path, "") == 0)
+ return false;
+ return (stat(path, &sb) == 0 && S_ISREG(sb.st_mode));
+}
diff --git a/FICS/copyfile.h b/FICS/copyfile.h
new file mode 100644
index 0000000..afe1699
--- /dev/null
+++ b/FICS/copyfile.h
@@ -0,0 +1,13 @@
+#ifndef COPYFILE_H
+#define COPYFILE_H
+
+#include <stdbool.h>
+
+#include "common.h"
+
+__FICS_BEGIN_DECLS
+bool fics_copyfile(const char *, const char *);
+bool is_regular_file(const char *);
+__FICS_END_DECLS
+
+#endif