Linux终端下简单的登录程序 密码不回显
来源:互联网 发布:php商城视频教程 编辑:程序博客网 时间:2024/06/03 19:11
在Linux进行登录是输入密码不会被回显,所以我也写了个简单的登入程序,使得在输入密码时不再进行回显。
#include <stdio.h>#include <stdlib.h>#include <termios.h>#include <unistd.h>#include <errno.h>#include <string.h>#include <stdbool.h>#define USER_NAME "username"#define USER_PASS "userpass"#define MAX 20#define ECHOFLAGS (ECHO | ECHOE | ECHOK)int set_disp_mode(int fd, int option) { int err; struct termios term; if (tcgetattr(fd, &term) == -1) { fprintf(stderr, "Can\'t get the attribution of the terminal!\n"); exit(1); } if (option) { term.c_lflag |= ECHOFLAGS; } else { term.c_lflag &= ~ECHOFLAGS; } err = tcsetattr(fd, TCSAFLUSH, &term); if (err == -1 && err == EINTR) { fprintf(stderr, "Can\'t set the attribution of the terminal!\n"); exit(1); } return 0;}int get_passwd(char* passwd, int size) { int c; int n = 0; fprintf(stderr, "Please input password:"); do { c = getchar(); if (c != '\n' && c != '\r') { passwd[n++] = c; } } while (c != '\n' && c != '\r' && n < (size-1)); passwd[n] = '\0'; return n;}bool check_login(const char* name, const char* passwd) { int nlen = strlen(USER_NAME); int plen = strlen(USER_PASS); return (strncmp(name, USER_NAME, nlen) == 0) && (strncmp(passwd, USER_PASS, plen) == 0);}int main(void) { char name[MAX]; char passwd[MAX]; LOGIN: fprintf(stderr, "login user:"); fgets(name, MAX, stdin); name[strlen(name)-1] = '\0'; set_disp_mode(STDIN_FILENO, 0); get_passwd(passwd, MAX); if (check_login(name, passwd)) { fprintf(stderr, "\nHello %s, Welcome!\n", name); } else { fprintf(stderr, "\n"); set_disp_mode(STDIN_FILENO, 1); goto LOGIN; } set_disp_mode(STDIN_FILENO, 1); return 0;}
0 0
- Linux终端下简单的登录程序 密码不回显
- Linux下基于C的简单终端聊天程序
- Linux下用C语言实现简单的密码登录
- linux下的xampp安装及mysql终端登录
- LINUX的终端登录过程
- Linux终端下输入密码显示星号
- linux下终端自动登录ssh服务器
- linux终端下飞鸽传书(飞秋)协议的简单实现
- linux下rsh的无密码登录配置
- linux下rsh的无密码登录配置
- Python实现Linux下SSh的无密码登录
- Linux下phpmyadmin忘记root的登录密码,找回方法
- 简单的登录程序
- 查看、关闭登录到linux的终端
- Linux下SSH免密码登录
- Linux下SSH免密码登录
- Linux下配置SSH免密码登录
- linux下ssh无密码登录
- CentOS安装Prolog和Erlang语言
- Linux系统下加载U盘设备时文件乱码的有效解决方法
- RabbitMq在ubuntu的安装
- 利用Lua实现二叉查找树并进行各种遍历
- 我做自媒体的思维
- Linux终端下简单的登录程序 密码不回显
- Python文件夹与文件的操作实现代码
- 已知后序遍历和中序遍历求解前序遍历
- 关于ssh2上传文件带进度遇到问题
- CentOS删除Applications中的菜单项
- 在RowCommand事件中获取索引值示例代码
- CodeStatistics
- Android 渠道包的打包及管理
- 解决Linux下Svn检出Windows SVN服务器上项目SSL handshake failed: SSL error: Key usage violation in certificate ha