Rev 6 | Rev 11 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6 | Rev 10 | ||
---|---|---|---|
1 | /* ============================================================================ |
1 | /* ============================================================================ |
2 | # Create |
2 | # Create |
3 | # name : Andre Rikkert de Koe date: apr 2011 |
3 | # name : Andre Rikkert de Koe date: apr 2011 |
4 | # descr : filefind |
4 | # descr : filefind |
5 | # This is a demo of programming in the java language. |
5 | # This is a demo of programming in the java language. |
6 | # Its part of a project to implement a program to create a directory |
6 | # Its part of a project to implement a program to create a directory |
7 | # tree similar to the output of the Unix find program. |
7 | # tree similar to the output of the Unix find program. |
8 | # EUID : Any user who has read access to all the files being searched for. |
8 | # EUID : Any user who has read access to all the files being searched for. |
9 | # run : interactive |
9 | # run : interactive |
10 | # |
10 | # |
11 | # Changes |
11 | # Changes |
12 | # name : date: |
12 | # name : date: |
13 | # descr : short description |
13 | # descr : short description |
14 | # |
14 | # |
15 | # ========================================================================== */ |
15 | # ========================================================================== */ |
16 | 16 | ||
17 | import java.io.*; |
17 | import java.io.*; |
18 | import java.util.*; |
18 | import java.util.*; |
19 | 19 | ||
20 | public class filefind /*implements Runnable */ |
20 | public class filefind |
21 | { |
21 | { |
22 | protected File base; |
22 | protected File base; |
23 | protected PrintStream out; |
23 | protected PrintStream out; |
24 | 24 | ||
25 | static void Usage(String progname, String mesg) |
25 | static void Usage(String progname, String mesg) |
26 | { |
26 | { |
27 | System.out.println("Usage: " + progname + " directory"); |
27 | System.out.println("Usage: " + progname + " directory"); |
28 | System.out.println(mesg); |
28 | System.out.println(mesg); |
29 | } |
29 | } |
30 | 30 | ||
31 | static boolean issymlink(File file1) |
31 | static boolean issymlink(File file1) |
32 | { |
32 | { |
33 | boolean result; |
33 | boolean result; |
34 | try |
34 | try |
35 | { |
35 | { |
36 | result = !file1.getAbsolutePath().equals(file1.getCanonicalPath()); |
36 | result = !file1.getAbsolutePath().equals(file1.getCanonicalPath()); |
37 | } |
37 | } |
38 | catch (IOException ie) |
38 | catch (IOException ie) |
39 | { |
39 | { |
40 | System.err.println("Caught IOException: " + ie.getMessage()); |
40 | System.err.println("Caught IOException: " + ie.getMessage()); |
41 | result = false; |
41 | result = false; |
42 | } |
42 | } |
43 | return result; |
43 | return result; |
44 | } |
44 | } |
45 | /** |
- | |
46 | * Create a filefind object for the given string, with |
- | |
47 | * a print indentation given. |
- | |
48 | */ |
45 | |
49 | public filefind(String filename) |
46 | public filefind(String filename) |
50 | { |
47 | { |
51 | base = new File(filename); |
48 | base = new File(filename); |
52 | } |
49 | } |
53 | 50 | ||
54 | public void start_traverse(String directory) |
51 | static void start_traverse(File base, String basename) |
55 | { |
52 | { |
56 | System.out.println(directory); |
53 | System.out.println(basename); |
57 | traverse(base); |
54 | traverse(base); |
58 | } |
55 | } |
59 | 56 | ||
60 | static void traverse(File b) |
57 | static void traverse(File b) |
61 | { |
58 | { |
62 | String curdir; |
- | |
63 | curdir = b.getAbsolutePath(); |
- | |
64 | System.setProperty("user.dir",curdir); |
- | |
65 | - | ||
66 | File [] subs = b.listFiles(); |
59 | File [] subs = b.listFiles(); |
67 | for(int i = 0; i < subs.length; i++) |
60 | for(int i = 0; i < subs.length; i++) |
68 | { |
61 | { |
69 | //File f2 = new File(subs[i]); |
- | |
70 | if (subs[i].isDirectory() && !issymlink(subs[i])) |
62 | if (subs[i].isDirectory() && !issymlink(subs[i])) |
71 | { |
63 | { |
72 | System.out.println(subs[i]); |
64 | System.out.println(subs[i]); |
73 | traverse(subs[i]); |
65 | traverse(subs[i]); |
74 | } |
66 | } |
75 | else |
67 | else |
76 | { |
68 | { |
77 | System.out.println(subs[i]); |
69 | System.out.println(subs[i]); |
78 | } |
70 | } |
79 | } |
71 | } |
80 | System.setProperty("user.dir",curdir); |
- | |
81 | } |
72 | } |
82 | 73 | ||
83 | /* ============================================================================ |
74 | /* ============================================================================ |
84 | # MAIN |
75 | # MAIN |
85 | # ========================================================================== */ |
76 | # ========================================================================== */ |
86 | 77 | ||
87 | public static void main(String [] args) |
78 | public static void main(String [] args) |
88 | { |
79 | { |
89 | String basename; |
80 | String basename; |
90 | String progname = "filefind"; |
81 | String progname = "filefind"; |
91 | if (args.length == 1) |
82 | if (args.length == 1) |
92 | { |
83 | { |
93 | basename = args[0]; |
84 | basename = args[0]; |
94 | File base = new File(basename); |
85 | File base = new File(basename); |
95 | if (base.isDirectory()) |
86 | if (base.isDirectory()) |
96 | { |
87 | { |
97 | filefind walker = new filefind(basename); |
- | |
98 | //walker.setOutput(System.out); |
- | |
99 | walker.start_traverse(basename); |
88 | start_traverse(base, basename); |
100 | } |
89 | } |
101 | else |
90 | else |
102 | { |
91 | { |
103 | Usage(progname, "ERROR: No directory " + basename + " found"); |
92 | Usage(progname, "ERROR: No directory " + basename + " found"); |
104 | } |
93 | } |
105 | } |
94 | } |
106 | else |
95 | else |
107 | Usage(progname, "ERROR: 1 argument expected"); |
96 | Usage(progname, "ERROR: 1 argument expected"); |
108 | } |
97 | } |
109 | } |
98 | } |
110 | 99 |