sbase/util/cp.c

145 lines
3.0 KiB
C
Raw Normal View History

2012-01-30 22:41:33 +00:00
/* See LICENSE file for copyright and license details. */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
2012-01-30 22:41:33 +00:00
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
2014-07-09 21:28:43 +00:00
#include <sys/types.h>
#include <unistd.h>
2014-07-09 21:28:43 +00:00
#include <utime.h>
2012-01-30 22:41:33 +00:00
#include "../fs.h"
#include "../text.h"
#include "../util.h"
2014-07-09 21:28:43 +00:00
bool cp_aflag = false;
bool cp_dflag = false;
2014-05-05 13:58:14 +00:00
bool cp_fflag = false;
2014-07-09 21:28:43 +00:00
bool cp_pflag = false;
2012-01-30 22:41:33 +00:00
bool cp_rflag = false;
bool cp_vflag = false;
int cp_status = EXIT_SUCCESS;
2012-01-30 22:41:33 +00:00
int
cp(const char *s1, const char *s2)
{
FILE *f1, *f2;
char *ns1, *ns2;
long size1, size2;
struct dirent *d;
struct stat st;
2014-07-09 21:28:43 +00:00
struct utimbuf ut;
char buf[PATH_MAX];
2012-01-30 22:41:33 +00:00
DIR *dp;
int r;
2012-01-30 22:41:33 +00:00
if(cp_vflag)
printf("'%s' -> '%s'\n", s1, s2);
2014-07-09 21:28:43 +00:00
if(cp_dflag == true)
r = lstat(s1, &st);
else
r = stat(s1, &st);
2014-07-09 21:28:43 +00:00
if(r == 0) {
if(cp_dflag == true && S_ISLNK(st.st_mode)) {
if(readlink(s1, buf, sizeof(buf) - 1) >= 0) {
if(cp_fflag == true);
unlink(s2);
if(symlink(buf, s2) != 0) {
weprintf("%s: can't create '%s'\n", argv0, s2);
cp_status = EXIT_FAILURE;
return 0;
}
2014-07-09 21:28:43 +00:00
}
goto preserve;
2014-07-09 21:28:43 +00:00
}
if(S_ISDIR(st.st_mode)) {
if (!cp_rflag)
eprintf("%s: is a directory\n", s1);
2014-07-09 21:28:43 +00:00
if(!(dp = opendir(s1)))
eprintf("opendir %s:", s1);
if (mkdir(s2, st.st_mode) == -1 && errno != EEXIST)
eprintf("mkdir %s:", s2);
apathmax(&ns1, &size1);
apathmax(&ns2, &size2);
while((d = readdir(dp))) {
if(strcmp(d->d_name, ".") && strcmp(d->d_name, "..")) {
2014-07-09 21:28:43 +00:00
r = snprintf(ns1, size1, "%s/%s", s1, d->d_name);
if(r >= size1 || r < 0) {
eprintf("%s/%s: filename too long\n",
s1, d->d_name);
}
r = snprintf(ns2, size2, "%s/%s", s2, d->d_name);
if(r >= size2 || r < 0) {
eprintf("%s/%s: filename too long\n",
s2, d->d_name);
}
fnck(ns1, ns2, cp);
}
}
2014-07-09 21:28:43 +00:00
closedir(dp);
free(ns1);
free(ns2);
goto preserve;
}
2012-01-30 22:41:33 +00:00
}
if(cp_aflag == true) {
if(S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) ||
S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode)) {
unlink(s2);
if(mknod(s2, st.st_mode, st.st_rdev) < 0) {
weprintf("%s: can't create '%s':", argv0, s2);
cp_status = EXIT_FAILURE;
return 0;
}
goto preserve;
}
}
2012-01-30 22:41:33 +00:00
if(!(f1 = fopen(s1, "r")))
eprintf("fopen %s:", s1);
2014-05-05 13:58:14 +00:00
if(!(f2 = fopen(s2, "w"))) {
if (cp_fflag == true) {
2014-05-05 13:58:14 +00:00
unlink(s2);
if(!(f2 = fopen(s2, "w")))
eprintf("fopen %s:", s2);
} else {
2014-05-05 13:58:14 +00:00
eprintf("fopen %s:", s2);
}
2014-05-05 13:58:14 +00:00
}
2012-01-30 22:41:33 +00:00
concat(f1, s1, f2, s2);
fchmod(fileno(f2), st.st_mode);
2012-01-30 22:41:33 +00:00
fclose(f2);
fclose(f1);
2014-07-09 21:28:43 +00:00
preserve:
if(cp_aflag == true || cp_pflag == true) {
if(!(S_ISLNK(st.st_mode))) {
/* timestamp */
ut.actime = st.st_atime;
ut.modtime = st.st_mtime;
utime(s2, &ut);
}
2014-07-09 21:28:43 +00:00
/* preserve owner ? */
if(S_ISLNK(st.st_mode))
r = lchown(s2, st.st_uid, st.st_gid);
else
r = chown(s2, st.st_uid, st.st_gid);
if(r == -1) {
2014-07-09 21:28:43 +00:00
weprintf("cp: can't preserve ownership of '%s':", s2);
cp_status = EXIT_FAILURE;
}
2014-07-09 21:28:43 +00:00
}
2012-01-30 22:41:33 +00:00
return 0;
}