Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
67 lines (56 loc) · 1.49 KB
/
Copy pathmain.go
File metadata and controls
67 lines (56 loc) · 1.49 KB
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
package main
/*
#cgo CFLAGS: -I${SRCDIR}/../zig-out/include
// --- Shared (.so) build ---
// Build your Zig library with: zig build -Dcapi=true -Dso=true
// Then Go will link dynamically against libreplace-exe.so
// #cgo LDFLAGS: -L${SRCDIR}/../zig-out/lib -lreplace-exe
// --- Static (.a) build ---
// Alternatively, build Zig with: zig build -Dcapi=true -Dso=false
// and comment out the line above, then uncomment below to link statically
#cgo LDFLAGS: ${SRCDIR}/../zig-out/lib/libreplace-exe.a
#include <replace_exe.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
func main() {
C.init()
if len(os.Args) < 2 {
fmt.Println("Usage:")
fmt.Println(" ./demo-go delete # test self delete")
fmt.Println(" ./demo-go replace <path> # test self replace")
return
}
cmd := os.Args[1]
switch cmd {
case "delete":
fmt.Println("[Go] Calling self_delete() ...")
res := C.self_delete()
if res != 0 {
fmt.Println("[Go] self_delete failed!")
} else {
fmt.Println("[Go] Success — process may delete itself 😅")
}
case "replace":
if len(os.Args) < 3 {
fmt.Println("Usage: ./demo-go replace <path>")
os.Exit(1)
}
path := C.CString(os.Args[2])
defer C.free(unsafe.Pointer(path))
fmt.Printf("[Go] Calling self_replace(%q) ...\n", os.Args[2])
res := C.self_replace(path)
if res != 0 {
fmt.Println("[Go] self_replace failed!")
} else {
fmt.Println("[Go] Replacement succeeded!")
}
default:
fmt.Println("Unknown command:", cmd)
}
}