main.c

来自「用来在Linux下遍历目录」· C语言 代码 · 共 49 行

C
49
字号
//#######################################################################
//#  To test the searchdir function , by fred.zeng
//#	 using stat funciton define in sys/stat.h
//#######################################################################
#include "searchdir.h"
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

// getstat() function is used to get the infomation of the file by name
struct stat * getstat(char * const filename)
{		struct stat * ststat = alloca(sizeof(struct stat)) ;
		stat(filename,ststat);		
		return ststat;
}

// pf() is a callback function who will be invoked by listdir to process 
// the displaying work of every filename and directory
void pf(char * const filename,int file_type)
{
	//get the pointer to the information of the file by name
	struct stat * pstat = getstat(filename);
	
	if (file_type==T_FILE)
	{	
		
		//display the filename and size
		//printf("[%ld]",pstat->st_mode&S_IFMT);
		printf("%s [File] Size = %ld byte\n",filename,pstat->st_size);
		
	}
	else
	{
		//printf("[%ld]",pstat->st_mode&S_IFMT);
		printf("%s [DIR] \n",filename);
	}
}

// The main() function
int main(int argc, char *const argv [])
{
if (argc==1) // no parameter means path=./
listdir("./",pf);
else
listdir(argv[1],pf);
return 1;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?