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