From: Karel Zak Date: Wed, 11 Jan 2012 14:17:06 +0000 (+0100) Subject: losetup: move xgetpass() to lib/xgetpass.c X-Git-Tag: v2.21-rc1~59 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6c503d5916efe6549bb8428931547735c2c54fa4;p=thirdparty%2Futil-linux.git losetup: move xgetpass() to lib/xgetpass.c Signed-off-by: Karel Zak --- diff --git a/include/Makefile.am b/include/Makefile.am index aa72a3e603..1e41520e17 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -38,4 +38,5 @@ dist_noinst_HEADERS = \ widechar.h \ writeall.h \ rpmatch.h \ + xgetpass.h \ xalloc.h diff --git a/include/xgetpass.h b/include/xgetpass.h new file mode 100644 index 0000000000..b5a3c87de1 --- /dev/null +++ b/include/xgetpass.h @@ -0,0 +1,6 @@ +#ifndef UTIL_LINUX_XGETPASS_H +#define UTIL_LINUX_XGETPASS_H + +extern char *xgetpass(int pfd, const char *prompt); + +#endif /* UTIL_LINUX_XGETPASS_H */ diff --git a/lib/xgetpass.c b/lib/xgetpass.c new file mode 100644 index 0000000000..8587681489 --- /dev/null +++ b/lib/xgetpass.c @@ -0,0 +1,46 @@ +/* + * A function to read the passphrase either from the terminal or from + * an open file descriptor. + * + * Public domain. + */ + +#include +#include +#include +#include +#include +#include + +#include "c.h" +#include "xgetpass.h" + +char *xgetpass(int pfd, const char *prompt) +{ + char *pass = NULL; + int len = 0, i; + + if (pfd < 0) /* terminal */ + return getpass(prompt); + + for (i=0; ; i++) { + if (i >= len-1) { + char *tmppass = pass; + len += 128; + + pass = realloc(tmppass, len); + if (!pass) { + pass = tmppass; /* the old buffer hasn't changed */ + break; + } + } + if (read(pfd, pass + i, 1) != 1 || + pass[i] == '\n' || pass[i] == 0) + break; + } + + if (pass) + pass[i] = '\0'; + return pass; +} + diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index 0664b2d2c7..43eb8ca429 100644 --- a/sys-utils/Makefile.am +++ b/sys-utils/Makefile.am @@ -33,6 +33,7 @@ losetup_SOURCES = losetup.c \ $(top_srcdir)/lib/sysfs.c \ $(top_srcdir)/lib/loopdev.c \ $(top_srcdir)/lib/canonicalize.c \ + $(top_srcdir)/lib/xgetpass.c \ $(top_srcdir)/lib/strutils.c if HAVE_STATIC_LOSETUP diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c index 7e80ee5135..895363b354 100644 --- a/sys-utils/losetup.c +++ b/sys-utils/losetup.c @@ -18,6 +18,7 @@ #include "nls.h" #include "strutils.h" #include "loopdev.h" +#include "xgetpass.h" enum { A_CREATE = 1, /* setup a new device */ @@ -31,45 +32,6 @@ enum { static int verbose; -/* - * A function to read the passphrase either from the terminal or from - * an open file descriptor. - */ -static char *xgetpass(int pfd, const char *prompt) -{ - char *pass; - int buflen, i; - - if (pfd < 0) /* terminal */ - return getpass(prompt); - - pass = NULL; - buflen = 0; - for (i=0; ; i++) { - if (i >= buflen-1) { - /* we're running out of space in the buffer. - * Make it bigger: */ - char *tmppass = pass; - buflen += 128; - pass = realloc(tmppass, buflen); - if (pass == NULL) { - /* realloc failed. Stop reading. */ - warn(_("Out of memory while reading passphrase")); - pass = tmppass; /* the old buffer hasn't changed */ - break; - } - } - if (read(pfd, pass+i, 1) != 1 || - pass[i] == '\n' || pass[i] == 0) - break; - } - - if (pass == NULL) - return ""; - - pass[i] = 0; - return pass; -} static int printf_loopdev(struct loopdev_cxt *lc) {