C命令行密码输入 [英] C command-line password input

查看:29
本文介绍了C命令行密码输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户使用命令行界面输入密码.但我不想在屏幕上显示此密码(或显示****").

I want to allow users to enter password using command-line interface. but I don't want to display this password on screen (or display "****").

如何在C中做到这一点?谢谢.

How to do it in C? Thanks.

更新:

我只在 Linux 上工作.所以我实际上并不关心 Win 或其他系统.我尝试了 Lucas 的解决方案,效果很好.不过,我还有一个问题:

I'm working on Linux only. So I don't actually care about Win or other systems. I tried Lucas' solution and it worked fine. However, I still have another question:

  1. 如果这是单个进程&单线程应用,改变termios的设置会影响不同的终端?

  1. If this is a single process & single thread app, changing setting of termios affects different terminals?

1 个进程 - 多线程,多进程 - 多线程怎么样?

How about 1 process - multi threads, multi processes - multi threads?

非常感谢.

推荐答案

如果您的系统提供,getpass 是一个选项:

If your system provides it, getpass is an option:

#include <unistd.h>
/* ... */
char *password = getpass("Password: ");

这不会在输入字符时显示任何内容.

This will not display anything as characters are typed.

这篇关于C命令行密码输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆