荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Ohoh (Linux), 信区: Linux
标  题: [转载]小工具 search
发信站: 荔园晨风BBS站 (Wed Nov 21 16:54:58 2001), 转信

                        作者:bobdai[bobdai@sohu.com]

    刚上手玩 UNIX 的时候,对系统命令不熟,可是编程的时候,经常要到
include 目录里去查哪个函数或哪个常数等等的在哪个头文件里。include 里的文件
实在是太多了!开始我只有去猜,后来实在猜不出就去查有这个功能命令,但立刻被
UNIX 的命令弄得头晕脑胀(我直到现在都还有“UNIX 命令恐惧症”)。后来一想,
不如自己动手吧。既得到了想要的功能,又能帮助自己熟悉熟悉 UNIX 的编程,一举
两得,何乐而不为呢?于是就有了 search 这个小工具。

    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); //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 )
      {
        //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);
}

【注】:受BBS文章列数的限制,排版不是很好。


--
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.1.119]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店