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 <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 8
enum ERROR{NO_ARGUMENTS, WRONG_ARGUMENT};
@ -22,8 +26,22 @@ int print_help() {
}
int cp(char *argv[]){
printf("%s\n", argv[0]);
open();
int old, new;
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;
}
@ -42,7 +60,7 @@ int main(int argc, char *argv[]) {
}
}
if (argc == 3) {
cp(argv);
if (cp(argv) > 0) return 1;
}
return 0;
}

View File

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