代码: 全选
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
DIR *a=opendir(argv[1]);
struct dirent *b=NULL;
struct stat* c=malloc(sizeof(struct stat));
struct passwd *d=NULL;
struct group *e=NULL;
while(1)
{
b=readdir(a);
if(!b)
break;
stat(b->d_name,c);
if(S_ISSOCK(c->st_mode))
printf("socker file");
else if(S_ISLNK(c->st_mode))
printf("symbolic link");
else if(S_ISREG(c->st_mode))
printf("regular file");
else if(S_ISBLK(c->st_mode))
printf("block device");
else if(S_ISDIR(c->st_mode))
printf("directory");
printf(" ");
if(c->st_mode & S_IRUSR)
printf("r");
else
printf("-");
if(c->st_mode & S_IWUSR)
printf("w");
else
printf("-");
if(c->st_mode & S_IXUSR)
printf("x");
else
printf("-");
if(c->st_mode & S_IRGRP)
printf("r");
else
printf("-");
if(c->st_mode & S_IWGRP)
printf("w");
else
printf("-");
if(c->st_mode & S_IXGRP)
printf("x");
else
printf("-");
if(c->st_mode & S_IROTH)
printf("r");
else
printf("-");
if(c->st_mode & S_IWOTH)
printf("w");
else
printf("-");
if(c->st_mode & S_IXOTH)
printf("x");
else
printf("-");
printf(" ");
d=getpwuid(c->st_uid);
printf("%s ",d->pw_name);
e=getgrgid(c->st_gid);
printf("%s ",e->gr_name);
printf("%s %s",c->st_size,b->d_name);
printf("\n");
}
}