#include "stdio.h"
#include
#include
#include
#include
#include
#include
int main()
{
DIR *dir;
struct dirent *ptr;
struct stat buffer;
int i;
char gid_num[20];
char uid_num[20];
char str[1];
char buf[80];
getcwd(buf,sizeof(buf));
stat(buf,&buffer);
dir=opendir(buf);
while((ptr=readdir(dir))!=NULL)
{
if(strcmp(ptr->d_name,"A")>=0)
{
switch (buffer.st_mode & S_IFMT) {
case S_IFBLK: printf("block device"); break;
case S_IFCHR: printf("character device"); break;
case S_IFDIR: printf("directory"); break;
case S_IFIFO: printf("FIFO/pipe"); break;
case S_IFLNK: printf("symlink"); break;
case S_IFREG: printf("regular file"); break;
case S_IFSOCK: printf("socket"); break;
default: printf("unknown?"); break;
}
printf("%s",str);
printf(" %d",buffer.st_ino);
printf(" %d",buffer.st_uid);
printf(" %d",buffer.st_gid);
printf(" %d",buffer.st_size);
printf(" %s",ctime(&buffer.st_mtime));
printf(" %s\n",ptr->d_name);
}
}
closedir(dir);
return 0;
}
显示结果如下:
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls1.c
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls1
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls3
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls2
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls2.c
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
ls3.c
directory 2014013 500 500 4096 Thu May 10 19:35:14 2012
Desktop
为什么总是显示directory呢?怎么能把gid与uid变成字符串呢比如是root形式?