1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
/**************************************************************************************************
$Header: /pub/cvsroot/yencode/lib/input.c,v 1.2 2002/03/10 16:22:19 bboy Exp $
Copyright (C) 2002 Don Moore <bboy@bboy.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at Your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**************************************************************************************************/
#include "misc.h"
#ifndef PASS_MAX
#define PASS_MAX 128
#endif
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
PASSINPUT
Prompts user to input password if it's necessary.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
char *
passinput(const char *prompt)
{
static char buf[PASS_MAX + 1];
char *ptr;
sigset_t sig, sigsave;
struct termios term, termsave;
FILE *fp;
int c;
if (!isatty(STDIN_FILENO))
return (NULL);
if (!(fp = fopen(ctermid(NULL), "r+")))
return (NULL);
setbuf(fp, NULL);
sigemptyset(&sig);
sigaddset(&sig, SIGINT);
sigaddset(&sig, SIGTSTP);
sigprocmask(SIG_BLOCK, &sig, &sigsave);
tcgetattr(fileno(fp), &termsave);
term = termsave;
term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
tcsetattr(fileno(fp), TCSAFLUSH, &term);
fputs(prompt, fp);
ptr = buf;
while ((c = getc(fp)) != EOF && c != '\n')
{
if (ptr < &buf[PASS_MAX])
*ptr++ = c;
}
*ptr = 0;
putc('\n', fp);
tcsetattr(fileno(fp), TCSAFLUSH, &termsave);
sigprocmask(SIG_SETMASK, &sigsave, NULL);
fclose(fp);
return (buf);
}
/*--- passinput() -------------------------------------------------------------------------------*/
/* vi:set ts=3: */
|