2 * Test program for find files
6 Copyright (C) 2000-2003 Kern Sibbald and John Walker
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public
19 License along with this program; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 #include "findlib/find.h"
30 /* Global variables */
31 static int num_files = 0;
32 static int max_file_len = 0;
33 static int max_path_len = 0;
34 static int trunc_fname = 0;
35 static int trunc_path = 0;
39 static int print_file(FF_PKT *ff, void *pkt);
40 static void count_files(FF_PKT *ff);
46 "Usage: testfind [-d debug_level] [-] [pattern1 ...]\n"
47 " -a print extended attributes (Win32 debug)\n"
48 " -dnn set debug level to nn\n"
49 " - read pattern(s) from stdin\n"
50 " -? print this message.\n"
52 "Patterns are file inclusion -- normally directories.\n"
53 "Debug level >= 1 prints each file found.\n"
54 "Debug level >= 10 prints path/file for catalog.\n"
55 "Errors always printed.\n"
56 "Files/paths truncated is number with len > 255.\n"
57 "Truncation is only in catalog.\n"
65 main (int argc, char *const *argv)
69 int i, ch, hard_links;
71 while ((ch = getopt(argc, argv, "ad:?")) != -1) {
73 case 'a': /* print extended attributes *debug* */
77 case 'd': /* set debug level */
78 debug_level = atoi(optarg);
79 if (debug_level <= 0) {
93 ff = init_find_files();
95 add_fname_to_include_list(ff, 0, "/"); /* default to / */
97 for (i=0; i < argc; i++) {
98 if (strcmp(argv[i], "-") == 0) {
99 while (fgets(name, sizeof(name)-1, stdin)) {
100 strip_trailing_junk(name);
101 add_fname_to_include_list(ff, 0, name);
105 add_fname_to_include_list(ff, 0, argv[i]);
109 find_files(ff, print_file, NULL);
110 hard_links = term_find_files(ff);
114 Max file length: %d\n\
115 Max path length: %d\n\
116 Files truncated: %d\n\
117 Paths truncated: %d\n\
119 num_files, max_file_len, max_path_len,
120 trunc_fname, trunc_path, hard_links);
127 static int print_file(FF_PKT *ff, void *pkt)
132 if (debug_level == 1) {
133 printf("%s\n", ff->fname);
134 } else if (debug_level > 1) {
135 printf("Lnka: %s -> %s\n", ff->fname, ff->link);
139 if (debug_level == 1) {
140 printf("%s\n", ff->fname);
141 } else if (debug_level > 1) {
142 printf("Empty: %s\n", ff->fname);
147 if (debug_level == 1) {
148 printf("%s\n", ff->fname);
149 } else if (debug_level > 1) {
150 printf("Reg: %s\n", ff->fname);
155 if (debug_level == 1) {
156 printf("%s\n", ff->fname);
157 } else if (debug_level > 1) {
158 printf("Lnk: %s -> %s\n", ff->fname, ff->link);
163 if (debug_level == 1) {
164 printf("%s\n", ff->fname);
165 } else if (debug_level > 1) {
166 printf("Dir: %s\n", ff->fname);
171 if (debug_level == 1) {
172 printf("%s\n", ff->fname);
173 } else if (debug_level > 1) {
174 printf("Spec: %s\n", ff->fname);
179 printf(_("Err: Could not access %s: %s\n"), ff->fname, strerror(errno));
182 printf(_("Err: Could not follow ff->link %s: %s\n"), ff->fname, strerror(errno));
185 printf(_("Err: Could not stat %s: %s\n"), ff->fname, strerror(errno));
188 printf(_("Skip: File not saved. No change. %s\n"), ff->fname);
191 printf(_("Err: Attempt to backup archive. Not saved. %s\n"), ff->fname);
194 printf(_("Recursion turned off. Directory not entered. %s\n"), ff->fname);
197 printf(_("Skip: File system change prohibited. Directory not entered. %s\n"), ff->fname);
200 printf(_("Err: Could not open directory %s: %s\n"), ff->fname, strerror(errno));
203 printf(_("Err: Unknown file ff->type %d: %s\n"), ff->type, ff->fname);
208 encode_attribsEx(NULL, attr, ff);
210 printf("AttrEx=%s\n", attr);
212 // set_attribsEx(NULL, ff->fname, NULL, NULL, ff->type, attr);
217 static void count_files(FF_PKT *ar)
221 char file[MAXSTRING];
222 char spath[MAXSTRING];
226 /* Find path without the filename.
227 * I.e. everything after the last / is a "filename".
228 * OK, maybe it is a directory name, but we treat it like
229 * a filename. If we don't find a / then the whole name
230 * must be a path name (e.g. c:).
232 for (p=l=ar->fname; *p; p++) {
234 l = p; /* set pos of last slash */
237 if (*l == '/') { /* did we find a slash? */
238 l++; /* yes, point to filename */
239 } else { /* no, whole thing must be path name */
243 /* If filename doesn't exist (i.e. root directory), we
244 * simply create a blank name consisting of a single
245 * space. This makes handling zero length filenames
249 if (fnl > max_file_len) {
253 printf(_("===== Filename truncated to 255 chars: %s\n"), l);
258 strncpy(file, l, fnl); /* copy filename */
261 file[0] = ' '; /* blank filename */
266 if (pnl > max_path_len) {
270 printf(_("========== Path name truncated to 255 chars: %s\n"), ar->fname);
274 strncpy(spath, ar->fname, pnl);
279 printf(_("========== Path length is zero. File=%s\n"), ar->fname);
281 if (debug_level >= 10) {
282 printf("Path: %s\n", spath);
283 printf("File: %s\n", file);