Debian Codegraph @

shellcgo.go

raw symbols SCIP
1 //go:build (cgo && aix) || (cgo && android) || (cgo && darwin) || (cgo && dragonfly) || (cgo && freebsd) || (cgo && illumos) || (cgo && linux) || (cgo && netbsd) || (cgo && openbsd) || (cgo && solaris)
2 // +build cgo,aix cgo,android cgo,darwin cgo,dragonfly cgo,freebsd cgo,illumos cgo,linux cgo,netbsd cgo,openbsd cgo,solaris
3
4 package shell
5
6 // #cgo solaris CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1
7 // #cgo illumos CFLAGS: -D_POSIX_PTHREAD_SEMANTICS=1
8 // #include <errno.h>
9 // #include <pwd.h>
10 // #include <stdlib.h>
11 // #include <sys/types.h>
12 // #include <unistd.h>
13 import "C"
14
15 import (
16 "unsafe"
17 )
18
19 func cgoGetUserShell(name string) (string, bool) {
20 buflen := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
21 if buflen == -1 {
22 buflen = 1024
23 }
24
25 for {
26 var (
27 cName = C.CString(name)
28 pwd C.struct_passwd
29 buf = make([]byte, buflen)
30 result *C.struct_passwd
31 )
32 //nolint:gocritic
33 rc := C.getpwnam_r(cName, &pwd, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(buflen), &result)
34 C.free(unsafe.Pointer(cName))
35
36 switch rc {
37 case 0:
38 return C.GoString(result.pw_shell), true
39 case C.ERANGE:
40 buflen *= 2
41 default:
42 return "", false
43 }
44 }
45 }