Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.5 kB
7
Indexable
Never
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 4096

char *base2(char octal)
{
    switch(octal)
    {
        case '0': return "000";
        case '1': return "001";
        case '2': return "010";
        case '3': return "011";
        case '4': return "100";
        case '5': return "101";
        case '6': return "110";
        case '7': return "111";
        default: return NULL;
    }
}

int main(int argc, char *argv[])
{
    int input_fd,output_fd;
    char buffer[BUF_SIZE];
    ssize_t bytes_in;

    if(argc<2)
    {
        printf("Usage: cp file_src file_dest.\n/");
        return 1;
    }
    else if(argc<3)
    {
        char temp[]="temporarXXXXXX";
        int temp_fd=mkstemp(temp);
        if(temp_fd==-1)
        {
            perror("Eroare la fisierul temporar.");
            return 2;
        }
        close(temp_fd);
        temp_fd=open(temp,O_RDWR|O_CREAT,0600);
        if(temp_fd==-1)
        {
            perror("Eroare la deschiderea fisierului temporar.");
            return 3;
        }
        input_fd=open(argv[1],O_RDWR);
        if(input_fd==-1)
        {
            perror("Eroare la primul fisier.");
            return 4;
        }
        while((bytes_in=read(input_fd,buffer,BUF_SIZE))>0)
        {
            for(int i=0;i<bytes_in;i++)
            {
                if(buffer[i]>='0' && buffer[i]<='7')
                {
                    char *binar=base2(buffer[i]);
                    write(temp_fd,binar,3);
                }
                else
                    write(temp_fd,&buffer[i],1);
            }
        }

        if(lseek(temp_fd,0,SEEK_SET)==-1)
        {
            perror("Eroare la functia lseek.");
            return 5;
        }

        lseek(input_fd,0,SEEK_SET);

        while((bytes_in=read(temp_fd,buffer,BUF_SIZE))>0)
        {
            int bytes_out=write(input_fd,buffer,bytes_in);
            if(bytes_in!=bytes_out)
            {
                printf("Fatal write error.");
                return 6;
            }
        }
        close(input_fd);
        close(temp_fd);
    }
    else
    {
    input_fd=open(argv[1],O_RDONLY);
    if(input_fd==-1)
    {
        perror("Eroare la primul fisier.");
        return 7;
    }

    output_fd=open(argv[2],O_WRONLY|O_CREAT);
    if(output_fd==-1)
    {
        perror("Eroare la al doilea fisier.");
        return 8;
    }

    struct stat st;
    stat(argv[2],&st);
    if(st.st_size>0)
    {
        
        printf("Doriti suprascrierea fisierului? (Y/N): ");
        char rez;
        scanf("%c",&rez);
        if(rez!='Y')
        {
            close(input_fd);
            close(output_fd);
            return 9;
        }
        else
        {
            int result=ftruncate(output_fd,0);
            if(result!=0)
            {
                perror("Nu a avut loc trunchierea.");
                close(input_fd);
                close(output_fd);
                return 10;
            }
        }
    }

    while((bytes_in=read(input_fd,buffer,BUF_SIZE))>0)
    {
        for(int i=0;i<bytes_in;i++)
        {
            if(buffer[i]>='0' && buffer[i]<='7')
            {
                char *binar=base2(buffer[i]);
                write(output_fd,binar,3);
            }
            else
                write(output_fd,&buffer[i],1);
        }
    }
    
    close(input_fd);
    close(output_fd);
    }

    return 0;
}

//sugi?
Leave a Comment