Question

I'm having a little trouble using the stat function in C. My goal is to implement the ls command-line function, and I'm just starting my project.

I was already able to print all files in my current directory using something like :

    env.current_directory = opendir(".");
    while ((env.ent = readdir(env.current_directory)) != NULL)
    {
        stat(env.ent->d_name, &env.infos);
        ft_putstr(env.ent->d_name);
        ft_putchar('\n');
    }
    closedir(env.current_directory);

Okay, so this is working fine. But this is not enough, since I have to implement options [-Ralrt]. In order to deal with sorting and such, I've declared the following structs (this is obviously not definitive and by the way if you think I'm misleading myself, I'll happily consider any idea :D ):

typedef struct s_ent_info       t_ent_info;
struct                          s_ent_info
{
    char                        *name;
    struct stat                 props;
};
typedef struct s_environnement  t_env;
struct                          s_environnement
{
    int                         entity_qty;
    DIR                         *current_directory;
    struct dirent               *current_entity;
    /* struct stat              infos;*/ // This was for testing
    t_ent_info                  **entity;
};

And adapted the first method by doing this :

t_env   env;
int i;

env.entity_qty = i = 0;
env.current_directory = opendir(".");
while ((env.current_entity = readdir(env.current_directory)) != NULL)
    env.entity_qty++;
env.entity = malloc(sizeof(env.entity) * (env.entity_qty + 1));
if (env.entity)
{
    env.entity[env.entity_qty] = NULL;
    rewinddir(env.current_directory);
    while ((env.current_entity = readdir(env.current_directory)) != NULL)
    {
        env.entity[i] = malloc(sizeof(t_ent_info *));
        env.entity[i]->name = ft_set_new_str(env.current_entity->d_name);
        printf("Before i %d name %s adress %p\n", i, env.entity[i]->name, &env.entity[i]->name);
        stat(env.current_entity->d_name, &env.entity[i]->props);
        printf("After i %d name %s adress %p\n", i, env.entity[i]->name, &env.entity[i]->name);
        i++;
    }
}

As you can see there is already a couple of printf, cause yeah, here is the deal. The "Before" printf call outputs a totally normal string. But the "After" one is only garbage, except for the first one... And I don't understand why, or what did I do wrong (btw I KNOW I did something wrong, I just don't know what).

Here is what the program outputs (You can see that adresses are the same... This is the most confusing part for me) :

Before i 0 name . adress 0x7fb4e24000e0
After i 0 name . adress 0x7fb4e24000e0

Before i 1 name .. adress 0x7fb4e24046e0
After i 1 name �8� adress 0x7fb4e24046e0

Before i 2 name .git adress 0x7fb4e2404710
After i 2 name 29� adress 0x7fb4e2404710

...

Anyone having a clue?

EDIT :

I've tried to use a specific char ** to keep the files names, thinking that maybe somehow stat would mess up my struct, but it doesn't work either, I have the same result.

Was it helpful?

Solution

This line:

env.entity[i] = malloc(sizeof(t_ent_info *));

Needs to be:

env.entity[i] = malloc(sizeof(t_ent_info));

Currently you're only allocating enough space for a t_ent_info pointer - you need to allocate the size of the whole structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top