1: /*
2: * $Id: fs.c,v 1.3 2025/03/09 19:14:25 snw Exp $
3: * filesystem support functions
4: *
5: *
6: * Author: Serena Willis <snw@coherent-logic.com>
7: * Copyright (C) 1998 MUG Deutschland
8: * Copyright (C) 2024, 2025 Coherent Logic Development LLC
9: *
10: *
11: * This file is part of FreeM.
12: *
13: * FreeM is free software: you can redistribute it and/or modify
14: * it under the terms of the GNU Affero Public License as published by
15: * the Free Software Foundation, either version 3 of the License, or
16: * (at your option) any later version.
17: *
18: * FreeM is distributed in the hope that it will be useful,
19: * but WITHOUT ANY WARRANTY; without even the implied warranty of
20: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21: * GNU Affero Public License for more details.
22: *
23: * You should have received a copy of the GNU Affero Public License
24: * along with FreeM. If not, see <https://www.gnu.org/licenses/>.
25: *
26: * $Log: fs.c,v $
27: * Revision 1.3 2025/03/09 19:14:25 snw
28: * First phase of REUSE compliance and header reformat
29: *
30: *
31: * SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
32: * SPDX-License-Identifier: AGPL-3.0-or-later
33: **/
34:
35: #include <fcntl.h>
36: #include <unistd.h>
37: #include <errno.h>
38: #include "fs.h"
39:
40: int cp(const char *to, const char *from)
41: {
42: int fd_to, fd_from;
43: char buf[4096];
44: ssize_t nread;
45: int saved_errno;
46:
47: fd_from = open(from, O_RDONLY);
48: if (fd_from < 0)
49: return -1;
50:
51: fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
52: if (fd_to < 0)
53: goto out_error;
54:
55: while (nread = read(fd_from, buf, sizeof buf), nread > 0)
56: {
57: char *out_ptr = buf;
58: ssize_t nwritten;
59:
60: do {
61: nwritten = write(fd_to, out_ptr, nread);
62:
63: if (nwritten >= 0)
64: {
65: nread -= nwritten;
66: out_ptr += nwritten;
67: }
68: else if (errno != EINTR)
69: {
70: goto out_error;
71: }
72: } while (nread > 0);
73: }
74:
75: if (nread == 0)
76: {
77: if (close(fd_to) < 0)
78: {
79: fd_to = -1;
80: goto out_error;
81: }
82: close(fd_from);
83:
84: /* Success! */
85: return 0;
86: }
87:
88: out_error:
89: saved_errno = errno;
90:
91: close(fd_from);
92: if (fd_to >= 0)
93: close(fd_to);
94:
95: errno = saved_errno;
96: return -1;
97: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>