/* These replace the system command cp and mv without doing a system command */ /* If the file is on a different filesystem, it will do a copy, then unlink */ /* Returns 0 on success and -1 on failure */ #include #include #include #include #include #include #define BUFSIZE 8192 int FileCopy (const char *source, const char *dest) { char buf[BUFSIZE]; int f, t, k, mode, perm; struct stat statbuf; if ((f = open(source, O_RDONLY, 0)) < 0) return(-1); fstat (f, &statbuf); perm = statbuf.st_mode; mode = O_CREAT | O_WRONLY | O_TRUNC; if ((t = open(dest, mode, perm)) < 0) { close(f); return(-1); } while ((k = read(f, buf, BUFSIZE)) > 0 && write(t, buf, k) == k); close(f); close(t); if (k != 0) { return(-1); } return(0); } int FileMove (const char *source, const char *dest) { int status; status = rename (source, dest); if ((status == -1) && (errno = EXDEV)) { if ((status = FileCopy (source, dest)) == 0) unlink (source); else return (-1); } return (status); }