/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 2003-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped2223/*24* prompt for password and return it in buf25* return < 0 : error26* return >= n : n-1 in buf, nul terminated27*/2829#include <codex.h>30#include <sig.h>31#include <ast_tty.h>3233ssize_t34codexgetpass(const char* prompt, void* buf, size_t n)35{36char* s;37long flags;38ssize_t r;39Sfio_t* rp;40Sfio_t* wp;41Sig_handler_t sigint;42struct termios tty;4344if (rp = sfopen(NiL, "/dev/tty", "r+"))45wp = rp;46else47{48rp = sfstdin;49wp = sfstderr;50}51sigint = signal(SIGINT, SIG_IGN);52tcgetattr(sffileno(rp), &tty);53flags = tty.c_lflag;54tty.c_lflag &= ~(ECHO|ECHONL);55tcsetattr(sffileno(rp), TCSANOW, &tty);56tty.c_lflag = flags;57if (prompt)58{59sfprintf(wp, "%s", prompt);60sfsync(wp);61}62if (s = sfgetr(rp, '\n', 1))63r = strncopy((char*)buf, s, n) - (char*)buf;64else65r = -1;66sfprintf(wp, "\n");67sfsync(wp);68tcsetattr(sffileno(rp), TCSANOW, &tty);69signal(SIGINT, sigint);70if (rp == wp)71sfclose(rp);72return r;73}747576