当前位置:早雪网网络学院编程文档C/C++ → 小工具 search

小工具 search

减小字体 增大字体 作者:不详  来源:supcode.com收集整理  发布时间:2005-7-22 19:38:31
    刚上手玩 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 ) );
      }
    

[1] [2]  下一页


Tags:工具,search
[数据载入中...] [返回上一页] [打 印]