📄 keyio.c
字号:
/* dftp: FTP client example * Copyright (c) 1996 by Donald C. Asonye * Email: donald@uh.edu * * keyio.c. * * This and other files are hereby released to the public for * educational purposes. You may redistribute this and other files * or routines as long as you are not compensated for it, and as * long as this notice is included with it. You may not use the * released source codes in anyway commercial without permission. * * It'd be nice if you share your modifications with me and everyone * else. I encourage you to make the code better as I am very busy. * * Share your knowledge :) */#include <stdio.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#if (!defined( _WIN32 ) || !defined( WIN32 )) #include <unistd.h>#include <sys/ioctl.h>#ifdef TERMIOS#include <termios.h>#else#ifdef SGTTYB#include <sgtty.h>#else#include <termio.h>#endif /* SGTTYB */#endif /* !TERMIOS */#else#include <conio.h>#endif /* !_WIN32 */#define PASSWORD_LENGTH 256#if (!defined( _WIN32 ) || !defined( WIN32 )) /* we don't need this on PC */#ifdef TERMIOS struct termios oldtty, noecho;#else#ifdef SGTTYB struct sgttyb oldtty, noecho;#else struct termio oldtty, noecho;#endif#endif /* !_WIN32 *//* * EchoOff * turn off echoing. we call this function when we want to read * the password. (only for UNIX) */void EchoOff(){ int fd = fileno(stdin); /* * first, get the current settings. * then turn off the ECHO flag. */#ifdef TERMIOS if (tcgetattr(fd, &oldtty) < 0) perror("tcgetattr"); noecho = oldtty; noecho.c_lflag &= ~ECHO;#else# ifdef SGTTYB if (ioctl(fd, TIOCGETP, &oldtty) < 0) perror("ioctl"); noecho = oldtty; noecho.sg_flags &= ~ECHO;# else if (ioctl(fd, TCGETA, &oldtty) < 0) perror("ioctl"); noecho = oldtty; noecho.c_lflag &= ~ECHO;# endif#endif /* * now set the current tty to this setting */#ifdef TERMIOS if (tcsetattr(fd, TCSAFLUSH, &noecho) < 0) perror("tcsetattr");#else#ifdef SGTTYB if (ioctl(fd, TIOCSETP, &noecho) < 0) perror("ioctl");#else if (ioctl(fd, TCSETA, &noecho) < 0) perror("ioctl");#endif#endif /* !TERMIOS */}/* * EchoOn * turn echoing back on. (UNIX only ) */void EchoOn(){ int fd = fileno(stdin); /* * set tty back to it's original state */#ifdef TERMIOS if (tcsetattr(fd, TCSAFLUSH, &oldtty) < 0) perror("tcsetattr");#else#ifdef SGTTYB if (ioctl(fd, TIOCSETP, oldtty) < 0) perror("ioctl");#else if (ioctl(fd, TCSETA, &oldtty) < 0) perror("ioctl");#endif#endif /* !TERMIOS */}#endif /* !_WIN32 *//* * GetPassword * read in the user's password. turn off echoing before reading. * then turn echoing back on. */void GetPassword( char *szPass ){ #if (!defined( _WIN32 ) || !defined( WIN32 )) EchoOff(); (void)printf("Password:"); (void)fgets(szPass,PASSWORD_LENGTH,stdin); (void)strtok(szPass,"\n"); EchoOn(stdin,1);#else /* * for WIN32 we just use getch() instead of getche() */ int i=0; char ch=0; (void)printf("Password:"); while( (i < PASSWORD_LENGTH) && ( ch != '\r') ) { ch = getch(); if( ch != '\r' && ch != '\n') szPass[i++] = ch; }#endif (void)printf("\r\n");}/* * GetUnixInput * function called to get user input on the UNIX side. */int GetUnixInput(char *command){#if (!defined( _WIN32 ) || !defined( WIN32 )) char ch; int i; /* * ignore leading whitespace */ while( (ch=getchar()) == ' ' || ch == '\t') ; if( ch != '\n') { command[0] = ch; fgets(&command[1],1024,stdin); strtok(command,"\n"); i = strlen(command) - 1; while( i>0 && isspace(command[i])) i--; if( i>= 0) command[i+1] = 0; } return 1;#else return 0;#endif}/* * GetWin32Input * called to get input on the WIN32 side. */int GetWin32Input( char *command){#if (defined( _WIN32 ) || defined( WIN32 )) char ch; static int i=0; /* * i, above, is static because this function can be called * many times before '\r' is pressed. we set i=0 when * '\r' is encountered. */ while( kbhit() ) { if( (ch=getch()) == 0) getch(); /* ignore */ else { if( ch == '\r') { command[i] = 0; i = 0; printf("\r\n"); i = strlen(command) - 1; /* * ignore trailing whitespace */ while( i>0 && isspace(command[i])) i--; if( i>= 0) command[i+1] = 0; i = 0; return 1; } else if( ch == (char)8 && i > 0) { /* process backspace */ printf("%c %c",8,8); command[i] = 0; i--; } else if( ( ch >= 32) && ( ch <= 126) ) { printf("%c", ch); command[i++] = ch; } } } return 0;#else return 0; #endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -