0

Sorry for this totally amateur question, but I can't understand one difference between the native Linux stat program and the stat function in C

The point is, when I use a stat program in the example directory: "/ usr" its modification time is as follows:

  File: /usr
Access: 2022-01-08 17:28:57.400521375 -0600
Modify: 2021-12-22 15:45:08.584831000 -0600
Change: 2021-12-22 15:45:08.584831000 -0600


But when I execute the following code

int getDirContent(){

    DIR *dir;
    struct dirent *entry;
    struct stat pFilestat;
    if ((dir = opendir("/")) == NULL)
        perror("opendir() error");
    else {
        puts("contents of root:");
    
    /* Read directory entries */
    while ((entry = readdir(dir)) != NULL){
         /* Extract Filename */
        stat(entry->d_name, &pFilestat);
        printf("%s\n", entry->d_name);

        /* Extract Create time */
        if (S_ISDIR(pFilestat.st_mode)){
            printf("Access: %s",ctime(&pFilestat.st_atime));
            printf("Modify: %s",ctime(&pFilestat.st_mtime));
            printf("Change: %s",ctime(&pFilestat.st_ctime));


        }
        }

    closedir(dir);
  }
}

the result is as follows

contents of root:
[...]
usr
Access: Sun Jan  9 16:08:20 2022
Modify: Sun Jan  9 16:09:10 2022
Change: Sun Jan  9 16:09:10 2022

My system:

OS: Linux debian x86_64 GNU/Linux
compiler version:
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6)

[user@debia]:~$ timedatectl
               Local time: Sun 2022-01-09 16:05:35 CET
           Universal time: Sun 2022-01-09 15:05:35 UTC
                 RTC time: Sun 2022-01-09 15:05:36
                Time zone: Europe/(CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

if the question has already been asked, please send me a link. Unfortunately, I tried to find the answer, but I can't ask a good question on google

EDIT:

Using of getDirContent: main.c

include <string.h>
#include <stdio.h>

#include "header/getDirContent.h"

int main() {


    const char * pPath_array[] = {"/etc","/home", NULL}; // array of pointers
    /*
    For the sake of simplicity, the code removes the possibility for functions to accept arguments in the form of an array. Everything happens from inside the getDirContent function*/

    getDirContent();
    
    return 0;
}

wants to go through the directories indicated in the pointer table

reg3x_mr
  • 75
  • 1
  • 10

1 Answers1

1

Maybe the problem is that stat is not acessing the same files readdir is...

The value in entry->d_name does not include the directory nor it is the full path to the entry. It is just the file name (e.g: "url", not "/url").

You could try and concatenate the directory ("/") with the entry->d_name like this:

char fullFilepath[PATH_MAX + 1];
snprintf(fullFilepath, PATH_MAX, "/%s", entry->d_name);

/* Extract Filename */
stat(fullFilepath, &pFilestat);
printf("%s\n", fullFilepath);
  • `strncat(fullFilepath, entry->d_name, 128);` is terribly wrong. You could use `snprintf()` instead. – wildplasser Jan 09 '22 at 17:08
  • 1
    Using full path is the correct solution. But, you're using a "magic number" of 128 everywhere. And, I'd use (e.g.) 1024 instead (e.g.) `PATH_MAX`. No need for `calloc`, just do: `char fullFilepath[PATH_MAX + 1];` – Craig Estey Jan 09 '22 at 17:11
  • Changed the code according to the comments, thanks :) – João Fukuda Jan 09 '22 at 17:21
  • to be honest, I thought about the same thing too, because when I look at what readdir () reads, I don't know why, but cuts off the "/" sign – reg3x_mr Jan 09 '22 at 18:39
  • Yes, that was exactly that! Thank you very much for help !!!!:) – reg3x_mr Jan 09 '22 at 19:45