OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [unistd/] [getpass.c] - Blame information for rev 1325

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1992,93,94,95,96,97,98,99 Free Software Foundation, Inc.
2
   This file is part of the GNU C Library.
3
 
4
   The GNU C Library is free software; you can redistribute it and/or
5
   modify it under the terms of the GNU Library General Public License as
6
   published by the Free Software Foundation; either version 2 of the
7
   License, or (at your option) any later version.
8
 
9
   The GNU C Library is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Library General Public License for more details.
13
 
14
   You should have received a copy of the GNU Library General Public
15
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
   Boston, MA 02111-1307, USA.  */
18
 
19
#include <stdio.h>
20
#include <string.h>
21
#include <termios.h>
22
#include <unistd.h>
23
#include <string.h>
24
 
25
/* It is desirable to use this bit on systems that have it.
26
   The only bit of terminal state we want to twiddle is echoing, which is
27
   done in software; there is no need to change the state of the terminal
28
   hardware.  */
29
 
30
#ifndef TCSASOFT
31
#define TCSASOFT 0
32
#endif
33
#define PWD_BUFFER_SIZE 256
34
 
35
char *
36
getpass (prompt)
37
     const char *prompt;
38
{
39
  FILE *in, *out;
40
  struct termios s, t;
41
  int tty_changed;
42
  static char buf[PWD_BUFFER_SIZE];
43
  int nread;
44
 
45
  /* Try to write to and read from the terminal if we can.
46
     If we can't open the terminal, use stderr and stdin.  */
47
 
48
  in = fopen ("/dev/tty", "r+");
49
  if (in == NULL)
50
    {
51
      in = stdin;
52
      out = stderr;
53
    }
54
  else
55
    out = in;
56
 
57
  /* Turn echoing off if it is on now.  */
58
 
59
  if (tcgetattr (fileno (in), &t) == 0)
60
    {
61
      /* Save the old one. */
62
      s = t;
63
      /* Tricky, tricky. */
64
      t.c_lflag &= ~(ECHO|ISIG);
65
      tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
66
      if (in != stdin) {
67
        /* Disable buffering for read/write FILE to prevent problems with
68
         * fseek and buffering for read/write auto-transitioning. */
69
        setvbuf(in, NULL, _IONBF, 0);
70
      }
71
    }
72
  else
73
    tty_changed = 0;
74
 
75
  /* Write the prompt.  */
76
  fputs(prompt, out);
77
  fflush(out);
78
 
79
  /* Read the password.  */
80
  fgets (buf, PWD_BUFFER_SIZE-1, in);
81
  if (buf != NULL)
82
    {
83
      nread = strlen(buf);
84
      if (nread < 0)
85
        buf[0] = '\0';
86
      else if (buf[nread - 1] == '\n')
87
        {
88
          /* Remove the newline.  */
89
          buf[nread - 1] = '\0';
90
          if (tty_changed)
91
            /* Write the newline that was not echoed.  */
92
            putc('\n', out);
93
        }
94
    }
95
 
96
  /* Restore the original setting.  */
97
  if (tty_changed) {
98
    (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
99
  }
100
 
101
  if (in != stdin)
102
    /* We opened the terminal; now close it.  */
103
    fclose (in);
104
 
105
  return buf;
106
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.