Obtain the absolute path of a file in C [Linux]

1

I'm currently trying to get the absolute path of a file in C [Linux] but I do not get the expected path, but it skips a folder at least that seems to be the error.
main.c

int main()
    {
        char archivos[MAXARCHIVOS][MAXLONG];
        char direccionesAbsolutas[MAXARCHIVOS][MAXDIRABS];
        char extensiones[MAXARCHIVOS][MAXLONG];
        int cantidadArchivos = archivosEnDirectorioRecursiva("./documentos", archivos, direccionesAbsolutas);
        return 0;
    }

functions.c

int archivosEnDirectorioRecursiva(char *basePath, char archivos[MAXARCHIVOS][MAXLONG], char dirs[MAXARCHIVOS][MAXDIRABS])
{
    char buf[PATH_MAX + 1];
    static int i = 0;
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);

    // Unable to open directory stream
    if (!dir)
        return 0;

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {
            if(dp->d_name[0] != '.' && (int)dp->d_type == 8)
            {
                realpath(dp->d_name, buf);
                strcpy(dirs[i], buf);
                printf("%s\n", buf);
                strcpy(archivos[i], dp->d_name);
                i++;
            }
            // Construct new path from our base path
            strcpy(path, basePath);
            strcat(path, "/");
            strcat(path, dp->d_name);
            archivosEnDirectorioRecursiva(path, archivos, dirs);
        }
    }
    closedir(dir);
    return i;
}

The expected output is : /home/reflejo/Escritorio/Ejercicio2/documentos/Codegolf.pdf
The output obtained is : / home / reflex / Desktop /Exercise2/Codegolf.pdf
Attached image of the structure of the directory.

Thanks in advance !!

    
asked by Juanjoo Tocino 04.11.2018 в 16:11
source

0 answers