c - C:在 Linux 上显示一个系统通用对话框请求密码
问题描述
我在 Debian Linux 操作系统上,用 C 语言开发文件保护应用程序。我试图在执行某些代码之前向用户请求密码以进行身份验证。我希望提供一个系统通用密码窗口(即,在更新安装或挂载文件系统期间更新管理器要求我们输入根密码时出现的一次)。我尝试使用 shell 命令从我systemd-aask-password
的C 代码运行以请求密码。但是,这两个命令都在 shell 中工作,而不是通过桌面启动器触发时。dialog --passwordbox
popen()
有没有办法通过出现在外壳外面的对话框来请求密码?该方法可以使用 shelll-script、python、perl 或通用 C 代码,以便我可以与现有的 C 程序集成。
解决方案
您可以尝试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;
}
推荐阅读
- xamarin - 为什么 Xamarin.Forms 应用不能在 Chromebook 上运行?
- javascript - 在不通过量角器测试的情况下捕获错误(等待元素可见)
- javascript - PHP 多读少读 - Magento 2
- c++ - GDI+ - 不能用 Gdiplus::Graphics (C++) 做任何事情
- css - 简化的 ID 和子组合器
- r - R读取格式错误的csv,字段中的引号和分隔符不均匀
- c# - Unity RigidBody2D悬停在某些计算机上不起作用
- android - 发布应用程序总是在闪屏上崩溃
- c++ - C++ - 使用变量指针调用函数
- regex - python regex '\\\\' 如何评估?