Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcodex/codexgetpass.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* David Korn <[email protected]> *
19
* Phong Vo <[email protected]> *
20
* *
21
***********************************************************************/
22
#pragma prototyped
23
24
/*
25
* prompt for password and return it in buf
26
* return < 0 : error
27
* return >= n : n-1 in buf, nul terminated
28
*/
29
30
#include <codex.h>
31
#include <sig.h>
32
#include <ast_tty.h>
33
34
ssize_t
35
codexgetpass(const char* prompt, void* buf, size_t n)
36
{
37
char* s;
38
long flags;
39
ssize_t r;
40
Sfio_t* rp;
41
Sfio_t* wp;
42
Sig_handler_t sigint;
43
struct termios tty;
44
45
if (rp = sfopen(NiL, "/dev/tty", "r+"))
46
wp = rp;
47
else
48
{
49
rp = sfstdin;
50
wp = sfstderr;
51
}
52
sigint = signal(SIGINT, SIG_IGN);
53
tcgetattr(sffileno(rp), &tty);
54
flags = tty.c_lflag;
55
tty.c_lflag &= ~(ECHO|ECHONL);
56
tcsetattr(sffileno(rp), TCSANOW, &tty);
57
tty.c_lflag = flags;
58
if (prompt)
59
{
60
sfprintf(wp, "%s", prompt);
61
sfsync(wp);
62
}
63
if (s = sfgetr(rp, '\n', 1))
64
r = strncopy((char*)buf, s, n) - (char*)buf;
65
else
66
r = -1;
67
sfprintf(wp, "\n");
68
sfsync(wp);
69
tcsetattr(sffileno(rp), TCSANOW, &tty);
70
signal(SIGINT, sigint);
71
if (rp == wp)
72
sfclose(rp);
73
return r;
74
}
75
76