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