首页 > 解决方案 > C:在 Linux 上显示一个系统通用对话框请求密码

问题描述

我在 Debian Linux 操作系统上,用 C 语言开发文件保护应用程序。我试图在执行某些代码之前向用户请求密码以进行身份​​验证。我希望提供一个系统通用密码窗口(即,在更新安装或挂载文件系统期间更新管理器要求我们输入根密码时出现的一次)。我尝试使用 shell 命令从我systemd-aask-password的C 代码运行以请求密码。但是,这两个命令都在 shell 中工作,而不是通过桌面启动器触发时。dialog --passwordboxpopen()

在此处输入图像描述

有没有办法通过出现在外壳外面的对话框来请求密码?该方法可以使用 shelll-script、python、perl 或通用 C 代码,以便我可以与现有的 C 程序集成。

标签: clinuxdialogpasswordsdebian

解决方案


您可以尝试zenity

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

int main(void)
{
    FILE *cmd;
    char result[32];

    cmd = popen("zenity --password", "r");
    if (cmd == NULL) {
        perror("popen");
        exit(EXIT_FAILURE);
    }
    if (fgets(result, sizeof(result), cmd)) {
        result[strcspn(result, "\r\n")] = 0; 
        printf("password: %s\n", result);
    }
    pclose(cmd);
    return 0;
}

推荐阅读