Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>
  1. inputpass
  2. 반향되지 않는 사용자 패스워드 받아들이기
  3. yundream
  4. Version 0.1
  5. 2004/01/16

설명

터미널에서 패스워드를 입력받을 때 패스워드 내용이 반향이 되면 안됩니다. 이 때 사용할 수 있는 간단한 함수입니다. termios의 터미널 제어 함수들을 이용해서 작성했습니다.

사용방법

int main()
{
    printf("you input is (%s)\n", inputpass());
    return 0;
}

코드

#include <termios.h>
#include <stdio.h>

#define MAX_PASS_LEN    16 
static char passwd[MAX_PASS_LEN];
static char *inputpass(void);

static char *inputpass(void)
{
    char temp;
    int i = 0;
    struct termios old, new;

    tcgetattr(fileno(stdin), &old);
    new = old;
    // 반향을 제거한다.
    new.c_lflag =~ ECHO;
    tcsetattr(fileno(stdin), TCSAFLUSH, &new);

    printf("password ");
    while((temp=getchar()) != '\n')
    {
        if (i == MAX_PASS_LEN) break;
        passwd[i] = temp;
        i++;
    }
    // 원래 터미널 상태로 되돌린다.
    tcsetattr(fileno(stdin), TCSANOW, &old);
    return passwd;
}

변경사항

참고문헌

  1. http://www.joinc.co.kr/modules.php?name=News&file=article&sid=101 터미널제어

2004/01/08