{"id":3991,"date":"2020-02-22T00:01:06","date_gmt":"2020-02-22T08:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3991"},"modified":"2020-02-29T08:33:41","modified_gmt":"2020-02-29T16:33:41","slug":"input-minus-the-echo","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3991","title":{"rendered":"Input Minus the Echo"},"content":{"rendered":"<p>The prompt appears in the terminal window: <code>Type your password<\/code>. As you type the password, text doesn&#8217;t appear on the screen. Yes, the program is using stream I\/O. So how do you code standard input that doesn&#8217;t output characters?<br \/>\n<!--more--><br \/>\nThe answer is brought to you by the term <em>echo<\/em>.<\/p>\n<p>The terminal window&#8217;s ancestor is the teletype machine, which is why a terminal window&#8217;s device name begins with <em>tty<\/em>. Streaming I\/O has its foundations in telecommunications and many of a terminal&#8217;s configuration options come from that domain. One of them is the echo.<\/p>\n<p>In telecommunications, when a character is sent from one device to another it can also be sent to the display. It need not be, but the user expects it. This feature is called <em>echo<\/em> or <em>local echo<\/em>. Figure 1 illustrates.<\/p>\n<div id=\"attachment_3995\" style=\"width: 594px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1.gif\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-3995\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1-1024x576.gif\" alt=\"Simplex Communications\" width=\"584\" height=\"329\" class=\"size-large wp-image-3995\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1-1024x576.gif 1024w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1-300x169.gif 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1-768x432.gif 768w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/02\/0222-figure1-500x281.gif 500w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/a><p id=\"caption-attachment-3995\" class=\"wp-caption-text\">Figure 1. In simplex communications, output text must be echoed to the display. For duplex communications, the other system returns the output character. (Click image to view animation.)<\/p><\/div>\n<p>A character received is sent to the display be default, as shown in Figure 1. But for the sending terminal, local echo is an option. In fact, remnants of this option are seen in MS-DOS batch files, with the command <code>ECHO OFF<\/code>, which suppresses echoing of commands to the display while the batch file runs.<\/p>\n<p>When you type a password in a terminal window, the programmer disables local echo. After text is input and processed,  local echo is restored. The programmer resets the echo by programming the terminal directly, but many implementation of C feature a special input function that does the same thing, <em>getpass()<\/em>.<\/p>\n<p>The <em>getpass()<\/em> function is defined in the <code>unistd.h<\/code> header file. Here is its <em>man<\/em> page format:<\/p>\n<p><code>char *getpass(const char *prompt);<\/code><\/p>\n<p>The sole argument is a <code>prompt<\/code> string, sent to standard output. The function accepts input &mdash; with local echo off &mdash; and returns a pointer to the text input. Unlike the <em>fgets()<\/em> function, <em>getpass()<\/em> strips input of the newline character. Local echo is restored after the function call.<\/p>\n<p>The maximum size of the input string varies. It may be represented by a constant value, such as <code>_PASSWORD_LEN<\/code>, which is defined in OS X in the <code>pwd.h<\/code> header file. Otherwise, most implementations set the string size to 128 characters.<\/p>\n<p>Here&#8217;s sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    char *password;\r\n\r\n    <span class=\"comments\">\/* fetch the password *\/<\/span>\r\n    password = getpass(\"Your password: \");\r\n    printf(\"Your password is '%s'\\n\",password);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>getpass()<\/em> function disables local echo and outputs the prompt. Input text is referenced by the <code>password<\/code> <em>char<\/em> pointer, then output in the <em>printf()<\/em> statement:<\/p>\n<p><code>Your password:<br \/>\nYour password is 'this is a test'<\/code><\/p>\n<p>Don&#8217;t fret if your compiler&#8217;s C library lacks the <em>getpass()<\/em> function. Many Windows compilers, such as the MinGW compiler used with Code::Blocks, lack this function. The Linux shell apps for Windows 10 do sport <em>getpass()<\/em>. Further, for any Linux\/Unix system, you can program the terminal directly to suppress local echo and craft your own password input function. I cover this process in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4001\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Normally, standard input is output to the display. When you type a password, it&#8217;s suppressed. Ever wonder how that happens? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3991\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[6,7,8,9],"class_list":["post-3991","post","type-post","status-publish","format-standard","hentry","category-main","tag-get-pass","tag-get-password","tag-input-without-echo","tag-local-echo"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3991"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3991\/revisions"}],"predecessor-version":[{"id":4019,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3991\/revisions\/4019"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}