list files

#include <apue.h>
#include <dirent.h>
int main( int argc, char *argv[] )
{
	DIR* dp,*dp2;
	char* argv2[2];
	char buff[512];
	struct dirent *dirp;
	if(argc!=2)
	{
	   printf( "usage:ls directory_name\n" );
	   return 0;
	}
	if((dp=opendir(argv[1]))==NULL)
	{
	   printf("can't open %s",argv[1]);
	   return 0;
	}
	while((dirp=readdir(dp))!=NULL)
	{
		if(strcmp(".",dirp->d_name)==0 || strcmp("..",dirp->d_name)==0)
		   continue;
		sprintf(buff,"%s/%s",argv[1],dirp->d_name);
		if((dp2=opendir(buff))!=NULL)
		{
			closedir(dp2);
			argv2[0]=NULL;
			argv2[1]=buff;
			
			main(2,argv2);
		}
		else
		  printf("%s\n",buff);
	}
	closedir(dp);
	return 0;
}

你可能感兴趣的:(list files)