http://dev.csdn.net/article/36/36598.shtm 之内容增强板
解决http://dev.csdn.net/article/36/36598.shtm 之中的问题,能够显示不同目录下的所有文件信息,不受到执行文件路径的限制。
/*
函数名: dir
功 能: dir命令
用 法: int dir(char *str1);
程序例:
*/
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
static int get_info(const char * filename,const char * path)
{
/* printf("%s/n",filename);*/
char fullname[255];
struct stat statbuf;
if(!strcmp(filename,".")||!strcmp(filename,".."))return(0);
strcpy(fullname,path);
strcat(fullname,"/");
strcat(fullname,filename);
if(lstat(fullname,&statbuf)==-1)
{
printf("%s ,errno %d,%s/n",fullname,errno,strerror(errno ));
return(1);
}
if(S_ISDIR(statbuf.st_mode))
printf("%s/t Directory/tmodified at %s",fullname,ctime(&statbuf.st_mtime));
if(S_ISREG(statbuf.st_mode))
printf("%s/tsize:%ld bytes/tmodified at %s",fullname,statbuf.st_size,ctime(&statbuf.st_mtime));
return(0);
}
int main(int argc, char *argv[])
{
DIR * dirp;
struct dirent * direntp;
char* path = "/home/zhuhj";
if(argc>1)
{
path = argv[1];
printf("file name is %s/n",path);
}
if((dirp=opendir(path)))
{
while((direntp=readdir(dirp))!=NULL)
get_info(direntp->d_name,path);
/* printf("%s/t",direntp->d_name);*/
closedir(dirp);
exit(1);
}
else
{
printf("Error/n");
exit(1);
}
return(0);
}