2012-01-30 22:41:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
#include "../fs.h"
|
|
|
|
#include "../text.h"
|
|
|
|
#include "../util.h"
|
|
|
|
|
2014-05-05 13:58:14 +00:00
|
|
|
bool cp_fflag = false;
|
2012-01-30 22:41:33 +00:00
|
|
|
bool cp_rflag = false;
|
|
|
|
|
|
|
|
int
|
|
|
|
cp(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
FILE *f1, *f2;
|
|
|
|
char *ns1, *ns2;
|
|
|
|
long size1, size2;
|
|
|
|
struct dirent *d;
|
|
|
|
struct stat st;
|
|
|
|
DIR *dp;
|
|
|
|
|
|
|
|
if (stat(s1, &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
|
|
if (!cp_rflag) {
|
|
|
|
eprintf("%s: is a directory\n", s1);
|
2013-03-05 20:46:48 +00:00
|
|
|
} else {
|
2012-01-30 22:41:33 +00:00
|
|
|
if(!(dp = opendir(s1)))
|
|
|
|
eprintf("opendir %s:", s1);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
if (mkdir(s2, st.st_mode) == -1 && errno != EEXIST)
|
|
|
|
eprintf("mkdir %s:", s2);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
apathmax(&ns1, &size1);
|
|
|
|
apathmax(&ns2, &size2);
|
2013-03-05 20:46:48 +00:00
|
|
|
while((d = readdir(dp))) {
|
|
|
|
if(strcmp(d->d_name, ".")
|
|
|
|
&& strcmp(d->d_name, "..")) {
|
|
|
|
if(snprintf(ns1, size1, "%s/%s", s1,
|
2014-01-24 16:23:13 +00:00
|
|
|
d->d_name) >= size1) {
|
2013-03-05 20:46:48 +00:00
|
|
|
eprintf("%s/%s: filename too long\n",
|
|
|
|
s1, d->d_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(snprintf(ns2, size2, "%s/%s", s2,
|
2014-01-24 16:23:13 +00:00
|
|
|
d->d_name) >= size2) {
|
2013-03-05 20:46:48 +00:00
|
|
|
eprintf("%s/%s: filename too long\n",
|
|
|
|
s2, d->d_name);
|
|
|
|
}
|
2012-01-30 22:41:33 +00:00
|
|
|
fnck(ns1, ns2, cp);
|
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
}
|
2012-01-30 22:41:33 +00:00
|
|
|
closedir(dp);
|
|
|
|
free(ns1);
|
|
|
|
free(ns2);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!(f1 = fopen(s1, "r")))
|
|
|
|
eprintf("fopen %s:", s1);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2014-05-05 13:58:14 +00:00
|
|
|
if(!(f2 = fopen(s2, "w"))) {
|
2014-05-05 14:02:16 +00:00
|
|
|
if (cp_fflag == true) {
|
2014-05-05 13:58:14 +00:00
|
|
|
unlink(s2);
|
2014-05-05 14:02:16 +00:00
|
|
|
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 14:02:16 +00:00
|
|
|
}
|
2014-05-05 13:58:14 +00:00
|
|
|
}
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
concat(f1, s1, f2, s2);
|
2014-02-18 16:49:41 +00:00
|
|
|
fchmod(fileno(f2), st.st_mode);
|
2012-01-30 22:41:33 +00:00
|
|
|
fclose(f2);
|
|
|
|
fclose(f1);
|
2013-03-05 20:46:48 +00:00
|
|
|
|
2012-01-30 22:41:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|