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);
105 static int print_file(FF_PKT *ff, void *pkt)
110 if (debug_level == 1) {
111 printf("%s\n", ff->fname);
112 } else if (debug_level > 1) {
113 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
117 if (debug_level == 1) {
118 printf("%s\n", ff->fname);
119 } else if (debug_level > 1) {
120 printf("Empty: %s\n", ff->fname);
125 if (debug_level == 1) {
126 printf("%s\n", ff->fname);
127 } else if (debug_level > 1) {
128 printf("Reg: %s\n", ff->fname);
133 if (debug_level == 1) {
134 printf("%s\n", ff->fname);
135 } else if (debug_level > 1) {
136 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
141 if (debug_level == 1) {
142 printf("%s\n", ff->fname);
143 } else if (debug_level > 1) {
144 printf("Dir: %s\n", ff->fname);
149 if (debug_level == 1) {
150 printf("%s\n", ff->fname);
151 } else if (debug_level > 1) {
152 printf("Spec: %s\n", ff->fname);
157 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
160 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
163 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
166 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
169 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
172 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
175 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
178 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
181 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
186 encode_attribsEx(NULL, attr, ff);
188 printf("AttrEx=%s\n", attr);
190 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
195 static void count_files(FF_PKT *ar)
199 char file[MAXSTRING];
200 char spath[MAXSTRING];
204 /* Find path without the filename.
205 * I.e. everything after the last / is a "filename".
206 * OK, maybe it is a directory name, but we treat it like
207 * a filename. If we don't find a / then the whole name
208 * must be a path name (e.g. c:).
210 for (p=l=ar->fname; *p; p++) {
212 l = p; /* set pos of last slash */
215 if (*l == '/') { /* did we find a slash? */
216 l++; /* yes, point to filename */
217 } else { /* no, whole thing must be path name */
221 /* If filename doesn't exist (i.e. root directory), we
222 * simply create a blank name consisting of a single
223 * space. This makes handling zero length filenames
227 if (fnl > max_file_len) {
231 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
236 strncpy(file, l, fnl); /* copy filename */
239 file[0] = ' '; /* blank filename */
244 if (pnl > max_path_len) {
248 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
252 strncpy(spath, ar->fname, pnl);
257 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
259 if (debug_level >= 10) {
260 printf("Path: %s\n", spath);
261 printf("File: %s\n", file);