2018-10-24 07:48:24 +00:00
|
|
|
#include <LibC/stdio.h>
|
|
|
|
#include <LibC/unistd.h>
|
2018-10-24 10:43:52 +00:00
|
|
|
#include <LibC/dirent.h>
|
2018-10-27 12:56:52 +00:00
|
|
|
#include <LibC/errno.h>
|
|
|
|
#include <LibC/string.h>
|
2018-10-24 07:48:24 +00:00
|
|
|
|
|
|
|
int main(int c, char** v)
|
|
|
|
{
|
2018-10-26 12:24:11 +00:00
|
|
|
DIR* dirp = opendir(".");
|
2018-10-24 10:43:52 +00:00
|
|
|
if (!dirp) {
|
2018-10-27 12:56:52 +00:00
|
|
|
perror("opendir failed");
|
2018-10-24 07:48:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-10-24 11:19:36 +00:00
|
|
|
char pathbuf[256];
|
2018-10-24 10:43:52 +00:00
|
|
|
while (auto* de = readdir(dirp)) {
|
2018-10-26 12:24:11 +00:00
|
|
|
sprintf(pathbuf, "%s", de->d_name);
|
2018-10-27 12:56:52 +00:00
|
|
|
|
2018-10-24 11:19:36 +00:00
|
|
|
stat st;
|
|
|
|
int rc = lstat(pathbuf, &st);
|
|
|
|
if (rc == -1) {
|
2018-10-27 12:56:52 +00:00
|
|
|
printf("lstat(%s) failed: %s\n", pathbuf, strerror(errno));
|
2018-10-24 11:19:36 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2018-10-24 07:48:24 +00:00
|
|
|
|
2018-10-24 11:19:36 +00:00
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
printf("d");
|
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
printf("l");
|
|
|
|
else if (S_ISBLK(st.st_mode))
|
|
|
|
printf("b");
|
|
|
|
else if (S_ISCHR(st.st_mode))
|
|
|
|
printf("c");
|
|
|
|
else if (S_ISFIFO(st.st_mode))
|
|
|
|
printf("f");
|
|
|
|
else if (S_ISREG(st.st_mode))
|
|
|
|
printf("-");
|
|
|
|
else
|
|
|
|
printf("?");
|
|
|
|
|
|
|
|
printf("%c%c%c%c%c%c%c%c",
|
|
|
|
st.st_mode & S_IRUSR ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWUSR ? 'w' : '-',
|
|
|
|
st.st_mode & S_IXUSR ? 'x' : '-',
|
|
|
|
st.st_mode & S_IRGRP ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWGRP ? 'w' : '-',
|
|
|
|
st.st_mode & S_IXGRP ? 'x' : '-',
|
|
|
|
st.st_mode & S_IROTH ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWOTH ? 'w' : '-'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
printf("t");
|
|
|
|
else
|
|
|
|
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
|
|
|
|
|
|
|
printf(" i:%x ", de->d_ino);
|
|
|
|
printf(" %x ", st.st_size);
|
|
|
|
printf("%s%c", de->d_name, S_ISDIR(st.st_mode) ? '/' : ' ');
|
|
|
|
printf("\n");
|
2018-10-24 10:43:52 +00:00
|
|
|
}
|
2018-10-24 07:48:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|