int main(int argc,char*argv[])
{
ARGS_CHECK(argc,2);
DIR *dp=opendir(argv[1]);
ERROR_CHECK(dp,NULL,"opendir");
if(strcmp(argv[1],".")!=0){
chdir(argv[1]);
}
struct stat pstat;
struct dirent *pdirent;
struct tm*ptm;
while((pdirent=readdir(dp))!=NULL){
char type[11]={0};
switch(pdirent->d_type){
case DT_BLK:type[0]='b';
break;
case DT_CHR:type[0]='c';
break;
case DT_REG:type[0]='-';
break;
case DT_DIR:type[0]='d';
break;
case DT_LNK:type[0]='l';
}
int ret =stat(pdirent->d_name,
ERROR_CHECK(ret,-1,"stat");
int mode=pstat.st_mode;
int k=9;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if((mode&1)&&(k>0)){
switch(j){
case 0:type[k--]='x';
break;
case 1:type[k--]='w';
break;
case 2:type[k--]='r';
break;
}
}
else{
type[k--]='-';
}
mode>>=1;
}
}
ptm=gmtime(&pstat.st_mtime);
printf("%s %ld %s %s %5ld %d月 %3d %2d:%2d %s\n",type,pstat.st_nlink,
getpwuid(pstat.st_uid)->pw_name,getgrgid(pstat.st_gid)->gr_name,
pstat.st_size,ptm->tm_mon+1,ptm->tm_mday,(8+ptm->tm_hour)%24,ptm->tm_min,
pdirent->d_name);
}
return 0;
}