-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
78 lines (62 loc) · 1.5 KB
/
run.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"bytes"
"fmt"
"os"
"strings"
)
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
func writeslashes(buf *bytes.Buffer, n int) {
buf.WriteString(strings.Repeat(`\`, n))
}
func quoteargs(args []string) string {
var res bytes.Buffer
if len(args) == 0 {
return ""
}
for _, arg := range args {
if arg != "" && !strings.ContainsAny(arg, " \t\n\v\"") {
res.WriteString(arg + " ")
continue
}
res.WriteString(`"`)
for i := 0; ; i++ {
nslashes := 0
for ; i < len(arg) && arg[i] == '\\'; i++ {
nslashes++
}
if i == len(arg) {
writeslashes(&res, nslashes*2)
break
} else {
if arg[i] == '"' {
writeslashes(&res, nslashes*2+1)
} else {
writeslashes(&res, nslashes)
}
res.WriteString(string(arg[i]))
}
}
res.WriteString(`" `)
}
// Cut out the trailing space.
res.Truncate(res.Len() - 1)
return res.String()
}
func check() {
if len(os.Args) == 0 {
fmt.Fprintf(os.Stderr, "usudo needs a program to run!\n")
os.Exit(1)
}
}
func run(exe string, unquoted_args []string) {
args := quoteargs(unquoted_args)
err := ShellExecute("runas", exe, args)
if err != nil {
fmt.Fprintf(os.Stderr, "error running program: %v\n", err)
os.Exit(1)
}
}