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 " -dnn set debug level to nn\n"
28 " - read pattern(s) from stdin\n"
29 " -? print this message.\n"
31 "Patterns are file inclusion -- normally directories.\n"
32 "Debug level >= 1 prints each file found.\n"
33 "Debug level >= 10 prints path/file for catalog.\n"
34 "Errors always printed.\n"
35 "Files/paths truncated is number with len > 255.\n"
36 "Truncation is only in catalog.\n"
44 main (int argc, char *const *argv)
50 while ((ch = getopt(argc, argv, "ad:?")) != -1) {
52 case 'a': /* print extended attributes *debug* */
56 case 'd': /* set debug level */
57 debug_level = atoi(optarg);
58 if (debug_level <= 0) {
72 ff = init_find_files();
74 add_fname_to_include_list(ff, 0, "/"); /* default to / */
76 for (i=0; i < argc; i++) {
77 if (strcmp(argv[i], "-") == 0) {
78 while (fgets(name, sizeof(name)-1, stdin)) {
79 strip_trailing_junk(name);
80 add_fname_to_include_list(ff, 0, name);
84 add_fname_to_include_list(ff, 0, argv[i]);
88 find_files(ff, print_file, NULL);
93 Max file length: %d\n\
94 Max path length: %d\n\
95 Files truncated: %d\n\
96 Paths truncated: %d\n"),
97 num_files, max_file_len, max_path_len,
98 trunc_fname, trunc_path);
104 static int print_file(FF_PKT *ff, void *pkt)
109 if (debug_level == 1) {
110 printf("%s\n", ff->fname);
111 } else if (debug_level > 1) {
112 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
116 if (debug_level == 1) {
117 printf("%s\n", ff->fname);
118 } else if (debug_level > 1) {
119 printf("Empty: %s\n", ff->fname);
124 if (debug_level == 1) {
125 printf("%s\n", ff->fname);
126 } else if (debug_level > 1) {
127 printf("Reg: %s\n", ff->fname);
132 if (debug_level == 1) {
133 printf("%s\n", ff->fname);
134 } else if (debug_level > 1) {
135 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
140 if (debug_level == 1) {
141 printf("%s\n", ff->fname);
142 } else if (debug_level > 1) {
143 printf("Dir: %s\n", ff->fname);
148 if (debug_level == 1) {
149 printf("%s\n", ff->fname);
150 } else if (debug_level > 1) {
151 printf("Spec: %s\n", ff->fname);
156 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
159 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
162 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
165 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
168 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
171 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
174 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
177 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
180 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
185 encode_attribsEx(NULL, attr, ff);
187 printf("AttrEx=%s\n", attr);
189 set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
194 static void count_files(FF_PKT *ar)
198 char file[MAXSTRING];
199 char spath[MAXSTRING];
203 /* Find path without the filename.
204 * I.e. everything after the last / is a "filename".
205 * OK, maybe it is a directory name, but we treat it like
206 * a filename. If we don't find a / then the whole name
207 * must be a path name (e.g. c:).
209 for (p=l=ar->fname; *p; p++) {
211 l = p; /* set pos of last slash */
214 if (*l == '/') { /* did we find a slash? */
215 l++; /* yes, point to filename */
216 } else { /* no, whole thing must be path name */
220 /* If filename doesn't exist (i.e. root directory), we
221 * simply create a blank name consisting of a single
222 * space. This makes handling zero length filenames
226 if (fnl > max_file_len) {
230 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
235 strncpy(file, l, fnl); /* copy filename */
238 file[0] = ' '; /* blank filename */
243 if (pnl > max_path_len) {
247 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
251 strncpy(spath, ar->fname, pnl);
256 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
258 if (debug_level >= 10) {
259 printf("Path: %s\n", spath);
260 printf("File: %s\n", file);