I am unable to access a memory address given in a string

83 views Asked by At

So my problem is that i have to read the content of a file into the buffer (that is a void*).

I searched a lot and I can't find any information about converting the memory address in the string to an actually void*, and probably has a dumb solution but I have to ask here.

This my code:

int ReadFile(char *f, void *p, ssize_t cont)
{
    struct stat s;
    ssize_t n;
    int fd;

    if (stat(f, &s) == -1 || (fd = open(f, O_RDONLY)) == -1)
    {
        perror("ERROR: ");
        return -1;
    }
    if (cont == -1) 
        cont = s.st_size;
    if ((n = read(fd, p, cont)) == -1)
    {
        perror("ERROR: ");
        close(df);
        return -1;
    }
    printf("Read %ld bytes from %s in %p\n", n, f, p);
    close(df);
    return 0;
}

I think this function is kinda ok.

My problem resides into when I call it, the program asks for an input, like a shell, this can be an example of request:

> read file.txt 0xffffffff 15

being 0xffffffff the memory addres where read will send the bytes gotten, my program gets this memory address as a string like:

"read file.txt 0xffffffff 15"

I already have the step where I split all this string into segments like: "read" , "file.txt" , "0xffffffff", "15".

But when I have to pass the function read the required arguments, I can't find the way to convert "0xffffffff" to 0xfffffff.

1

There are 1 answers

3
gulpr On

I can't find the way to convert "0xffffffff" to 0xfffffff.

unsigned val;
sscanf("0xffffffff", "0x%x", &val);  //you can also "%x"

// now val contains the value needed 

Replace "0xffffffff" with your string.