Subversion Repositories svn.Prod repos

Rev

Rev 10 | Rev 43 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 10 Rev 11
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
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
 
45
 
46
        public filefind(String filename)
46
        public filefind(String filename)
47
        {
47
        {
48
                base = new File(filename);
48
                base = new File(filename);
49
        }
49
        }
50
 
50
 
51
        static void start_traverse(File base, String basename)
51
        static void start_traverse(File base)
52
        {
52
        {
-
 
53
                try
-
 
54
                {
53
                System.out.println(basename);
55
                        System.out.println(base.getCanonicalPath());
-
 
56
                }
-
 
57
                catch (IOException ie)
-
 
58
                {
-
 
59
                        System.err.println("Caught IOException: " + ie.getMessage());
-
 
60
                }
54
                traverse(base);
61
                traverse(base);
55
        }
62
        }
56
 
63
 
57
        static void traverse(File b)
64
        static void traverse(File b)
58
        {
65
        {
59
                File [] subs = b.listFiles();
66
                File [] subs = b.listFiles();
60
                for(int i = 0; i < subs.length; i++)
67
                for(int i = 0; i < subs.length; i++)
61
                {
68
                {
62
                        if (subs[i].isDirectory() && !issymlink(subs[i]))
69
                        if (subs[i].isDirectory() && !issymlink(subs[i]))
63
                        {
70
                        {
64
                                System.out.println(subs[i]);
71
                                System.out.println(subs[i]);
65
                                traverse(subs[i]);
72
                                traverse(subs[i]);
66
                        }
73
                        }
67
                        else
74
                        else
68
                        {
75
                        {
69
                                System.out.println(subs[i]);
76
                                System.out.println(subs[i]);
70
                        }
77
                        }
71
                }
78
                }
72
        }
79
        }
73
 
80
 
74
/* ============================================================================
81
/* ============================================================================
75
# MAIN
82
# MAIN
76
# ========================================================================== */
83
# ========================================================================== */
77
 
84
 
78
        public static void main(String [] args)
85
        public static void main(String [] args)
79
        {
86
        {
80
                String basename;
87
                String basename;
81
                String progname = "filefind";
88
                String progname = "filefind";
82
                if (args.length == 1)
89
                if (args.length == 1)
83
                {
90
                {
84
                        basename = args[0];
91
                        basename = args[0];
85
                        File base = new File(basename);
92
                        File base = new File(basename);
86
                        if (base.isDirectory())
93
                        if (base.isDirectory())
87
                        {
94
                        {
88
                                start_traverse(base, basename);
95
                                start_traverse(base);
89
                        }
96
                        }
90
                        else
97
                        else
91
                        {
98
                        {
92
                                Usage(progname, "ERROR: No directory " + basename + " found");
99
                                Usage(progname, "ERROR: No directory " + basename + " found");
93
                        }
100
                        }
94
                }
101
                }
95
                else
102
                else
96
                Usage(progname, "ERROR: 1 argument expected");
103
                Usage(progname, "ERROR: 1 argument expected");
97
        }
104
        }
98
}
105
}
99
 
106