System calls with files

2

I have a code whose mission is to copy a source to a destination. The point is that, when compiling, it warns me that on line 38 an assignment is made from an integer to a pointer without a molding and I do not know how to fix it.

Also, running it in linux gives me a generated 'core' segment violation. Likewise, execution is aborted in CodeBlocks.

This is my code:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

int copia (char * ruta_fue, char * ruta_des){
    int fd_fue,fd_des,leidos,escritos;
    void * dato;
    if(access(ruta_des,F_OK)==0){
        fd_fue = open(ruta_des,O_WRONLY|O_TRUNC);
    } else {
        fd_fue = creat(ruta_des,0600);
    }
    fd_fue = open(ruta_fue,O_RDONLY);
    if(fd_fue<0){
        fprintf(stderr, "Ha fallado la apertura de %s\n", ruta_fue);
        perror("");
        return -1;
    }
    while( ( leidos = read(fd_fue,&dato,sizeof(int) ) ) > 0 ){
        if( ( escritos = write(fd_des,dato,leidos) ) < 0 ){
            fprintf(stderr, "Ha fallado la escritura de %s\n", ruta_des);
            perror("");
            return -1;
        }
    }
    close(fd_fue);
    close(fd_des);
    printf("La copia se ha realizado bien");
    return 1;
}

int main(int argc, char * argv[]){
    char * ruta_fue,ruta_des;
    ruta_fue = (char *)argv[1];
    ruta_des = (char *)argv[2];
    copia(ruta_fue,ruta_des);

    struct stat info_fue, info_des;
    stat(ruta_fue,&info_fue);
    stat(ruta_des,&info_des);
    if(info_fue.st_size!=info_des.st_size){
        fprintf(stderr,"Los tamaños de %s y %s no coinciden",ruta_fue,ruta_des);
        return -1;
    }
    printf("Los tamaños de %s y %s son iguales",ruta_fue,ruta_des);
}

Note: line 38 is ruta_des = (char *)argv[2]; .

    
asked by enoy 21.03.2016 в 18:58
source

2 answers

3

As a general rule try to compile with the flags -Wall -Wextra -Wpedantic , and then try to fix the errors one by one starting with the first one that comes out.

For example, of your application (this should have been attached to the question):

 In function 'int main(int, char**)':
38:14: error: invalid conversion from 'char*' to 'char' [-fpermissive]
39:28: error: invalid conversion from 'char' to 'char*' [-fpermissive]
43:28: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
45:80: warning: format '%s' expects argument of type 'char*', but argument 4 has type 'int' [-Wformat=]
48:67: warning: format '%s' expects argument of type 'char*', but argument 3 has type 'int' [-Wformat=]
 At global scope:
35:14: warning: unused parameter 'argc' [-Wunused-parameter]

We see that you are trying to assign a char to a char *. This happens because you have the declaration of the variables incorrectly:

char * ruta_fue,ruta_des;
  • ruta_fue is a char *
  • ruta_des is a char !!!

Change it to:

char *ruta_fue, *ruta_des;

Another problem you have is that you do not check if the number of parameters received by the argc program matches what you expect: (3 or more) since access argc[1] and argc[2] may not be valid . Protect that region by checking if argc is what you expect and the program ends:

if (argc < 3) return -1;

Another mistake you have is:

23:50: warning: 'fd_des' may be used uninitialized in this function [-Wmaybe-uninitialized]

You should initialize that descriptor file before calling write

    
answered by 21.03.2016 в 19:24
2

The problem is that statement

char * ruta_fue,ruta_des;

is equivalent to

char * ruta_fue;
char ruta_des;

What you want, on the other hand, is

char * ruta_fue;
char * ruta_des;

or, if you want to put it on a line:

char *ruta_fue, *ruta_des;

In the declaration char * x; imagine that the asterisk is stuck to the variable, not the type: it is read as char (* x) , which in turn reads as " x points to a char "- or" x dereference is of type char "). Thus char * x, z ; means " x points to char and z is% char "

( Details - in English).

    
answered by 21.03.2016 в 19:13