~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/get_password.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "config.h"
26
26
#include "client/get_password.h"
27
27
 
 
28
#include <string>
 
29
 
28
30
#include <string.h>
29
31
#include <stdio.h>
30
32
#include <stdlib.h>
45
47
#  endif
46
48
#endif
47
49
 
 
50
using namespace std;
 
51
 
 
52
bool tty_password= false;
 
53
const std::string PASSWORD_SENTINEL("\0\0\0\0\0", 5);
 
54
 
48
55
/*
49
56
  Can't use fgets, because readline will get confused
50
57
  length is max number of chars in to, not counting \0
66
73
      {
67
74
        if (echo)
68
75
        {
69
 
          fputs("\b \b",stdout);
70
 
          fflush(stdout);
 
76
          fputs("\b \b",stderr);
 
77
          fflush(stderr);
71
78
        }
72
79
        pos--;
73
80
        continue;
77
84
      break;
78
85
    if (iscntrl(tmp) || pos == end)
79
86
      continue;
80
 
    if (echo)
81
 
    {
82
 
      fputc('*',stdout);
83
 
      fflush(stdout);
84
 
    }
85
87
    *(pos++) = tmp;
86
88
  }
87
89
  while (pos != to && isspace(pos[-1]) == ' ')
96
98
  TERMIO org,tmp;
97
99
  char buff[80];
98
100
 
99
 
  if (isatty(fileno(stdout)))
 
101
  if (isatty(fileno(stderr)))
100
102
  {
101
 
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
102
 
    fflush(stdout);
 
103
    fputs(opt_message ? opt_message : "Enter password: ",stderr);
 
104
    fflush(stderr);
103
105
  }
104
106
#  if defined(HAVE_TERMIOS_H)
105
107
  tcgetattr(fileno(stdin), &org);
108
110
  tmp.c_cc[VMIN] = 1;
109
111
  tmp.c_cc[VTIME] = 0;
110
112
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
111
 
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
 
113
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
112
114
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
113
115
#  elif defined(HAVE_TERMIO_H)
114
116
  ioctl(fileno(stdin), (int) TCGETA, &org);
117
119
  tmp.c_cc[VMIN] = 1;
118
120
  tmp.c_cc[VTIME]= 0;
119
121
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
120
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
 
122
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
121
123
  ioctl(fileno(stdin),(int) TCSETA, &org);
122
124
#  else
123
125
  gtty(fileno(stdin), &org);
125
127
  tmp.sg_flags &= ~ECHO;
126
128
  tmp.sg_flags |= RAW;
127
129
  stty(fileno(stdin), &tmp);
128
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
 
130
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
129
131
  stty(fileno(stdin), &org);
130
132
#  endif
131
 
  if (isatty(fileno(stdout)))
132
 
    fputc('\n',stdout);
 
133
  if (isatty(fileno(stderr)))
 
134
    fputc('\n',stderr);
133
135
 
134
136
  return strdup(buff);
135
137
}
 
138
 
 
139
pair<string, string> parse_password_arg(string s)
 
140
{
 
141
  if (s.find("--password") == 0)
 
142
  {
 
143
    if (s == "--password")
 
144
    {
 
145
      tty_password= true;
 
146
      //check if no argument is passed.
 
147
      return make_pair("password", PASSWORD_SENTINEL);
 
148
    }
 
149
 
 
150
    if (s.substr(10,3) == "=\"\"" || s.substr(10,3) == "=''")
 
151
    {
 
152
      // Check if --password="" or --password=''
 
153
      return make_pair("password", PASSWORD_SENTINEL);
 
154
    }
 
155
    
 
156
    if(s.substr(10) == "=" && s.length() == 11)
 
157
    {
 
158
      // check if --password= and return a default value
 
159
      return make_pair("password", PASSWORD_SENTINEL);
 
160
    }
 
161
  }
 
162
 
 
163
  return make_pair(string(""), string(""));
 
164
}
 
165