Rev 43 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 43 | Rev 44 | ||
---|---|---|---|
1 | //usr/bin/env go run $0 $@; exit $? |
1 | //usr/bin/env go run $0 $@; exit $? |
2 | 2 | ||
3 | // ============================================================================ |
3 | // ============================================================================ |
4 | // Create |
4 | // Create |
5 | // name : Andre Rikkert de Koe date: Mar 2018 |
5 | // name : Andre Rikkert de Koe date: Mar 2018 |
6 | // descr : filefind |
6 | // descr : filefind |
7 | // This is a demo of programming in the Elixir language. |
7 | // This is a demo of programming in the Go language. |
8 | // Its part of a project to implement a program to create a directory |
8 | // Its part of a project to implement a program to create a directory |
9 | // tree similar to the output of the Unix find program. |
9 | // tree similar to the output of the Unix find program. |
10 | // EUID : Any user who has read access to all the files being searched for. |
10 | // EUID : Any user who has read access to all the files being searched for. |
11 | // run : interactive |
11 | // run : interactive |
12 | // |
12 | // |
13 | // Changes |
13 | // Changes |
14 | // name : date: |
14 | // name : date: |
15 | // descr : short description |
15 | // descr : short description |
16 | // |
16 | // |
17 | // ============================================================================ |
17 | // ============================================================================ |
18 | 18 | ||
19 | package main |
19 | package main |
20 | 20 | ||
21 | import ( |
21 | import ( |
22 | "fmt" |
22 | "fmt" |
23 | "io/ioutil" |
23 | "io/ioutil" |
24 | "log" |
24 | "log" |
25 | "os" |
25 | "os" |
26 | ) |
26 | ) |
27 | 27 | ||
28 | // ============================================================================= |
28 | // ============================================================================= |
29 | // Functions |
29 | // Functions |
30 | // ============================================================================= |
30 | // ============================================================================= |
31 | 31 | ||
32 | func Usage(progname, mesg string) { |
32 | func Usage(progname, mesg string) { |
33 | log.Print("Usage: " + progname + " directory") |
33 | log.Print("Usage: " + progname + " directory") |
34 | log.Fatal(mesg) |
34 | log.Fatal(mesg) |
35 | } |
35 | } |
36 | 36 | ||
37 | func traverse(s string) { |
37 | func traverse(s string) { |
38 | curdir, err := os.Getwd() |
38 | curdir, err := os.Getwd() |
39 | if err := os.Chdir(s); err != nil { |
39 | if err := os.Chdir(s); err != nil { |
40 | log.Fatal(err) |
40 | log.Fatal(err) |
41 | } |
41 | } |
42 | if err != nil { |
42 | if err != nil { |
43 | log.Fatal(err) |
43 | log.Fatal(err) |
44 | } |
44 | } |
45 | curdir2, err := os.Getwd() |
45 | curdir2, err := os.Getwd() |
46 | files, err := ioutil.ReadDir(curdir2) |
46 | files, err := ioutil.ReadDir(curdir2) |
47 | if err != nil { |
47 | if err != nil { |
48 | log.Fatal(err) |
48 | log.Fatal(err) |
49 | } |
49 | } |
50 | for _, f := range files { |
50 | for _, f := range files { |
51 | fmt.Println (curdir2 + "/" + f.Name()) |
51 | fmt.Println (curdir2 + "/" + f.Name()) |
52 | //fmt.Println(f.Name()) |
52 | //fmt.Println(f.Name()) |
53 | fi, err := os.Stat(f.Name()) |
53 | fi, err := os.Stat(f.Name()) |
54 | if err != nil { |
54 | if err != nil { |
55 | log.Fatal(err) |
55 | log.Fatal(err) |
56 | } |
56 | } |
57 | mode := fi.Mode(); |
57 | mode := fi.Mode(); |
58 | if mode.IsDir() { |
58 | if mode.IsDir() { |
59 | traverse(fi.Name()) |
59 | traverse(fi.Name()) |
60 | } |
60 | } |
61 | } |
61 | } |
62 | os.Chdir(curdir) |
62 | os.Chdir(curdir) |
63 | } |
63 | } |
64 | 64 | ||
65 | func start_traverse(s string) { |
65 | func start_traverse(s string) { |
66 | fmt.Println(s) |
66 | fmt.Println(s) |
67 | traverse(s) |
67 | traverse(s) |
68 | } |
68 | } |
69 | 69 | ||
70 | // ============================================================================= |
70 | // ============================================================================= |
71 | // MAIN |
71 | // MAIN |
72 | // ============================================================================= |
72 | // ============================================================================= |
73 | 73 | ||
74 | func main() { |
74 | func main() { |
75 | progname := os.Args[0] |
75 | progname := os.Args[0] |
76 | if len(os.Args) == 2 { |
76 | if len(os.Args) == 2 { |
77 | directory := os.Args[1] |
77 | directory := os.Args[1] |
78 | fi, err := os.Stat(directory) |
78 | fi, err := os.Stat(directory) |
79 | if err != nil { |
79 | if err != nil { |
80 | _ = err // ugly hack to allow not using the err variable |
80 | _ = err // ugly hack to allow not using the err variable |
81 | Usage(progname, "ERROR: Not a directory : " + directory) |
81 | Usage(progname, "ERROR: Not a directory : " + directory) |
82 | } |
82 | } |
83 | if fi.IsDir() { |
83 | if fi.IsDir() { |
84 | start_traverse(directory) |
84 | start_traverse(directory) |
85 | } else { |
85 | } else { |
86 | Usage(progname, "ERROR: Not a directory : " + directory) |
86 | Usage(progname, "ERROR: Not a directory : " + directory) |
87 | } |
87 | } |
88 | } else { |
88 | } else { |
89 | Usage(progname, "ERROR: 1 argument expected") |
89 | Usage(progname, "ERROR: 1 argument expected") |
90 | } |
90 | } |
91 | } |
91 | } |