Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz odpowiednik programu „cp” do kopiowania plików, bez obsługi katalogów i opcji wykonania.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #define BUFFER_SIZE 128
- int main(int argc, char *argv[])
- {
- if(argc != 3) {
- fprintf(stderr, "Niepoprawna liczba argumentow!\nUzycie programu: %s [plik, ktory ma zostac skopiowany] [nowa nazwa/sciezka pliku]\n", argv[0]);
- return EXIT_FAILURE;
- }
- char *first_filename = argv[1];
- char *second_filename = argv[2];
- int first_fd, second_fd;
- ssize_t status;
- char buffer[BUFFER_SIZE];
- ssize_t bytes_count = 0;
- if((first_fd = open(first_filename, O_RDONLY, 0600)) == -1) {
- perror("open_first");
- return EXIT_FAILURE;
- }
- if((second_fd = open(second_filename, O_WRONLY | O_CREAT, 0600)) == -1) {
- perror("open_second");
- return EXIT_FAILURE;
- }
- for(;;) {
- status = read(first_fd, buffer, BUFFER_SIZE-1);
- if(status == -1) {
- perror("read");
- return EXIT_FAILURE;
- }
- else if(status == 0) {
- break;
- }
- else {
- bytes_count += status;
- if(write(second_fd, buffer, BUFFER_SIZE-1) == -1) {
- perror("write");
- return EXIT_FAILURE;
- }
- }
- }
- printf("Skopiowano pomyslnie %ld bajtow!\n", bytes_count);
- if(close(second_fd) == -1) {
- perror("close_second");
- return EXIT_FAILURE;
- }
- if(close(first_fd) == -1) {
- perror("close_first");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment