2021-03-09 23:20:28 +01:00
|
|
|
// UNIX cp copy
|
|
|
|
// Wrote for Computer Networks and Systems lab classes
|
|
|
|
// AUTHOR : Sergiusz Warga
|
|
|
|
|
2021-03-24 18:52:27 +01:00
|
|
|
#include <fcntl.h>
|
2021-03-03 15:22:13 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-03-09 23:20:28 +01:00
|
|
|
#include <string.h>
|
2021-03-24 18:52:27 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2021-03-03 16:32:13 +01:00
|
|
|
|
2021-03-09 23:20:28 +01:00
|
|
|
#define BUFFER_SIZE 8 // TODO: Think about it.
|
2021-03-03 15:22:13 +01:00
|
|
|
|
2021-03-09 23:20:28 +01:00
|
|
|
enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT, DUNNO};
|
2021-03-03 15:22:13 +01:00
|
|
|
|
|
|
|
int print_error_message(enum ERROR error_type) {
|
|
|
|
if (error_type == NO_ARGUMENTS) {
|
|
|
|
fprintf(stderr,"cp: missing file operand\n"); // TODO: Maybe replace "cp" with argv[0]?
|
|
|
|
fprintf(stderr,"Try cp --help for more information.\n");
|
|
|
|
return 0;
|
|
|
|
} else if (error_type == WRONG_ARGUMENT) {
|
|
|
|
fprintf(stderr, "cp: unrecognized option\n");
|
|
|
|
fprintf(stderr,"Try cp --help for more information.\n");
|
2021-03-09 23:20:28 +01:00
|
|
|
} else if (error_type == DUNNO) {
|
|
|
|
fprintf(stderr, "Something went wrong!\n");
|
2021-03-03 15:22:13 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int print_help() {
|
|
|
|
printf("HELP\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:20:28 +01:00
|
|
|
int is_dir(const char *name) {
|
|
|
|
struct stat path;
|
|
|
|
stat(name, &path);
|
|
|
|
return S_ISDIR(path.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int cp(char *old_name, char *new_name) {
|
|
|
|
// int cp(char *old_argv, char *new_argv) {
|
|
|
|
// char *old_name, *new_name;
|
|
|
|
// strncpy(old_name, old_argv, sizeof(old_argv));
|
|
|
|
// strncpy(new_name, new_argv, sizeof(new_argv));
|
2021-03-03 16:32:13 +01:00
|
|
|
int old, new;
|
2021-03-09 23:20:28 +01:00
|
|
|
if (is_dir(new_name)) {
|
|
|
|
printf("%s\n", new_name);
|
|
|
|
strncat(new_name, "/", 1);
|
|
|
|
printf("%s\n", new_name);
|
|
|
|
strncat(new_name, old_name, sizeof(old_name));
|
|
|
|
printf("%s\n", new_name);
|
|
|
|
}
|
|
|
|
printf("Moving %s to %s\n", old_name, new_name);
|
|
|
|
old = open(old_name, O_RDONLY);
|
|
|
|
new = open(new_name, O_CREAT | O_WRONLY);
|
2021-03-03 16:32:13 +01:00
|
|
|
char buffer[BUFFER_SIZE];
|
2021-03-03 16:39:18 +01:00
|
|
|
int read_bytes;
|
2021-03-03 16:32:13 +01:00
|
|
|
|
2021-03-03 16:39:18 +01:00
|
|
|
while ((read_bytes = read(old, buffer, sizeof(buffer))) > 0) {
|
|
|
|
write(new, buffer, read_bytes);
|
2021-03-03 16:32:13 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 23:20:28 +01:00
|
|
|
printf("%s moved to %s\n", old_name, new_name);
|
2021-03-03 16:59:11 +01:00
|
|
|
|
2021-03-03 16:32:13 +01:00
|
|
|
close(old);
|
|
|
|
close(new);
|
|
|
|
|
2021-03-03 15:22:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-09 23:20:28 +01:00
|
|
|
int copy_mod_bits(char *old_name, char *new_name) {
|
|
|
|
struct stat mod_bits;
|
|
|
|
|
|
|
|
stat(old_name, &mod_bits);
|
|
|
|
chmod(new_name, mod_bits.st_mode);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:22:13 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc == 1) {
|
|
|
|
print_error_message(NO_ARGUMENTS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (argc == 2) {
|
|
|
|
if (strcmp(argv[1], "--help") == 0) {
|
|
|
|
print_help();
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
print_error_message(WRONG_ARGUMENT);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc == 3) {
|
2021-03-09 23:20:28 +01:00
|
|
|
if (cp(argv[1], argv[2]) > 0) return 1;
|
|
|
|
copy_mod_bits(argv[1], argv[2]);
|
|
|
|
}
|
|
|
|
if (argc > 3) {
|
|
|
|
int no_of_old_files = argc - 2;
|
|
|
|
for (int i = 1; i <= no_of_old_files; ++i) {
|
|
|
|
printf("TOOO %s\n", argv[argc-1]);
|
|
|
|
if (cp(argv[i], argv[argc-1]) > 0) return 1;
|
|
|
|
copy_mod_bits(argv[i], argv[argc-1]);
|
|
|
|
}
|
2021-03-03 15:22:13 +01:00
|
|
|
}
|
2021-03-09 23:20:28 +01:00
|
|
|
|
2021-03-03 15:22:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|