//#include "../util.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <stdarg.h>
#include <wait.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
// properties linknum user group cap date-time name
// 总用量 120K
// -rwxrwxr-x 1 hqyj hqyj 17K 4月 9 16:43 a.out
// drwxrwxr-x 2 hqyj hqyj 4.0K 4月 9 17:11 dkdkd
// -rw-rw-r-- 1 hqyj hqyj 51 4月 9 17:09 dls.c
// -rw-rw-r-- 1 hqyj hqyj 27K 4月 9 16:48 his.txt
// -rw-rw-r-- 1 hqyj hqyj 15K 4月 9 15:00 libstatic0.a
// -rw-rw-r-- 1 hqyj hqyj 56 4月 9 14:55 libstatic0.c
// -rw-rw-r-- 1 hqyj hqyj 1.7K 4月 9 14:59 libstatic0.o
// -rwxrwxr-x 1 hqyj hqyj 22K 4月 9 16:37 libstatic0.so
// -rw-rw-r-- 1 hqyj hqyj 103 4月 9 14:58 test_static0.c
// -rw-rw-r-- 1 hqyj hqyj 12K 4月 9 14:59 util.o
char r(__mode_t m, __mode_t i)
{
return m & i ? 'r' : '-';
}
char w(__mode_t m, __mode_t i)
{
return m & i ? 'w' : '-';
}
char x(__mode_t m, __mode_t i)
{
return m & i ? 'x' : '-';
}
void date_to_str(time_t tt, char d[])
{
struct tm *t = localtime(&tt);
sprintf(d, "%d/%02d/%02d %02d:%02d:%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
}
int main(int c, char *a[])
{
if (c < 2)
{
perror("args less than 2");
return -1;
}
char *dir = a[1];
DIR *d = opendir(dir);
if (!d)
{
perror("opendir error");
return -2;
}
char pathname[4096] = {};
struct dirent *dt = readdir(d);
struct stat st;
do
{
char *name = dt->d_name;
if (name[0] != '.')
{
strcpy(pathname, dir);
pathname[strlen(dir)] = '/';
pathname[strlen(dir) + 1] = '\0';
strcat(pathname, name);
char permissions[11] = {};
int i = 0;
byte type = dt->d_type;
switch (type)
{
case DT_DIR:
permissions[i++] = 'd';
break;
case DT_REG:
permissions[i++] = '-';
break;
default:
break;
}
lstat(pathname, &st);
__mode_t mode = st.st_mode;
permissions[i++] = r(mode, S_IRUSR);
permissions[i++] = w(mode, S_IWUSR);
permissions[i++] = x(mode, S_IXUSR);
permissions[i++] = r(mode, S_IRGRP);
permissions[i++] = w(mode, S_IWGRP);
permissions[i++] = x(mode, S_IXGRP);
permissions[i++] = r(mode, S_IROTH);
permissions[i++] = w(mode, S_IWOTH);
permissions[i++] = x(mode, S_IXOTH);
char date[20] = {};
date_to_str(st.st_mtime, date);
printf("%s\t%ld\t%s\t%s\t%ld\t%s\t%s\n", permissions, st.st_nlink, getpwuid(st.st_uid)->pw_name, getgrgid(st.st_gid)->gr_name, st.st_size, date, name);
}
} while (dt = readdir(d));
p_i(closedir(d));
return 0;
}
//result below
// 55@ubuntu:~/Desktop$ ./a.out .
// -rw-rw-r-- 1 hqyj hqyj 14538 2025/04/09 15:00:02 libstatic0.a
// -rw-rw-r-- 1 hqyj hqyj 27171 2025/04/09 16:48:41 his.txt
// -rw-rw-r-- 1 hqyj hqyj 103 2025/04/09 14:58:07 test_static0.c
// -rw-rw-r-- 1 hqyj hqyj 56 2025/04/09 14:55:57 libstatic0.c
// -rw-rw-r-- 1 hqyj hqyj 1680 2025/04/09 14:59:50 libstatic0.o
// -rwxrwxr-x 1 hqyj hqyj 22488 2025/04/09 16:37:28 libstatic0.so
// -rw-rw-r-- 1 hqyj hqyj 2841 2025/04/09 19:03:17 dls.c
// drwxrwxr-x 2 hqyj hqyj 4096 2025/04/09 17:11:20 dkdkd
// -rw-rw-r-- 1 hqyj hqyj 11976 2025/04/09 14:59:50 util.o
// -rwxrwxr-x 1 hqyj hqyj 23488 2025/04/09 19:03:24 a.out
// 0
55@ubuntu:~/Desktop$ ./a.out .
-rw-rw-r-- 1 hqyj hqyj 14538 2025/04/09 15:00:02 libstatic0.a
-rw-rw-r-- 1 hqyj hqyj 27171 2025/04/09 16:48:41 his.txt
-rw-rw-r-- 1 hqyj hqyj 103 2025/04/09 14:58:07 test_static0.c
-rw-rw-r-- 1 hqyj hqyj 56 2025/04/09 14:55:57 libstatic0.c
-rw-rw-r-- 1 hqyj hqyj 1680 2025/04/09 14:59:50 libstatic0.o
-rwxrwxr-x 1 hqyj hqyj 22488 2025/04/09 16:37:28 libstatic0.so
-rw-rw-r-- 1 hqyj hqyj 2841 2025/04/09 19:03:17 dls.c
drwxrwxr-x 2 hqyj hqyj 4096 2025/04/09 17:11:20 dkdkd
-rw-rw-r-- 1 hqyj hqyj 11976 2025/04/09 14:59:50 util.o
-rwxrwxr-x 1 hqyj hqyj 23488 2025/04/09 19:03:24 a.out
0