-
Notifications
You must be signed in to change notification settings - Fork 50
/
recursiv.c
255 lines (200 loc) · 4.07 KB
/
recursiv.c
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* Burra Gopal:
The function of the program is to traverse the
This program is derived from the C-programming language book
Originally, the program opens a directory file as a regular file.
But it won't work.
We have to open a directory file using opendir() system call,
and use readdir() to read each entry of the directory.
[chg] T.Gries 11.08.96
*/
/* #define REC_DIAG */
#include "autoconf.h" /* ../libtemplate/include */
#include <stdio.h>
#include <sys/types.h>
#if ISO_CHAR_SET
#include <locale.h>
#endif
#if HAVE_DIRENT_H
#ifndef _WIN32
# include <dirent.h>
#else
# include "ntdirent.h"
#endif
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
# if HAVE_NDIR_H
# include <ndir.h>
# endif
#endif
#ifdef __APPLE__
#include <sys/stat.h>
#endif
#ifdef _WIN32
#include "config.h"
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
int exec(); /* agrep.c */
#endif
/*
* #include <sys/stat.h>
*/
#include <fcntl.h>
#define BUFSIZE 256
#define DIRSIZE 14
#define max_list 10
#ifndef S_ISREG
#define S_ISREG(mode) (0100000&(mode))
#endif
#ifndef S_ISDIR
#define S_ISDIR(mode) (0040000&(mode))
#endif
/* TG 28.04.04 */
/* TG 2013032 */
#ifndef S_IFDIR
#define S_IFDIR __S_IFDIR
#endif
#ifndef S_IFMT
#define S_IFMT __S_IFMT
#endif
char *file_list[max_list*2];
int fdx=0; /* index of file_List */
extern int Numfiles;
char name_buf[BUFSIZE];
void directory();
static void treewalk();
/* returns -1 if error, num of matches >= 0 otherwise */
int
recursive(argc, argv)
int argc;
char **argv;
{
int i,j;
int num = 0, ret;
for(i=0; i< argc; i++) {
/* printf("RECURSIVE: I= %d = %s\n",i,argv[i]); */
strcpy(name_buf, argv[i]);
treewalk(name_buf);
if(fdx > 0) {
Numfiles = fdx;
if ((ret = exec(3, file_list)) == -1) return -1;
num += ret;
for(j=0; j<fdx; j++) {
free(file_list[j]);
}
}
fdx = 0;
}
return num;
}
/*
main(argc, argv)
int argc; char **argv;
{
char buf[BUFSIZE];
#if ISO_CHAR_SET
setlocale(LC_ALL, "");
#endif
if (argc == 1) {
strcpy(buf, ".");
treewalk(buf);
}
else
while(--argc > 0) {
strcpy(buf, *++argv);
treewalk(buf);
}
}
*/
static void
treewalk(name)
char *name;
{
struct stat stbuf;
int i;
extern void *malloc();
#ifdef REC_DIAG
printf(" In treewalk. name= %s\n",name);
#endif
#ifndef _WIN32
if(lstat(name, &stbuf) == -1) {
#else
if(stat(name, &stbuf) != 0) {
#endif
fprintf(stderr, "permission denied or non-existent: %s\n", name);
return;
}
#ifndef _WIN32
if ((stbuf.st_mode & S_IFMT) == S_IFLNK) {
#ifdef REC_DIAG
fprintf(stderr, "S_IFLNK %s",name);
#endif
return;
}
#endif
if (( stbuf.st_mode & S_IFMT) == S_IFDIR) {
#ifdef REC_DIAG
fprintf(stderr, "S_IFDIR %s",name);
#endif
directory(name);
}
else {
file_list[fdx] = (char *)malloc(BUFSIZE);
strcpy(file_list[fdx++], name);
#ifdef REC_DIAG
printf(" %s\n", name);
#endif
if(fdx >= max_list) {
Numfiles = fdx;
exec(3, file_list);
for(i=0; i<max_list; i++) free(file_list[i]);
fdx=0;
}
}
}
void
directory(name)
char *name;
{
struct dirent *dp;
char *nbp;
DIR *dirp;
#ifdef REC_DIAG
printf("in directory, name= %s\n",name);
#endif
nbp = name + strlen(name);
if( nbp+DIRSIZE+2 >= name+BUFSIZE ) /* name too long */
{
fprintf(stderr, "name too long: %.32s...\n", name);
return;
}
if((dirp = opendir(name)) == NULL) {
fprintf(stderr, "permission denied: %s\n", name);
return;
}
*nbp++ = '/';
*nbp = '\0';
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
if (dp->d_name[0] == '\0' || strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..")==0)
goto CONT;
#ifdef REC_DIAG
printf("dp->d_name = %s\n", dp->d_name);
#endif
strcpy(nbp, dp->d_name);
treewalk(name);
CONT:
;
}
closedir (dirp);
*--nbp = '\0'; /* restore name */
}