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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum { buf_size = 10 };
struct path_type {
char *buffer;
int size;
int pos;
};
static void init_path(struct path_type *path)
{
path->buffer = malloc(buf_size);
path->size = buf_size;
path->pos = 0;
}
static void copy_str(char *first, char *second, int pos)
{
int i;
for(i = 0; i <= pos; ++i)
first[i] = second[i];
}
static void allocate_memory(struct path_type *path)
{
char *tmp = malloc(path->size * 2);
copy_str(tmp, path->buffer, path->pos);
free(path->buffer);
path->buffer = tmp;
path->size *= 2;
}
static void add_dir_into_path(char *directory_name, int length,
struct path_type *path)
{
int i;
for(i = 0; i < length; ++i, ++path->pos)
path->buffer[path->pos] = directory_name[i];
path->buffer[path->pos] = '/';
++path->pos;
path->buffer[path->pos] = '\0';
}
static void edit_path(struct path_type *path)
{
if(path->pos == 2)
return;
path->pos -= 2; /* skip '/' and '\0' */
for( ; path->buffer[path->pos] != '/'; --path->pos)
path->buffer[path->pos] = '\0';
++path->pos;
}
static void recursive_step(char *target_name, struct path_type *path)
{
int name_length;
char *directory_name;
DIR *current_directory;
struct dirent *data;
directory_name = path->buffer;
current_directory = opendir(directory_name);
while((data = readdir(current_directory))) {
if(data->d_type == DT_DIR) {
if(!strcmp(data->d_name, target_name))
printf("%s%s\n", path->buffer, data->d_name);
if(!strcmp(data->d_name, ".") || !strcmp(data->d_name, ".."))
continue;
name_length = strlen(data->d_name);
name_length += 2; /* slash and null character */
while(name_length + path->pos > path->size)
allocate_memory(path);
add_dir_into_path(data->d_name, strlen(data->d_name), path);
recursive_step(target_name, path);
edit_path(path);
} else if(!strcmp(data->d_name, target_name))
printf("%s%s\n", path->buffer, data->d_name);
}
closedir(current_directory);
}
static void set_start_path(struct path_type *path)
{
path->buffer[path->pos] = '.';
++path->pos;
path->buffer[path->pos] = '/';
++path->pos;
path->buffer[path->pos] = '\0';
}
int main(int argc, char **argv)
{
struct path_type path;
if(argc != 2) {
fprintf(stderr, "enter only one param\n");
return 1;
}
init_path(&path);
set_start_path(&path);
recursive_step(argv[1], &path);
free(path.buffer);
return 0;
}
|