shared/util_linux_cgo.go
func UserId(name string) (int, error) {
	var pw C.struct_passwd
	var result *C.struct_passwd

	bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
	if bufSize < 0 {
		bufSize = 4096
	}

	buf := C.malloc(C.size_t(bufSize))
	if buf == nil {
		return -1, fmt.Errorf("allocation failed")
	}
	defer C.free(buf)

	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

again:
	rv, errno := C.getpwnam_r(cname,
		&pw,
		(*C.char)(buf),
		C.size_t(bufSize),
		&result)
	if rv < 0 {
		// OOM killer will take care of us if we end up doing this too
		// often.
		if errno == syscall.ERANGE {
			bufSize *= 2
			tmp := C.realloc(buf, C.size_t(bufSize))
			if tmp == nil {
				return -1, fmt.Errorf("allocation failed")
			}
			buf = tmp
			goto again
		}
		return -1, fmt.Errorf("failed user lookup: %s", syscall.Errno(rv))
	}

	if result == nil {
		return -1, fmt.Errorf("unknown user %s", name)
	}

	return int(C.int(result.pw_uid)), nil
}
func UserId(name string) (int, error) {
	var pw C.struct_passwd
	var result *C.struct_passwd

	bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
	if bufSize < 0 {
		bufSize = 4096
	}

	buf := C.malloc(C.size_t(bufSize))
	if buf == nil {
		return -1, fmt.Errorf("allocation failed")
	}
	defer C.free(buf)

	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

again:
	rv, errno := C.getpwnam_r(cname,
		&pw,
		(*C.char)(buf),
		C.size_t(bufSize),
		&result)
	if rv < 0 {
		// OOM killer will take care of us if we end up doing this too
		// often.
		if errno == syscall.ERANGE {
			bufSize *= 2
			tmp := C.realloc(buf, C.size_t(bufSize))
			if tmp == nil {
				return -1, fmt.Errorf("allocation failed")
			}
			buf = tmp
			goto again
		}
		return -1, fmt.Errorf("failed user lookup: %s", syscall.Errno(rv))
	}

	if result == nil {
		return -1, fmt.Errorf("unknown user %s", name)
	}

	return int(C.int(result.pw_uid)), nil
}
func UserId ( name string ) ( int , error ) { var pw C . struct_passwd var result * C . struct_passwd bufSize := C . sysconf ( C . _SC_GETPW_R_SIZE_MAX ) if bufSize < 0 { bufSize = 4096 } buf := C . malloc ( C . size_t ( bufSize ) ) if buf == nil { return - 1 , fmt . Errorf ( " " ) } defer C . free ( buf ) cname := C . CString ( name ) defer C . free ( unsafe . Pointer ( cname ) ) again : rv , errno := C . getpwnam_r ( cname , & pw , ( * C . char ) ( buf ) , C . size_t ( bufSize ) , & result ) if rv < 0 { // OOM killer will take care of us if we end up doing this too // often. if errno == syscall . ERANGE { bufSize *= 2 tmp := C . realloc ( buf , C . size_t ( bufSize ) ) if tmp == nil { return - 1 , fmt . Errorf ( " " ) } buf = tmp goto again } return - 1 , fmt . Errorf ( " " , syscall . Errno ( rv ) ) } if result == nil { return - 1 , fmt . Errorf ( " " , name ) } return int ( C . int ( result . pw_uid ) ) , nil }
// UserId is an adaption from https://codereview.appspot.com/4589049.
UserId is an adaption from https : // codereview . appspot . com / 4589049 .