-
Notifications
You must be signed in to change notification settings - Fork 4
/
mknod.go
105 lines (95 loc) · 1.9 KB
/
mknod.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
// Copyright © 2015-2016 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package mknod
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
"github.com/platinasystems/goes/lang"
)
type Command struct{}
func (Command) String() string { return "mknod" }
func (Command) Usage() string {
return "mknod [OPTION]... NAME TYPE [MAJOR MINOR]"
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "make block or character special files",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: `
OPTIONS
-m MAJOR`,
}
}
func (Command) Main(args ...string) error {
var filetype uint32 = 0
if len(args) == 0 {
return fmt.Errorf("FILE: missing")
}
aa := 0
for _, ar := range args {
if strings.Contains(ar, "-m=") {
aa++
}
}
l := len(args) - aa
a0 := 0 + aa
a1 := 1 + aa
a2 := 2 + aa
a3 := 3 + aa
switch args[a1] {
case "b":
filetype = syscall.S_IFBLK
case "c", "u":
filetype = syscall.S_IFCHR
case "p":
filetype = syscall.S_IFIFO
case "d":
filetype = syscall.S_IFDIR
case "r":
filetype = syscall.S_IFREG
}
filetype |= uint32(os.FileMode(flagValue(args, "m")))
nmaj := 0
nmin := 0
var err error
if l > 2 {
nmaj, err = strconv.Atoi(args[a2])
if err != nil {
return fmt.Errorf("%v", err)
}
}
if l > 3 {
nmin, err = strconv.Atoi(args[a3])
if err != nil {
return fmt.Errorf("%v", err)
}
}
n := (nmaj * 256) + nmin
err = syscall.Mknod(args[a0], filetype, n)
if err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
func flagValue(a []string, f string) uint32 {
for _, arg := range a {
if strings.Contains(arg, "-"+f+"=") {
result := strings.SplitAfter(arg, "=")
if len(result) > 1 {
i, err := strconv.ParseInt("0"+strings.TrimSpace(result[1]), 8, 32)
if err != nil {
return 0
}
return uint32(i)
}
}
}
return 0
}