#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <dirent.h>
#include <dirent.h>
#include <dirent.h>
char gettype(mode_t mode)
{
switch(S_IFMT & mode)
{
case S_IFREG: return '-';
case S_IFDIR: return 'd';
case S_IFLNK: return 'l';
case S_IFSOCK: return 's';
case S_IFCHR: return 'c';
case S_IFBLK: return 'b';
case S_IFIFO: return 'f';
}
}
char *getperm(mode_t mode)
{
static char buf[10] = {0};
int i = 9;
while(i--)
{
if(mode & 1<<i)
{
switch(i%3)
{
case 0: buf[i] = 'r';break;
case 1: buf[i] = 'w';break;
case 2: buf[i] = 'x';break;
}
}
else
buf[8-i] = '-';
}
return buf;
}
char *myctime(time_t *t)
{
static char buf[100000] = {0};
strncpy(buf, ctime(t), 20);
return buf;
}
int main(int argc, char **argv)
{
DIR *dp = opendir(".");
struct dirent *d;
while(d = readdir(dp) )
{
if('.' == d->d_name[0])
continue;
struct stat s;
int ret = stat(d->d_name, &s);
if(-1 == ret)
{
perror("stat");
return -1;
}
printf("%c%s %d %s %s %6ld %s \033[31m %s \033[0m\n",
gettype(s.st_mode),//type
getperm(s.st_mode),//prem
s.st_nlink,//nlink
getpwuid(s.st_uid)->pw_name,//user
getgrgid(s.st_gid)->gr_name,//groupuser
s.st_size,//size
myctime(&s.st_ctime),//time
d->d_name);//name
}
}