-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
62 lines (51 loc) · 868 Bytes
/
commands.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
package main
import (
"bufio"
"io"
"os"
)
const (
DIR = "DIR"
CD = "CD"
PWD = "PWD"
RETRIEVE = "RETR"
)
func chdir(w *bufio.Writer, s string) {
defer w.WriteString("\r\n")
if os.Chdir(s) == nil {
w.WriteString("OK")
} else {
w.WriteString("ERROR")
}
}
func pwd(w *bufio.Writer) {
defer w.WriteString("\r\n")
s, err := os.Getwd()
if err != nil {
w.WriteString("")
return
}
w.WriteString(s)
}
func dirList(w *bufio.Writer) {
defer w.WriteString("\r\n")
dir, err := os.Open(".")
if err != nil {
return
}
names, err := dir.Readdirnames(-1)
if err != nil {
return
}
for _, nm := range names {
w.WriteString(nm + "\r\n")
}
}
func retrieve(w *bufio.Writer, filename string) {
defer w.WriteString("\r\n")
file, err := os.OpenFile(filename, os.O_RDONLY, 0600)
if err != nil {
return
}
io.Copy(w, file)
}