所以在多次覺得不便胡情況下,決定自己用C 語言實現一個find命令的精簡版本,僅僅爲了查找文件或者目錄;
代碼如下:
find.c:
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#include<errno.h>
#include <time.h>
#include"unistd.h"
#include"sys/types.h"
#include"fcntl.h"
int findfile(char *filename,char *path){
struct stat statbuf;
struct dirent *dirp;
DIR *dp;
char tmpbuf[100];
char *ptr;
int num;
if(lstat(path,&statbuf) < 0){
fprintf(stderr,"lstat is error %s",strerror(errno));
return 0;
}
if(S_ISDIR(statbuf.st_mode) == 0){ //is a dir
return 0;
}
if((dp = opendir(path)) == NULL){
fprintf(stderr,"opendir is error %s \n",strerror(errno));
return 0;
}
ptr = path + strlen(path);
*ptr++ = '/';
*ptr = 0;
while((dirp = readdir(dp)) != NULL){
num = 0;
if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)
continue;
strcpy(ptr,dirp->d_name);
if(strcmp(dirp->d_name,filename) ==0){
printf("the result:%s\n",path);
}
if(findfile(filename,path) != 0) //使用遞歸層級查找所有當前目錄下以及當前目錄下胡目錄下存在的同名文件
break;
}
closedir(dp);
return 0;
}
int main(int arg,char **argc)
{
int i=0;
int ret;
// int resend;
char *tmppath,*path;
char *filename;
FILE *fp;
// printf("111111111111111111111\n");
// for(i = 0; i < arg; i++ )
// printf("the %d argc is %s\n",i,argc[i]);
if(arg != 3){
fprintf(stderr,"args lack:%s\n",strerror(errno));
return -1;
}else{
printf("the original path is %s\n",argc[1]);
path = realpath(argc[1],tmppath);
printf("the path: %s\n",path);
filename = argc[2];
printf("to find filename : %s\n",argc[2]);
}
printf("start:\n\n");
ret = findfile(filename,path);
printf("over!\n\n");
exit(ret);
}
程序寫完之後,利用交叉編譯工具,將find.c編譯成android下能運行的可執行程序:
我所用的是交叉編譯工具是arm-none-linux-gnueabi-gcc 4.4.3版本的;
配置交叉編譯工具環境變量就不多說了:
編譯find.c文件成find可執行文件
lei_lei.zhou@bj61019pcu:~/Find$ arm-none-linux-gnueabi-gcc find.c -static -o find
將find文件push到adb中去:
lei_lei.zhou@bj61019pcu:~/Find$ adb push find /system/bin/
(如果出現failed to copy 'find' to '/system/bin//find': Read-only file system錯誤,進入到adb shell中執行mount -o rw,remount /system將system文件夾變爲可讀寫的文件系統,再執行上一個命令)
find文件push到system中去了,就可以直接用find查找文件了;
注意這個find命令僅僅實現一個簡單且常用查找方式方法:
用法:find <查找起始路徑> <想要查找的文件>
例如:
lei_lei.zhou@bj61019pcu:~/Find$ adb shell
root@MyTestPhone:/ # find ./ wipe //在當前根目錄向下搜索wipe文件,結果在/system/bin下存在
the path: /
to find filename : wipe
start:
the result://system/bin/wipe
over!
至此,find命令移植到android中完成,如有什麼問題可直接聯繫我:
qq:996340566