﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-flat world,composite life</title><link>http://www.cppblog.com/sei-zhouyu/</link><description>enjoy my life</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 19:03:05 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 19:03:05 GMT</pubDate><ttl>60</ttl><item><title>linux下接收用户输入密码的完美实现 </title><link>http://www.cppblog.com/sei-zhouyu/archive/2007/07/09/27748.html</link><dc:creator>pujing</dc:creator><author>pujing</author><pubDate>Mon, 09 Jul 2007 09:09:00 GMT</pubDate><guid>http://www.cppblog.com/sei-zhouyu/archive/2007/07/09/27748.html</guid><wfw:comment>http://www.cppblog.com/sei-zhouyu/comments/27748.html</wfw:comment><comments>http://www.cppblog.com/sei-zhouyu/archive/2007/07/09/27748.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/sei-zhouyu/comments/commentRss/27748.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sei-zhouyu/services/trackbacks/27748.html</trackback:ping><description><![CDATA[<p>一个小问题，在linux编一个接收用户输入密码小程序，不显示密码。<br>google和baidu了一下，竟然没有找到现成的，好吧，自己编一个。哪里想到，<br>竟然一波三折。哈哈，最终还是搞定了！<br>1) 很容易根据termios的结构屏蔽终端属性的输出。<br>但是，这样一来，用户的输入不显示在屏幕上。用户不知道自己输入的个数。<br>对输入的内容心里也没有底。非常不方便。<br>2）于是改为一个一个字符的处理格式。编程实现了用'*'代替用户的输入。<br>但是这样linux处于非授权模式，一个限制是&#8216;退格&#8217;键不能用。用户必须保证一次<br>输入正确，万一错了的话，只能眼睁睁的重新运行程序，重来一次。<br>3）我最终在2）的基础上，实现了用'*'代替用户的输入，并且backspace key可用。<br><br>附代码：</p>
<p>#include &lt;termio.h&gt;<br>#include &lt;stdio.h&gt;</p>
<p>#define passLength 100&nbsp; </p>
<p>int main(int argc, char **argv)<br>{<br>&nbsp;&nbsp;&nbsp; struct termio tio, tin;<br>&nbsp;&nbsp;&nbsp; char*password =(char*)malloc(passLength); <br>&nbsp;&nbsp;&nbsp; char*b=password;<br>&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; ioctl(0, TCGETA, &amp;tio);<br>&nbsp;&nbsp;&nbsp; tin = tio;<br>&nbsp;&nbsp;&nbsp; tin.c_lflag &amp;= ~ECHO;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* turn off ECHO&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; tin.c_lflag &amp;= ~ICANON;&nbsp;&nbsp;&nbsp; /* turn off ICANON */<br>&nbsp;&nbsp;&nbsp;&nbsp;tin.c_lflag &amp;= ~ISIG;<br>&nbsp;&nbsp;&nbsp;&nbsp;tin.c_cc[VINTR]=1;<br>&nbsp;&nbsp;&nbsp; tin.c_cc[VMIN]=1;<br>&nbsp;&nbsp;&nbsp; tin.c_cc[VTIME]=0;<br>&nbsp;&nbsp;&nbsp;&nbsp;/*<br>&nbsp;&nbsp;&nbsp;&nbsp; * Set the new modes.&nbsp; Again we ignore return<br>&nbsp;&nbsp;&nbsp;&nbsp; * values.<br>&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; ioctl(0,TCSETA,&amp;tin);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; char selected;<br>&nbsp;&nbsp;&nbsp; int order=0;<br>&nbsp;&nbsp;&nbsp; printf("Enter password:");<br>&nbsp;&nbsp;&nbsp;&nbsp;do{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; selected =fgetc(stdin);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if((selected=='\b')&amp;&amp;(order&gt;0))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; fputc('\b',stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc(' ',stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('\b',stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order--;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;password--;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *password='\0';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if((selected!='\n')&amp;&amp;(selected!='\r')&amp;&amp;(selected!='\b')){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *password++=selected;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputc('*',stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fflush(stdout);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}while ((selected!='\n')&amp;&amp;(selected!='\r')&amp;&amp;(order&gt;=0)&amp;&amp;(order&lt;passLength));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp; /*<br>&nbsp;&nbsp;&nbsp; * Reset the old tty modes.<br>&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; ioctl(0, TCSETA, &amp;tio);<br>&nbsp;&nbsp;&nbsp; fprintf(stdout,"\nYou entered: %s\n",b);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;free(b);<br>&nbsp;&nbsp;&nbsp; exit(0);</p>
<p>}<br></p>
<img src ="http://www.cppblog.com/sei-zhouyu/aggbug/27748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sei-zhouyu/" target="_blank">pujing</a> 2007-07-09 17:09 <a href="http://www.cppblog.com/sei-zhouyu/archive/2007/07/09/27748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>