Skip to content

Commit 1220f23

Browse files
committed
from grep from commands/syscall.go to util/grep.go
1 parent a9ae7d4 commit 1220f23

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

commands/syscall.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package commands
22

33
import (
4-
"bufio"
54
"fmt"
65
"log"
7-
"os"
8-
"regexp"
96
"ret/theme"
7+
"ret/util"
108
)
119

1210
func syscallHelp() {
@@ -26,26 +24,6 @@ func syscallHelp() {
2624
fmt.Printf(" 🔗 " + theme.ColorGray + "https://github.com/rerrorctf/ret/blob/main/commands/syscall.go" + theme.ColorReset + "\n")
2725
}
2826

29-
func grep(path string, pattern string) {
30-
file, err := os.Open(path)
31-
if err != nil {
32-
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": opening file %v\n", path)
33-
}
34-
defer file.Close()
35-
36-
scanner := bufio.NewScanner(file)
37-
for scanner.Scan() {
38-
line := scanner.Text()
39-
if match, _ := regexp.MatchString(pattern, line); match {
40-
fmt.Println(line)
41-
}
42-
}
43-
44-
if err := scanner.Err(); err != nil {
45-
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": %v\n", err)
46-
}
47-
}
48-
4927
func Syscall(args []string) {
5028
if len(args) > 0 {
5129
switch args[0] {
@@ -68,19 +46,19 @@ func Syscall(args []string) {
6846
switch arch {
6947
case "x86":
7048
{
71-
grep("/usr/include/x86_64-linux-gnu/asm/unistd_32.h", pattern)
49+
util.Grep("/usr/include/x86_64-linux-gnu/asm/unistd_32.h", pattern)
7250
}
7351
case "32":
7452
{
75-
grep("/usr/include/x86_64-linux-gnu/asm/unistd_32.h", pattern)
53+
util.Grep("/usr/include/x86_64-linux-gnu/asm/unistd_32.h", pattern)
7654
}
7755
case "x64":
7856
{
79-
grep("/usr/include/x86_64-linux-gnu/asm/unistd_64.h", pattern)
57+
util.Grep("/usr/include/x86_64-linux-gnu/asm/unistd_64.h", pattern)
8058
}
8159
case "64":
8260
{
83-
grep("/usr/include/x86_64-linux-gnu/asm/unistd_64.h", pattern)
61+
util.Grep("/usr/include/x86_64-linux-gnu/asm/unistd_64.h", pattern)
8462
}
8563
default:
8664
{

util/grep.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package util
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
"regexp"
9+
"ret/theme"
10+
)
11+
12+
func Grep(path string, pattern string) {
13+
file, err := os.Open(path)
14+
if err != nil {
15+
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": opening file %v\n", path)
16+
}
17+
defer file.Close()
18+
19+
scanner := bufio.NewScanner(file)
20+
for scanner.Scan() {
21+
line := scanner.Text()
22+
if match, _ := regexp.MatchString(pattern, line); match {
23+
fmt.Println(line)
24+
}
25+
}
26+
27+
if err := scanner.Err(); err != nil {
28+
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": %v\n", err)
29+
}
30+
}

0 commit comments

Comments
 (0)