~ubuntu-branches/debian/jessie/lftp/jessie

« back to all changes in this revision

Viewing changes to src/GetPass.cc

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2004-06-01 04:01:39 UTC
  • Revision ID: james.westby@ubuntu.com-20040601040139-negt39q17jhkv3i4
Tags: upstream-3.0.5
Import upstream version 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lftp and utils
 
3
 *
 
4
 * Copyright (c) 1996-1997 by Alexander V. Lukyanov (lav@yars.free.net)
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <fcntl.h>
 
24
#include <unistd.h>
 
25
#include <termios.h>
 
26
 
 
27
#include "xmalloc.h"
 
28
#include "GetPass.h"
 
29
#include "CharReader.h"
 
30
#include "SignalHook.h"
 
31
 
 
32
char *GetPass(const char *prompt)
 
33
{
 
34
   static char *oldpass=0;
 
35
   static int tty_fd=-2;
 
36
   static FILE *f=0;
 
37
 
 
38
   xfree(oldpass);
 
39
   oldpass=0;
 
40
 
 
41
   if(tty_fd==-2)
 
42
   {
 
43
      if(isatty(0))
 
44
         tty_fd=0;
 
45
      else
 
46
      {
 
47
         tty_fd=open("/dev/tty",O_RDONLY);
 
48
         if(tty_fd!=-1)
 
49
            fcntl(tty_fd,F_SETFD,FD_CLOEXEC);
 
50
      }
 
51
   }
 
52
   if(tty_fd==-1)
 
53
      return 0;
 
54
 
 
55
   if(f==0)
 
56
      f=fdopen(tty_fd,"r");
 
57
   if(f==0)
 
58
      return 0;
 
59
 
 
60
   write(tty_fd,prompt,strlen(prompt));
 
61
 
 
62
 
 
63
   struct termios tc;
 
64
   tcgetattr(tty_fd,&tc);
 
65
   tcflag_t old_lflag=tc.c_lflag;
 
66
   tc.c_lflag&=~ECHO;
 
67
   tcsetattr(tty_fd,TCSANOW,&tc);
 
68
 
 
69
   oldpass=readline_from_file(f);
 
70
 
 
71
   tc.c_lflag=old_lflag;
 
72
   tcsetattr(tty_fd,TCSANOW,&tc);
 
73
 
 
74
   write(tty_fd,"\r\n",2);
 
75
 
 
76
   return oldpass;
 
77
}
 
78
 
 
79
char *readline_from_file(FILE *f)
 
80
{
 
81
   int   size=0x800;
 
82
   char  *line=(char*)xmalloc(size);
 
83
   int   len=0;
 
84
   char  *ptr=line;
 
85
 
 
86
   for(;;)
 
87
   {
 
88
      CharReader r(fileno(f));
 
89
      int c;
 
90
      for(;;)
 
91
      {
 
92
         SMTask::Schedule();
 
93
         c=r.GetChar();
 
94
         if(c!=r.NOCHAR)
 
95
            break;
 
96
         SMTask::Block();
 
97
         if(SignalHook::GetCount(SIGINT)>0)
 
98
         {
 
99
            xfree(line);
 
100
            return(xstrdup(""));
 
101
         }
 
102
      }
 
103
      if(c==r.EOFCHAR && ptr==line)
 
104
      {
 
105
         xfree(line);
 
106
         return(NULL);
 
107
      }
 
108
      if(c==r.EOFCHAR || c=='\n')
 
109
      {
 
110
         *ptr=0;
 
111
         return(line);
 
112
      }
 
113
      if(len+2>=size)
 
114
      {
 
115
         size*=2;
 
116
         line=(char*)xrealloc(line,size);
 
117
         ptr=line+len;
 
118
      }
 
119
      *ptr++=c;
 
120
      len++;
 
121
   }
 
122
}