dongzhong5967 2016-08-05 13:08
浏览 73
已采纳

随时调用C函数

I'm quite new on learning go language, and I start to be a great lover of this language. I hope, I will be a good gopher soon. Currently I try to call a C function to read the shadow file, my code is:

// #cgo CFLAGS: -D_POSIX_SOURCE=1
// #include <stdlib.h>
// #include <shadow.h>
// size_t size_of_shadow() { return sizeof(struct spwd); }
import "C"
import "unsafe"
import "fmt"

type Shadow struct {
    Name   string
    Passwd string
}

func Getspnam(name string) (*Shadow, error) {
    cname := C.CString(name)
    cspwd := (*C.struct_passwd)(C.malloc(C.size_of_shadow()))
    buf := (*C.char)(C.malloc(1024))
    _, err := C.getspnam_r(cname, cspwd, 1024, &cpwd)

    if unsafe.Pointer(cspwd) == unsafe.Pointer(uintptr(0)) {
        C.free(unsafe.Pointer(cname))

        if err == nil {
            err = fmt.Errorf("User %s not found", name)
        }

        return nil, err
    }

    s := Shadow{
        Name: C.GoString(cspwd.sp_namp),
        Passwd: C.GoString(cspwd.sp_pwdp),
    }

    C.free(unsafe.Pointer(cname))
    C.free(unsafe.Pointer(cspwd))
    C.free(unsafe.Pointer(buf))

    return &s, nil
}

Inspired by this little project and the documentation of the function of course:

https://github.com/LTD-Beget/passwd http://linux.die.net/man/3/getspnam

I'm on debian stretch and go 1.6 version, installed with the package manager. I got an error when I try to compile my file:

could not determine kind of name for C.getspnam_r

But when I open the header file shadow.h, the function is however present on the file.

  • 写回答

1条回答 默认 最新

  • drq9991 2016-08-05 14:41
    关注

    I fixed my mistake. The error was the used of the flag some was unnecessary and the typo on the name of the struct:

    // #include <stdlib.h>
    // #include <shadow.h>
    // size_t size_of_shadow() { return sizeof(struct spwd); }
    import "C"
    
    import "C"
    import "unsafe"
    import "fmt"
    
    type Shadow struct {
        Name   string
        Passwd string
    }
    
    func Getspnam(name string) (*Shadow, error) {
        cname := C.CString(name)
        defer C.free(unsafe.Pointer(cname))
    
        cspwd := (*C.struct_spwd)(C.malloc(C.size_of_shadow()))
        defer C.free(unsafe.Pointer(cspwd))
    
        buf := (*C.char)(C.malloc(1024))
        defer C.free(unsafe.Pointer(buf))
    
        _, err := C.getspnam_r(cname, cspwd, buf, 1024, &cspwd)
    
        if unsafe.Pointer(cspwd) == unsafe.Pointer(uintptr(0)) {
            if err == nil {
                err = fmt.Errorf("User %s not found", name)
            }
    
            return nil, err
        }
    
        s := Shadow{
            Name:   C.GoString(cspwd.sp_namp),
            Passwd: C.GoString(cspwd.sp_pwdp),
        }
    
        return &s, nil
    }
    

    The new version of the code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀