-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpfind.c
More file actions
59 lines (56 loc) · 1.22 KB
/
pfind.c
File metadata and controls
59 lines (56 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*************************************************************************
> File Name: pfind.c
> Author: jianghechao
> Mail: 591378033@qq.com
> Created Time: Thu 03 Apr 2014 02:46:51 PM CST
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#define MAX_LEN 1024
#define MAX_PATH 1024
void show_Error(char *str)
{
perror(str);
}
void search(char *path,char *filename)
{
char tempPath[MAX_PATH];
struct DIR *dir;
struct dirent *pdirent = NULL;
dir = opendir(path);
if(dir!=NULL)
{
while((pdirent = readdir(dir))!=NULL)
{
if(strcmp(".",pdirent->d_name)==0
||strcmp("..",pdirent->d_name)==0)
continue;
if(strcmp(pdirent->d_name,filename)==0)
{
printf("%s/%s\n",path,filename);
}
sprintf(tempPath,"%s/%s",path,pdirent->d_name);
//printf("%s\n",tempPath);
search(tempPath,filename);
}
closedir(dir);
}
}
int main(int argc,char **argv)
{
char fileName[MAX_LEN];
if(argc!=2)
{
show_Error("输入参数错误:usage:pfind filename");
return 1;
}
strcpy(fileName,argv[1]);
search(".",fileName);
return 0;
}