cp fix
This commit is contained in:
parent
a2fae4ab76
commit
8f4c4dd8a0
14
LICENSE
14
LICENSE
@ -1,14 +0,0 @@
|
|||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
Version 2, December 2004
|
|
||||||
|
|
||||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim or modified
|
|
||||||
copies of this license document, and changing it is allowed as long
|
|
||||||
as the name is changed.
|
|
||||||
|
|
||||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
||||||
|
|
||||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
|
||||||
|
|
@ -23,10 +23,10 @@ char *make_path(const char *src, const char *dst) {
|
|||||||
|
|
||||||
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
||||||
off_t len = lseek(ifd, 0, SEEK_END);
|
off_t len = lseek(ifd, 0, SEEK_END);
|
||||||
|
if (len <= 0) {
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
else if (len == 0) {
|
|
||||||
int fd = open(dst, O_CREAT | O_RDONLY, mode);
|
int fd = open(dst, O_CREAT | O_RDONLY, mode);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return 1;
|
return 1;
|
||||||
@ -60,18 +60,51 @@ int copy(int mode, const char *src, const char *dst) {
|
|||||||
if (ifd < 0)
|
if (ifd < 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
/* test ./testdir/ -> test ./testdir/test */
|
||||||
|
char *dup = NULL;
|
||||||
|
char *new_path = (char *)dst;
|
||||||
|
int flag = 0;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
|
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
|
||||||
if (ofd < 0) {
|
if (ofd < 0) {
|
||||||
close(ifd);
|
ret = 1;
|
||||||
return 1;
|
|
||||||
|
dup = strdup(src);
|
||||||
|
if (dup == NULL)
|
||||||
|
goto CLOSE;
|
||||||
|
|
||||||
|
flag = 1;
|
||||||
|
new_path = NULL;
|
||||||
|
|
||||||
|
new_path = make_path(dst, basename(dup));
|
||||||
|
if (new_path == NULL)
|
||||||
|
goto CLOSE;
|
||||||
|
|
||||||
|
ofd = open(new_path, O_CREAT | O_TRUNC | O_RDWR, mode);
|
||||||
|
if (ofd < 0)
|
||||||
|
goto CLOSE;
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (write_buffer(mode, ifd, ofd, dst))
|
if (write_buffer(mode, ifd, ofd, new_path))
|
||||||
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
|
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
|
||||||
|
|
||||||
close(ifd);
|
|
||||||
close(ofd);
|
close(ofd);
|
||||||
return 0;
|
|
||||||
|
CLOSE:
|
||||||
|
close(ifd);
|
||||||
|
if (flag) {
|
||||||
|
if (dup != NULL)
|
||||||
|
free(dup);
|
||||||
|
|
||||||
|
if (new_path != NULL)
|
||||||
|
free(new_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_stat(const char *path, struct stat *stat_path) {
|
int get_stat(const char *path, struct stat *stat_path) {
|
||||||
@ -88,9 +121,9 @@ int cptree(const char *src, const char *dst) {
|
|||||||
if (get_stat(src, &stat_path))
|
if (get_stat(src, &stat_path))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (S_ISDIR(stat_path.st_mode) == 0) {
|
if (!S_ISDIR(stat_path.st_mode)) {
|
||||||
if (copy(stat_path.st_mode, src, dst)) {
|
if (copy(stat_path.st_mode, src, dst)) {
|
||||||
fprintf(stderr, "cp: %s: copy() failed (%s)\n", src, strerror(errno));
|
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,11 +20,11 @@ int make_temp_dir(char *tmp) {
|
|||||||
|
|
||||||
int get_suffix(const char *str) {
|
int get_suffix(const char *str) {
|
||||||
size_t len = strlen(str);
|
size_t len = strlen(str);
|
||||||
for (size_t i = len - 1; i >= 0; i--)
|
for (size_t i = len - 1; i > 0; i--)
|
||||||
if (str[i] == 'X')
|
if (str[i] == 'X')
|
||||||
return len - i - 1;
|
return len - i - 1;
|
||||||
|
|
||||||
exit(1);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int make_temp_file(char *tmp) {
|
int make_temp_file(char *tmp) {
|
||||||
|
Loading…
Reference in New Issue
Block a user