summaryrefslogtreecommitdiff
path: root/src/commands/cp.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/commands/cp.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/commands/cp.c b/src/commands/cp.c
index 37e3354..df88155 100644
--- a/src/commands/cp.c
+++ b/src/commands/cp.c
@@ -50,16 +50,18 @@ static int short_arg(char c, char* next) {
}
static bool copy_file(char* from, char* to) {
- FILE* from_f = get_file_s(from, "r");
+ #define BS 1024
+
+ FILE *from_f, *to_f;
+ char buf[BS];
+ int read;
+
+ from_f = get_file_s(from, "r");
if (from_f == NULL) { return false; }
- FILE* to_f = get_file_s(to, "w");
+ to_f = get_file_s(to, "w");
if (to_f == NULL) { fclose(from_f); return false; }
- #define BS 1024
-
- char buf[BS];
- int read;
while ((read = fread(buf, 1, BS, from_f)) > 0) {
fwrite(buf, 1, read, to_f);
@@ -123,6 +125,10 @@ static void run_copy(struct stat* s) {
static void cp_file(char* path);
static void cp_directory(struct stat* s) {
+
+ DIR* d;
+ struct dirent* file;
+
if (!flags.recurse) {
error_s("-r not specified; omitting directory '%s'", get_path_buffer());
return;
@@ -133,14 +139,13 @@ static void cp_directory(struct stat* s) {
return;
}
- DIR* d = opendir(get_path_buffer());
+ d = opendir(get_path_buffer());
if (d == NULL) {
error_s("cannot open directory '%s': %s", get_path_buffer(), strerror(errno));
return;
}
- struct dirent* file;
while ((file = readdir(d)) != NULL) {
if (is_dot_dir(file->d_name)) continue;
cp_file(file->d_name);
@@ -148,10 +153,11 @@ static void cp_directory(struct stat* s) {
}
static char* get_file_name(char* path) {
- if (path[0] == '\0') return path;
-
int last = 0;
int i = 0;
+
+ if (path[0] == '\0') return path;
+
while (true) {
if (path[i+1] == '\0') break;
if (path[i] == '/') {
@@ -188,19 +194,22 @@ static void cp_file(char* path) {
COMMAND(cp) {
+ int start, i;
+ struct stat s;
+
flags.hard_link = false;
flags.sym_link = false;
flags.preserve = false;
flags.recurse = false;
flags.verbose = false;
- int start = parse_args(argc, argv, help, short_arg, NULL);
+ start = parse_args(argc, argv, help, short_arg, NULL);
if (argc - start < 2) {
global_help(help);
}
- // only when 2 args and first is a file, the 2nd will be a file
+ /* only when 2 args and first is a file, the 2nd will be a file */
if (argc - start == 2) {
struct stat s;
if (lstat(argv[start], &s) < 0) {
@@ -216,15 +225,14 @@ COMMAND(cp) {
return EXIT_SUCCESS;
}
- // push directory
+ /* push directory */
push_path_buffer_2(argv[argc-1]);
- struct stat s;
if (lstat(get_path_buffer_2(), &s) < 0) {
error("target: '%s': %s", get_path_buffer_2(), strerror(errno));
}
- for (int i = start; i < argc - 1; i++) {
+ for (i = start; i < argc - 1; i++) {
cp_file(argv[i]);
}