콘솔에서 scanf로 입력값 받을 때 * 로 나오게 하려면..

익명 사용자의 이미지

콘솔에서 scanf로 입력값 받을 때 * 로 나오게 하려면 어떠한 방법을 써
야 할까요.. ㅡㅡ;

감사합니다. ^^

익명 사용자의 이미지

터미널을 제어하는 함수가 유닉스에서 다음과 같이 제공됩니다.

#include

struct termios {
....
}

int tcgetattr( ... );
int tcsetattr( ... )
int tcdrain( ... );
int tcflow( ... );
int tcflush( ... );

위 함수를 이용하면 터미널을 제어할 수 있습니다.

터미널에 echo 하지 않도록 하고,
한글자씩 처리하도록 합니다.
fgetc() 종류의 함수로 글자를 입력받습니다.
fputc() 종류의 함수로 '*'를 출력합니다.
이것을 입력의 끝은 알리는 키 또는 시그널까지 반복합니다.
터미널을 원상복구 시킴니다.

위 과정을 통해 얻은 스트링을 sscanf() 함수를 이용합니다.

소스의 예는 다음과 같습니다.

/*
Beginning Linux Programming 에서 터미널 부분에 나온 소스를
수정해서 적었습니다.
*/

#include
#include
#include

#define PASSWORD_LEN 8

int main()
{
struct termios initialrsettings, newrsettings;
char password[PASSWORD_LEN + 1];
int arrayindex;
char inputchar;

/*
표준 입력으로부터 현재 설정치를 구하고, 앞에서 생성한 termios 구조체 내로 복사한다.
*/
tcgetattr(fileno(stdin), &initialrsettings);

/*
나중에 대체하기 위해 원래 설정치를 복사해둔다.
nowrsettings에서 반향과 정규모드를 취소하는 구조체를 생성하고,
사용자 에게 암호를 요청한다.
*/
newrsettings = initialrsettings;
newrsettings.c_lflag &= ~ECHO & ~ICANON;

printf("Enter password ");

/*
newrsettings로 터미널 속성을 설정하고
암호를 읽어들이고
속성을 원래 설정치로 복구한다.
*/
if(tcsetattr(fileno(stdin), TCSANOW, &newrsettings) != 0) {
fprintf(stderr, "Could not set attributes\n");
}
else {
for(arrayindex = 0; arrayindex < PASSWORD_LEN; arrayindex++) {
if((inputchar = fgetc(stdin)) == '\n')
break;

if((inputchar >= '!') && (inputchar <= '~')) { /* 알파벳과 특수문자만 허용한다 */
password[arrayindex] = inputchar;
fputc('*', stdout);
}
else
continue;
}
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
password[arrayindex] = '\0';
}
/*
sscanf를 이용한다.
sscanf(password, format_string, ... );
*/

fprintf(stdout, "\nYou entered %s\n", password); /* 제대로 작동하는제 체크합니다. */
exit(0);
}

익명 사용자의 이미지

감사합니다.

사실 저도 termio쪽으로 생각했지만
예전에 시리얼로 할 때 생각뿐이라서 read와 write가 없이 어떻게 적용할지 막막했었는데..

패스워드 뿐 아니라 다른쪽에서의 문제점도 실마리를 얻게 되었습니다.

제 책상에 스티브 아저씨의 책이 놓여져 있건만 눈뜬 장님이 따로 없군요..

감사합니다. 좋은 하루 보내세요 ^^

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.