#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
char *get_file(mode_t m, char name[])
{
char per[] = "rwx";
for (int i = 0; i < 9; i++)
{
if (m & (0400 >> i))
{
// if (0 == i % 3)
// putchar('r');
// else if (1 == i % 3)
// putchar('w');
// else if (2 == i % 3)
// putchar('x');
name[i] = per[(i % 3)];
}
else
{
name[i] = '-';
}
}
return name;
}
int main(int argc, const char *argv[])
{
struct stat buf;
char static name[10] = "";
if (stat(argv[1], &buf) < 0)
{
perror("stat");
return -1;
}
struct tm *info = localtime(&buf.st_mtime);
get_file(buf.st_mode, name);
printf("%s\n", name);
printf("mode:%o\nlink:%ld\nuid:%d\ngid:%d\nsize:%ld\ntime:%ld\ntime:%d-%02d-%02d %02d:%02d:%02d\n",
buf.st_mode, buf.st_nlink, buf.st_uid, buf.st_gid, buf.st_size, buf.st_mtime,
info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min, info->tm_sec);
return 0;
}
