forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.go
141 lines (125 loc) · 3.18 KB
/
ls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2013-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// ls prints the contents of a directory.
//
// Synopsis:
// ls [OPTIONS] [DIRS]...
//
// Options:
// -l: long form
// -Q: quoted
// -R: equivalent to findutil's find
// -F: append indicator (one of */=>@|) to entries
//
// Bugs:
// With the `-R` flag, directories are only ever printed once.
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"text/tabwriter"
flag "github.com/spf13/pflag"
"github.com/u-root/u-root/pkg/ls"
)
var (
all = flag.BoolP("all", "a", false, "show hidden files")
human = flag.BoolP("human-readable", "h", false, "human readable sizes")
directory = flag.BoolP("directory", "d", false, "list directories but not their contents")
long = flag.BoolP("long", "l", false, "long form")
quoted = flag.BoolP("quote-name", "Q", false, "quoted")
recurse = flag.BoolP("recursive", "R", false, "equivalent to findutil's find")
classify = flag.BoolP("classify", "F", false, "append indicator (one of */=>@|) to entries")
)
func listName(stringer ls.Stringer, d string, w io.Writer, prefix bool) error {
return filepath.Walk(d, func(path string, osfi os.FileInfo, err error) error {
// Soft error. Useful when a permissions are insufficient to
// stat one of the files.
if err != nil {
log.Printf("%s: %v\n", path, err)
return nil
}
fi := ls.FromOSFileInfo(path, osfi)
if *recurse {
// Mimic find command
fi.Name = path
} else if path == d {
if *directory {
fmt.Fprintln(w, stringer.FileString(fi))
return filepath.SkipDir
}
// Starting directory is a dot when non-recursive
if osfi.IsDir() {
fi.Name = "."
if prefix {
if *quoted {
fmt.Fprintf(w, "%q:\n", d)
} else {
fmt.Fprintf(w, "%v:\n", d)
}
}
}
}
// Hide .files unless -a was given
if *all || fi.Name[0] != '.' {
// Print the file in the proper format.
if *classify {
fi.Name = fi.Name + indicator(fi)
}
fmt.Fprintln(w, stringer.FileString(fi))
}
// Skip directories when non-recursive.
if path != d && fi.Mode.IsDir() && !*recurse {
return filepath.SkipDir
}
return nil
})
}
func indicator(fi ls.FileInfo) string {
if fi.Mode.IsRegular() && fi.Mode&0111 != 0 {
return "*"
}
if fi.Mode&os.ModeDir != 0 {
return "/"
}
if fi.Mode&os.ModeSymlink != 0 {
return "@"
}
if fi.Mode&os.ModeSocket != 0 {
return "="
}
if fi.Mode&os.ModeNamedPipe != 0 {
return "|"
}
return ""
}
func main() {
flag.Parse()
// Write output in tabular form.
w := &tabwriter.Writer{}
w.Init(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()
var s ls.Stringer = ls.NameStringer{}
if *quoted {
s = ls.QuotedStringer{}
}
if *long {
s = ls.LongStringer{Human: *human, Name: s}
}
// Array of names to list.
names := flag.Args()
if len(names) == 0 {
names = []string{"."}
}
// Is a name a directory? If so, list it in its own section.
prefix := len(names) > 1
for _, d := range names {
if err := listName(s, d, w, prefix); err != nil {
log.Printf("error while listing %#v: %v", d, err)
}
w.Flush()
}
}