小工具

清洗/清理设备2020年06月20日

刚上手玩 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; iargc; 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 ( (_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 ( (_mode  S_IFMT)==S_IFREG )

{

if ( x==1 ) f_open( strcat(c_dir, dir-d_name ) );

if ( x!=1 )

{

file://if ( _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_s1000)

{

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);

佛山白癜风医院有哪些
小儿积食的食疗法
心肌梗死支架多少钱
相关阅读
“凭什么或多或少天生资质聪颖, 我生来就是被人欺辱的命,而他洛冰河一来就能师承高门” 沈九 沈清秋

“怎么会同样天生资质聪颖, 我但他却就是被人欺辱的大喜,而他洛冰山一...

2023-10-23
莲藕新举措,不炒不炖营养还好吃,吃一下满嘴都是香嫩脆爽

在日常的孤独当之中,不同的季节可以可用不同的食物适切着可食用,这样对...

2023-10-11
祥硕将发表USB 4主机控制器芯片组

2022-05-22 10:25:20 作者:吕英娜 ASMedia)正式宣布成为首家推出USB 4PC伺服芯片组...

2023-10-07
金融市场提问:您好,请问贵司公众号中提到的“工程造价管控系统”是什么?跟普通...

投资额者回答:您好,请问贵司社会所号中的提到的“设计标准管理制度系统...

2023-10-05
《中国舞会:我的女儿出嫁了》中,沈梦辰说结婚领证没有了仪式感

103010年,沈梦辰该软件恢复了他的执照其网站! 匆匆递交,匆匆放照,匆匆...

2023-10-02
恒生指数、印度、韩国、上证指数、印尼等区域强势行情成型怎样的布局

5月20日亚澳股市居于涨跌之前列为联系汇率、巴基斯坦、中韩、上证指数、马...

2023-09-30
友情链接