widechar.h \
writeall.h \
rpmatch.h \
+ xgetpass.h \
xalloc.h
--- /dev/null
+#ifndef UTIL_LINUX_XGETPASS_H
+#define UTIL_LINUX_XGETPASS_H
+
+extern char *xgetpass(int pfd, const char *prompt);
+
+#endif /* UTIL_LINUX_XGETPASS_H */
--- /dev/null
+/*
+ * A function to read the passphrase either from the terminal or from
+ * an open file descriptor.
+ *
+ * Public domain.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+
+#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;
+}
+
$(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
#include "nls.h"
#include "strutils.h"
#include "loopdev.h"
+#include "xgetpass.h"
enum {
A_CREATE = 1, /* setup a new device */
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)
{