2 * Test program for find files
6 #include "findlib/find.h"
10 /* Global variables */
11 static int num_files = 0;
12 static int max_file_len = 0;
13 static int max_path_len = 0;
14 static int trunc_fname = 0;
15 static int trunc_path = 0;
19 static int print_file(FF_PKT *ff, void *pkt);
20 static void count_files(FF_PKT *ff);
26 "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n"
27 " -a print extended attributes (Win32 debug)\n"
28 " -dnn set debug level to nn\n"
29 " - read pattern(s) from stdin\n"
30 " -? print this message.\n"
32 "Patterns are file inclusion -- normally directories.\n"
33 "Debug level >= 1 prints each file found.\n"
34 "Debug level >= 10 prints path/file for catalog.\n"
35 "Errors always printed.\n"
36 "Files/paths truncated is number with len > 255.\n"
37 "Truncation is only in catalog.\n"
45 main (int argc, char *const *argv)
51 while ((ch = getopt(argc, argv, "ad:?")) != -1) {
53 case 'a': /* print extended attributes *debug* */
57 case 'd': /* set debug level */
58 debug_level = atoi(optarg);
59 if (debug_level <= 0) {
73 ff = init_find_files();
75 add_fname_to_include_list(ff, 0, "/"); /* default to / */
77 for (i=0; i < argc; i++) {
78 if (strcmp(argv[i], "-") == 0) {
79 while (fgets(name, sizeof(name)-1, stdin)) {
80 strip_trailing_junk(name);
81 add_fname_to_include_list(ff, 0, name);
85 add_fname_to_include_list(ff, 0, argv[i]);
89 find_files(ff, print_file, NULL);
94 Max file length: %d\n\
95 Max path length: %d\n\
96 Files truncated: %d\n\
97 Paths truncated: %d\n"),
98 num_files, max_file_len, max_path_len,
99 trunc_fname, trunc_path);
106 static int print_file(FF_PKT *ff, void *pkt)
111 if (debug_level == 1) {
112 printf("%s\n", ff->fname);
113 } else if (debug_level > 1) {
114 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
118 if (debug_level == 1) {
119 printf("%s\n", ff->fname);
120 } else if (debug_level > 1) {
121 printf("Empty: %s\n", ff->fname);
126 if (debug_level == 1) {
127 printf("%s\n", ff->fname);
128 } else if (debug_level > 1) {
129 printf("Reg: %s\n", ff->fname);
134 if (debug_level == 1) {
135 printf("%s\n", ff->fname);
136 } else if (debug_level > 1) {
137 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
142 if (debug_level == 1) {
143 printf("%s\n", ff->fname);
144 } else if (debug_level > 1) {
145 printf("Dir: %s\n", ff->fname);
150 if (debug_level == 1) {
151 printf("%s\n", ff->fname);
152 } else if (debug_level > 1) {
153 printf("Spec: %s\n", ff->fname);
158 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
161 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
164 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
167 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
170 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
173 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
176 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
179 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
182 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
187 encode_attribsEx(NULL, attr, ff);
189 printf("AttrEx=%s\n", attr);
191 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
196 static void count_files(FF_PKT *ar)
200 char file[MAXSTRING];
201 char spath[MAXSTRING];
205 /* Find path without the filename.
206 * I.e. everything after the last / is a "filename".
207 * OK, maybe it is a directory name, but we treat it like
208 * a filename. If we don't find a / then the whole name
209 * must be a path name (e.g. c:).
211 for (p=l=ar->fname; *p; p++) {
213 l = p; /* set pos of last slash */
216 if (*l == '/') { /* did we find a slash? */
217 l++; /* yes, point to filename */
218 } else { /* no, whole thing must be path name */
222 /* If filename doesn't exist (i.e. root directory), we
223 * simply create a blank name consisting of a single
224 * space. This makes handling zero length filenames
228 if (fnl > max_file_len) {
232 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
237 strncpy(file, l, fnl); /* copy filename */
240 file[0] = ' '; /* blank filename */
245 if (pnl > max_path_len) {
249 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
253 strncpy(spath, ar->fname, pnl);
258 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
260 if (debug_level >= 10) {
261 printf("Path: %s\n", spath);
262 printf("File: %s\n", file);