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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c