问题描述:
本文使用C语言实现了简单的文件遍历搜索功能并将搜索结果排序输出(测试平台为Ubuntu),本程序接受以下格式输入:
输入
search.o [-n NAME] [-L] path
- path: 搜索开始的根地址,可以为相对或者绝对路径
- -n NAME: 被搜索文件的文件名模板,程序只输出文件名符合该模板的文件名,这个模板和shell风格的通配符一致:
- *: 匹配任何文件,没有-n name参数时,默认仅有一个*号,即输出该路径下所有文件
- ?: 匹配任何单字符
- [seq]: 匹配包含seq字符串的任何文件名
- [!seq]: 匹配不包含seq字符串的任何文件名
- -L: 符号链接,如果有该参数,则符号链接也考虑在内,即程序会进入符号链接的文件下继续递归搜索
输出
- 一行输出一个搜索结果
- 在每一行,打印的结果以以下格式要求输出:
- Mode: 一个九个字符的字符串,代表该文件的访问权限,实现的功能类似于命令: ls -l filename | cut -b 2-10 .
- Size: the size of the file in a decimal number in bytes.一个十进制的数,代表该文件的大小(以字节计算)
- Extended file name:完整的文件名,包含文件路径和文件名
- 输出结果按照文件名的ASCII码表排序输出
源码:search.c
//Written by Openking 2014-12-31
/*usage: search.o [-h] [-n NAME] [-L] path
positional arguments:
path Path
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME File name pattern
-L, --followlinks Follow symbolic links*/
#include <locale.h>
#include <fnmatch.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX_PATH_LENGTH 512
//global variables
char*name ="*";
bool Have_n = 0, Have_L = 0;
char dir_Path[10000][MAX_PATH_LENGTH];
char output[10000][MAX_PATH_LENGTH+20];
int num = 0;
void sort(char path1[][MAX_PATH_LENGTH],char path2[][MAX_PATH_LENGTH+20],int n)
{
char temp1[MAX_PATH_LENGTH],temp2[MAX_PATH_LENGTH+20];
int i,j,k;
for(i = 0; i < n; i++)
{
k = i;
for(j = i + 1; j < n; j++)
if(strcmp(path1[k],path1[j]) > 0)
k = j;
if(k != i)
{
strcpy(temp1,path1[i]);
strcpy(temp2,path2[i]);
strcpy(path1[i],path1[k]);
strcpy(path2[i],path2[k]);
strcpy(path1[k],temp1);
strcpy(path2[k],temp2);
}
}
}
void doSearch(char *path)
{
DIR *ptr_dir;
struct dirent *dir_entry;
int i = 0;
char *child_path;
char *file_path;
child_path = (char*)malloc(sizeof(char)*MAX_PATH_LENGTH);
memset(child_path, 0, sizeof(char)*MAX_PATH_LENGTH);
file_path = (char*)malloc(sizeof(char)*MAX_PATH_LENGTH);
memset(file_path, 0, sizeof(char)*MAX_PATH_LENGTH);
ptr_dir = opendir(path);
while((dir_entry = readdir(ptr_dir)) != NULL)
{
if(dir_entry->d_type == DT_DIR) //directory
{
if(strcmp(dir_entry->d_name,".") == 0 ||
strcmp(dir_entry->d_name,"..") == 0)
continue;
sprintf(child_path, "%s/%s", path, dir_entry->d_name);
//printf("[DIR]%s\n", child_path);
doSearch(child_path);
continue;
}
else if(dir_entry->d_type == DT_LNK) //symbol link
{
sprintf(child_path, "%s/%s", path, dir_entry->d_name);
//printf("[Link]%s\n", child_path);
//printf("dir:%d lnk:%d reg:%d\n",DT_DIR,DT_LNK,DT_REG);
struct stat sb;
stat(child_path, &sb);
if((sb.st_mode & S_IFMT) == S_IFDIR)
if(Have_L)
doSearch(child_path);
else continue;
else goto L1;
}
else if(dir_entry->d_type == DT_REG)
{
L1:
if(fnmatch(name, dir_entry->d_name, FNM_PATHNAME|FNM_PERIOD) == 0)
{
sprintf(file_path, "%s/%s", path, dir_entry->d_name);
//printf("[FILE]%s\n", file_path);
sprintf(dir_Path[num], "%s",file_path);
struct stat file;
char power[] = "---------";
int size_file = 0,mode = 0;
lstat(file_path,&file);
size_file = file.st_size;
mode = file.st_mode;
if(mode & S_IRUSR) power[0] = 'r';
if(mode & S_IWUSR) power[1] = 'w';
if(mode & S_IXUSR) power[2] = 'x';
if(mode & S_IRGRP) power[3] = 'r';
if(mode & S_IWGRP) power[4] = 'w';
if(mode & S_IXGRP) power[5] = 'x';
if(mode & S_IROTH) power[6] = 'r';
if(mode & S_IWOTH) power[7] = 'w';
if(mode & S_IXOTH) power[8] = 'x';
sprintf(output[num++], "%s %d %s",power,size_file,file_path);
}
}
}
free(child_path);
child_path = NULL;
free(file_path);
file_path = NULL;
}
int main(int argc, char*argv[], char *env[])
{
int i,j;
static char str1[] = "-n", str2[] = "-L";
char*dir;
for(i = 1; i < argc; i++) // Deal the strings
{
//printf("argv[%d]:%s\n", i, argv[i]); //Output the command parameter
if(strcmp(argv[i], str1) == 0)
{
Have_n = 1;
name = argv[i+1]; //if have -n ,then record the name
i++; //skip the next name
//printf("name: %s\n",name);
continue;
}
else if(strcmp(argv[i], str2) == 0)
Have_L = 1;
else
{
dir = argv[i]; //record the directory
int num_ch = strlen(dir);
if(dir[num_ch - 1] =='/')
dir[num_ch-1] = 0;
//printf("directory: %s\n",dir);
}
}
//printf("-L: %d; -n: %d\n",Have_L,Have_n);
doSearch(dir);
sort(dir_Path,output,num);
for(i = 0; i < num; i++)
{
printf("%s\n",output[i]);
}
return 0;
}
测试输入:
search.o -n "*.py" -L ~/Desktop/workspace/python/
('rw-rw-r--', 1080, '/home/openking/Desktop/workspace/python/search.py')
('rw-rw-r--', 2442, '/home/openking/Desktop/workspace/python/shell.py')
('rw-rw-r--', 2262, '/home/openking/Desktop/workspace/python/test.py')