首先感谢您的宝贵时间,然后,我的问题是我正在尝试使用最大的标志(例如(ls -laRt ...))来实现ls命令。我了解stat结构以及所有时间/ ctime / strftime功能,但是当我想在输出中打印它们时,它们给了我很大的数字,所以我找到了如何用ctime转换st_mtime的时间戳,但我希望结果在“像(ls -lRt)格式这样的“ Monday Day Hour:Minutes”格式,而无需使用strftime,是否可能或者我也必须实现此功能?第二个问题是,对于我的硬链接数量,我如何将输出中的结果打印内容如“ -77452048”转换为“真实”结果?
这是我的主要内容,我是一个乞讨人,所以我向所有批评家开放,谢谢U!
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <err.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <stdint.h>
int main (int argc, char **argv)
{
DIR *dir;
struct dirent *d;
struct stat s;
char chemin[256];
struct passwd *p;
p = getpwuid(geteuid());
struct group *grp;
grp = getgrgid(getgrnam("."));
if (argc != 2)
{
perror("Filename\n");
exit(-1);
}
if ((dir=opendir(argv[1])) == NULL)
{
perror("opendir error");
exit(-1);
}
while ((d = readdir(dir)) != 0)
{
if(!strcmp (d->d_name, "."))
{
continue;
}
if(!strcmp (d->d_name, ".."))
{
continue;
}
strcpy (chemin, argv[1]);
strcat (chemin, "/");
strcat (chemin, d->d_name);
if (!stat(chemin, &s))
{
printf((S_ISDIR(s.st_mode)) ? "d" : "-");
printf((s.st_mode & S_IRUSR) ? "r" : "-");
printf((s.st_mode & S_IWUSR) ? "w" : "-");
printf((s.st_mode & S_IXUSR) ? "x" : "-");
printf((s.st_mode & S_IRGRP) ? "r" : "-");
printf((s.st_mode & S_IWGRP) ? "w" : "-");
printf((s.st_mode & S_IXGRP) ? "x" : "-");
printf((s.st_mode & S_IROTH) ? "r" : "-");
printf((s.st_mode & S_IWOTH) ? "w" : "-");
printf((s.st_mode & S_IXOTH) ? "x" : "-");
} else
{
perror("Erreur dans stat");
}
printf(" ");
printf("%d", &s.st_nlink);
printf(" ");
printf("%s", p->pw_name);
printf(" ");
printf("%s", grp->gr_name);
printf(" ");
printf("%d", &s.st_mtime);
printf(" ");
printf("%9jd", (intmax_t)s.st_size);
printf(" ");
printf("%s ", d->d_name);
printf("\n");
}
closedir(dir);
}