各位程序员在自己的虚拟机里一定没少执行过“ls”这个功能吧,这个代码就是实现了ls和ls-l功能,话不多说,上代码。
实现代码
int process_ls(char * path)
{
DIR * dirp;
struct dirent *direntp ;
dirp = opendir(path);
if(dirp == NULL)
{
perror("opendir");
exit(-1);
}
while( (direntp=readdir(dirp)) != NULL)
{
if(strncmp(direntp->d_name,".",1) == 0) continue;
printf("%s ",direntp->d_name);
}
printf("\n");
closedir(dirp);
return 0 ;
}
这部分代码就已经实现了ls的功能。
int process_ls_l(char *path)
{
DIR * dirp;
struct dirent *direntp ;
char filename[100] ={0};
struct stat st;
int ret;
char buf[100] ={0};
char tmp[100] ={0};
dirp = opendir(path);
if(dirp == NULL)
{
perror("opendir");
exit(-1);
}
while( (direntp=readdir(dirp)) != NULL)
{
if(strncmp(direntp->d_name,".",1) == 0) continue;
strcpy(filename,path);