C - stat struct not working properly

1.2k views Asked by At

I am trying to printf some characteristics of the files inside a directory (only the ones that start with lowercase). However, when I execute the following code, some of them work and some don't. It looks like the stat struct may not be working properly, but I haven't been able to identify the error so far.

struct dirent *ep;
DIR *dp;    
char* cwd;
char buff[PATH_MAX + 1];
off_t tamTotal=0;
struct stat archivo;    
char path[]="/home/edu/Escritorio/P7/practica7/prueba";

if(!(dp=opendir(path))){
    printf("Error.\n");
    exit(-1);
}

printf("\nFILES:\n");
while (ep = readdir (dp)){ 
    stat(ep->d_name, &archivo);
    if(S_ISREG(archivo.st_mode)){
        if(!isupper(ep->d_name[0])){
            printf("  %s\n",ep->d_name);
            printf("    Last modification date: %s \n",ctime(&archivo.st_mtime));
            printf("    i-no number: %lu \n",archivo.st_ino);
            printf("    Blocks: %lu \n",archivo.st_blocks);
            printf("    Size: %lu \n",archivo.st_size);
        }       
    }
}   

OUTPUT:

enter image description here

1

There are 1 answers

1
Paul Ogilvie On BEST ANSWER

Most likely the dirent structure returned by readdir only contains the name and not the directory or full path.

As a result, stat will fail, which you don't notice because you don't check its return value.