[英]Why stat funcion in Linux return different result than stat function in C
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抱歉这个完全业余的问题,但我无法理解原生 Linux stat 程序和 C 中的 stat function 之间的区别
The point is, when I use a stat program in the example directory: "/ usr" its modification time is as follows:重点是,当我使用示例目录中的stat程序时:“/usr”它的修改时间如下:
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
不幸的是,我试图找到答案,但我无法在 google 上提出一个好的问题
EDIT:编辑:
Using of getDirContent: main.c getDirContent的使用:main.c
include <string.h>
#include <stdio.h>
#include "header/integrity_monitoring.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想通过指针表中指示的目录 go
Maybe the problem is that stat
is not acessing the same files readdir
is...也许问题是
stat
没有访问相同的文件readdir
是......
The value in entry->d_name
does not include the directory nor it is the full path to the entry. entry->d_name
中的值不包括目录,也不是条目的完整路径。 It is just the file name (eg: "url"
, not "/url"
).它只是文件名(例如:
"url"
,而不是"/url"
)。
You could try and concatenate the directory ( "/"
) with the entry->d_name
like this:您可以尝试将目录(
"/"
)与entry->d_name
连接起来,如下所示:
char fullFilepath[PATH_MAX + 1];
snprintf(fullFilepath, PATH_MAX, "/%s", entry->d_name);
/* Extract Filename */
stat(fullFilepath, &pFilestat);
printf("%s\n", fullFilepath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.