扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
search 工具设计时我想有下面的功能:
1、基本功能:可以打开指定目录下的所有文件,按行搜索输入的关键字
2、支持子目录搜索
3、支持大小写不敏感搜索
但我目前只实现了第一个功能,但立刻派上用场帮了我大忙,后来的两个功能觉得没有太大的必要性,自己写自己用的工具能用就行,这是我的观点。这是我在 UNIX 下第一次自己写工具,觉得自己写一些工具真的是一个在计算机界混的人所应具备的基本技能!我把这个工具的源码放上来,希望对初学者有一点参考价值,也希望有人能完成后两个功能,或是扩充更多的功能,别忘了通知我一声呀!请下载。这个文件在 SCO OpenServer 5 下用 cc 编译通过,在别的系统上改改头文件就能通过。
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
int cou_f=0,cou_s=0,cou_t=0;
int x = 0;
int r = 0;
int c = 0;
char file_r[255] = "result";
char dirname[255] = ".";
char keyword[255] = "";
main(argc,argv)
int argc;
char **argv;
{
int i;
FILE *fp;
char buf[256];
void search();
printf("Press ''search -h'' to get help.\n");
for (i=1; i<argc; i++)
{
if (strcmp(argv[i],"-x")==0) x = 1;
else if (strcmp(argv[i],"-r")==0) r = 1;
else if (strcmp(argv[i],"-c")==0) c = 1;
else if (strcmp(argv[i],"-o")==0) strcpy(file_r,argv[++i]);
else if (strcmp(argv[i],"-d")==0) strcpy(dirname,argv[++i]);
else if (strcmp(argv[i],"-k")==0) strcpy(keyword,argv[++i]);
else if (strcmp(argv[i],"-h")==0)
{
printf("\n Tool for search keyword in files for SCO OpenServer\n");
printf(" Author: Daibo, 2000/3/29 \n");
printf("\n Usage: search -x -r -c -o ... -d ... -h \n");
printf(" -x open executive files.\n");
printf(" -r open child directory.\n");
printf(" -c Lower an Upper case sencitive.\n");
printf(" -o filename The output file,default is ''Result''.\n");
printf(" -d pathname The path,default is the current path.\n");
printf(" -k keyword The keyword you want to search.\n");
printf(" -h get help.\n");
}
}
if ( strlen( keyword ) < 1 )
{
printf("You must input the keyword.\n");
exit(1);
}
if ((fp = fopen(file_r,"w"))==NULL)
{
printf("Error openning file %s.",file_r);
exit(1);
}
if ( (dirname[0] != ''.'') && (dirname[0] != ''/'') )
{
strcpy( buf, "./" );
strcat( buf, dirname );
strcpy( dirname, buf );
}
printf("x:%d r:%d c:%d\n",x,r,c);
printf("directory: %s output: %s", dirname, file_r);
search(dirname);
if ((fp = fopen(file_r,"a"))==NULL)
{
printf("Error openning file %s.",file_r);
exit(1);
}
printf("\nI have searched %d files.\n", cou_t);
printf("\nThere are %d files, %d sentences include the keyword.\nFor details,see the file %s.\n", cou_f, cou_s, file_r);
fprintf(fp,"\nI have searched %d files.\n",cou_t);
fprintf(fp,"There are %d files, %d sentences include the keyword.\n", cou_f, cou_s);
fclose(fp);
}
void search(char *dir_n)
{
DIR *dp;
struct direct *dir;
struct stat sbuf;
char c_dir[255] = "";
char c_dir1[255] = "";
void f_open(char* file);
strcat(c_dir,dir_n);
strcat(c_dir,"/");
strcpy(c_dir1,c_dir);
if ((dp = opendir(dir_n))==NULL)
{
printf("Error openning directory %s.\n",dir_n);
exit(1);
}
printf("\nSearching %s ...\n ", dir_n); file://getchar();
while( ( dir = readdir(dp) )!=NULL )
{
strcpy(c_dir,c_dir1);
if ( dir->d_ino == 0 ) continue;
if ( ( stat( dir->d_name,&sbuf ) )<0 ) continue;
if ( (sbuf.st_mode & S_IFMT)==S_IFDIR )
if ( strcmp(dir->d_name, ".") != 0 )
if ( strcmp(dir->d_name, "..") != 0)
if (r==1) search( strcat(c_dir,dir->d_name ) );
if ( (sbuf.st_mode & S_IFMT)==S_IFREG )
{
if ( x==1 ) f_open( strcat(c_dir, dir->d_name ) );
if ( x!=1 )
{
file://if ( sbuf.st_mode & S_IEXEC==0)
if ( strcmp(file_r, dir->d_name) != 0 )
f_open( strcat(c_dir, dir->d_name ) );
}
}
}
printf( "%s search completed.\n", dir_n );
}
void f_open(char* file)
{
char str[1000];
FILE *fp,*r_fp;
int flag=0;
int n=0;
if ( ( fp = fopen( file, "r" ) ) == NULL)
{
printf("Error when openning file %s\n", file);
exit(1);
}
if ( ( r_fp = fopen( file_r, "a" ) ) == NULL)
{
printf("Error when openning file %s\n", file_r);
exit(1);
}
printf("%s \n",file);
while ( ( fgets(str, 1000, fp) )!=NULL )
{
n++;
if ( strstr( str, keyword )!=NULL )
{
cou_s++; flag = 1;
if (cou_s>1000)
{
printf( "Too many ... ... I''m tired.\n" );
exit(0);
}
printf(" >> Found keyword %s in file %s line %d:\n", keyword, file, n);
printf(" %s\n", str);
fprintf(r_fp," >> Found keyword %s in file %s line %d:\n",keyword,file,n);
fprintf(r_fp," %s\n", str);
}
}
if (flag) cou_f++;
cou_t++;
fclose(fp);
fclose(r_fp);
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者