blob: c155d2964f2d398d5796809e303bfcafd5268589 (
plain)
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
|
package hackers_git
import (
"syscall"
"unsafe"
)
//#define _XOPEN_SOURCE /* for getgrnam_r(3) in grp.h */
//#include <stdlib.h> /* for free(3) */
//#include <unistd.h> /* for sysconf(3) */
//#include <grp.h> /* for getgrnam_r(3) */
import "C"
func name2gid(name string) (gid int32) {
nameC := C.CString(name)
defer C.free(unsafe.Pointer(nameC))
gid = -1
buflen := C.sysconf(C._SC_GETGR_R_SIZE_MAX)
if buflen < 1 {
buflen = 1024
}
buf := make([]byte, buflen)
var grp C.struct_group
var ret *C.struct_group
for {
success, errno := C.getgrnam_r(nameC, &grp, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &ret)
if success >= 0 {
break
}
if errno == syscall.ERANGE {
buflen += 256
buf = make([]byte, buflen)
} else {
return
}
}
if ret != nil {
gid = int32(ret.gr_gid)
}
return
}
|