Copying 8 bytes

This commit is contained in:
Sergiusz Warga 2021-03-03 16:32:13 +01:00
parent eeaf800130
commit f621349ca7
2 changed files with 27 additions and 8 deletions

View File

@ -1,6 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 8
enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT}; enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT};
@ -22,8 +26,22 @@ int print_help() {
} }
int cp(char *argv[]){ int cp(char *argv[]){
printf("%s\n", argv[0]); int old, new;
open(); old = open(argv[1], O_RDONLY);
new = open(argv[2], O_CREAT | O_WRONLY);
char buffer[BUFFER_SIZE];
int read_bytes = read(old, buffer, sizeof(buffer));
if (read_bytes < 0) {
fprintf(stderr, "There was a problem with reading a file!\n");
return 3;
}
printf("%d bytes were read", read_bytes);
write(new, buffer, read_bytes);
close(old);
close(new);
return 0; return 0;
} }
@ -42,7 +60,7 @@ int main(int argc, char *argv[]) {
} }
} }
if (argc == 3) { if (argc == 3) {
cp(argv); if (cp(argv) > 0) return 1;
} }
return 0; return 0;
} }

View File

@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
rm raz dwa; rm -f old_file new_file;
touch raz; echo "QWERTYUIOP" > old_file;
gcc main.c -o cp; gcc -Wall main.c -o cp;
./cp raz dwa; ./cp old_file new_file;
ls | grep dwa; chmod +rw new_file;
diff old_file new_file;