You are on page 1of 3

#include<grp.h> #include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<pwd.h> #include<dirent.h> #include<stdlib.h> #include<time.

h> main() { DIR *d; struct dirent *fd; struct stat a; int s; d=opendir("."); fd=(struct dirent*)malloc(sizeof(struct dirent)); fd=readdir(d); while(fd!=NULL) { if((strcmp(fd->d_name,".")!=0) && (strcmp(fd->d_name,"..")!=0)) { stat(fd->d_name,&a); distype(a); disperm(a); printf("%d\t",a.st_nlink); dispuser(a); printf("%d\t",a.st_size); disptime(a); printf(" %s",fd->d_name); } fd=readdir(d);printf("\n"); } } int distype(struct stat a) { if(S_ISDIR(a.st_mode)) printf("d"); else if(S_ISBLK(a.st_mode)) printf("b"); else if(S_ISLNK(a.st_mode)) printf("l"); else if(S_ISFIFO(a.st_mode)) printf("f"); } int disperm(struct stat a) { if(S_IRUSR & a.st_mode) printf("r"); else printf("-"); if(S_IWUSR & a.st_mode) printf("w");

else printf("-"); if(S_IXUSR & a.st_mode) printf("x"); else printf("-"); if(S_IROTH & a.st_mode) printf("r"); else printf("-"); if(S_IWOTH & a.st_mode) printf("w"); else printf("-"); if(S_IXOTH & a.st_mode) printf("x"); else printf("-"); if(S_IRGRP & a.st_mode) printf("r"); else printf("-"); if(S_IWGRP & a.st_mode) printf("w"); else printf("-"); if(S_IXGRP & a.st_mode) printf("x"); else printf("-"); } int dispuser(struct stat a) { struct passwd *p; struct group *g; p=(struct passwd*)malloc(sizeof(struct passwd)); g=(struct group*)malloc(sizeof(struct group)); p=getpwuid(a.st_uid); g=getgrgid(a.st_gid); printf("%s\t",p->pw_name); printf("%s\t",g->gr_name); } int disptime(struct stat a) { struct tm *ti; ti=(struct tm*)malloc(sizeof(struct tm)); ti=localtime(&a.st_ctime); printf("%d:",ti->tm_hour); printf("%d:",ti->tm_min); printf("%d ",ti->tm_sec); printf("%d ",ti->tm_mday); printf("%d ",ti->tm_mon); printf("%d ",(ti->tm_year)+1900);

// t=ctime(&a->ti_ctime); //for(i=4;i<16;i++) //{ // printf("%c",t[i]); // printf("\t"); // } }

You might also like