#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <time.h>
int main(int argc, char *argv[])
{
if(argc < 2){
printf("please enter %s filename\n", argv[0]);
return -1;
}
struct stat statbuf;
if(-1 == lstat(argv[1], &statbuf)){
perror("lstat");
return -1;
}
//printf("size: %ld\n", statbuf.st_size);
switch(statbuf.st_mode & S_IFMT){
case S_IFSOCK:
printf("s");
break;
case S_IFLNK:
printf("l");
break;
case S_IFREG:
printf("-");
break;
case S_IFBLK:
printf("b");
break;
case S_IFDIR:
printf("d");
break;
case S_IFCHR:
printf("c");
break;
case S_IFIFO:
printf("p");
break;
}
/*if(S_ISLNK(statbuf.st_mode)){
printf("--> l");
}*/
int n = 8;
for(; n > 0; n--){
if(statbuf.st_mode &(1 << n)){ //判断权限位的低9位 0-8
switch(n % 3){
case 2:
printf("r");
break;
case 1:
printf("w");
break;
case 0:
printf("x");
break;
}
}else{
printf("-");
}
}
printf(" %ld", statbuf.st_nlink);
struct passwd * uidp = getpwuid(statbuf.st_uid);
printf(" %s\n" ,uidp->pw_name);
struct group * uidp = getgrid(statbuf.st_uid);
printf(" %s\n" ,uidp->gr_name);
printf(" %6ld\n" ,uidp->st_size);
struct tm *b=localtime(&statbuf.st_mtime);
printf(" %d-%d-%d %d:%d:%d\n",b->tm_year+1900,b->tm_mon+1,b->tm_mday,b->tm_hour,b->tm_min,b->tm_sec);
return 0;
}