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)
49 int i, ch, hard_links;
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);
90 hard_links = term_find_files(ff);
94 Max file length: %d\n\
95 Max path length: %d\n\
96 Files truncated: %d\n\
97 Paths truncated: %d\n\
99 num_files, max_file_len, max_path_len,
100 trunc_fname, trunc_path, hard_links);
107 static int print_file(FF_PKT *ff, void *pkt)
112 if (debug_level == 1) {
113 printf("%s\n", ff->fname);
114 } else if (debug_level > 1) {
115 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
119 if (debug_level == 1) {
120 printf("%s\n", ff->fname);
121 } else if (debug_level > 1) {
122 printf("Empty: %s\n", ff->fname);
127 if (debug_level == 1) {
128 printf("%s\n", ff->fname);
129 } else if (debug_level > 1) {
130 printf("Reg: %s\n", ff->fname);
135 if (debug_level == 1) {
136 printf("%s\n", ff->fname);
137 } else if (debug_level > 1) {
138 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
143 if (debug_level == 1) {
144 printf("%s\n", ff->fname);
145 } else if (debug_level > 1) {
146 printf("Dir: %s\n", ff->fname);
151 if (debug_level == 1) {
152 printf("%s\n", ff->fname);
153 } else if (debug_level > 1) {
154 printf("Spec: %s\n", ff->fname);
159 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
162 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
165 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
168 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
171 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
174 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
177 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
180 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
183 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
188 encode_attribsEx(NULL, attr, ff);
190 printf("AttrEx=%s\n", attr);
192 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
197 static void count_files(FF_PKT *ar)
201 char file[MAXSTRING];
202 char spath[MAXSTRING];
206 /* Find path without the filename.
207 * I.e. everything after the last / is a "filename".
208 * OK, maybe it is a directory name, but we treat it like
209 * a filename. If we don't find a / then the whole name
210 * must be a path name (e.g. c:).
212 for (p=l=ar->fname; *p; p++) {
214 l = p; /* set pos of last slash */
217 if (*l == '/') { /* did we find a slash? */
218 l++; /* yes, point to filename */
219 } else { /* no, whole thing must be path name */
223 /* If filename doesn't exist (i.e. root directory), we
224 * simply create a blank name consisting of a single
225 * space. This makes handling zero length filenames
229 if (fnl > max_file_len) {
233 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
238 strncpy(file, l, fnl); /* copy filename */
241 file[0] = ' '; /* blank filename */
246 if (pnl > max_path_len) {
250 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
254 strncpy(spath, ar->fname, pnl);
259 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
261 if (debug_level >= 10) {
262 printf("Path: %s\n", spath);
263 printf("File: %s\n", file);