package hackers_git import ( "syscall" "unsafe" ) //#define _XOPEN_SOURCE /* for getgrnam_r(3) in grp.h */ //#include /* for free(3) */ //#include /* for sysconf(3) */ //#include /* 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 }