]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
losetup: move xgetpass() to lib/xgetpass.c
authorKarel Zak <kzak@redhat.com>
Wed, 11 Jan 2012 14:17:06 +0000 (15:17 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 11 Jan 2012 14:17:06 +0000 (15:17 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/Makefile.am
include/xgetpass.h [new file with mode: 0644]
lib/xgetpass.c [new file with mode: 0644]
sys-utils/Makefile.am
sys-utils/losetup.c

index aa72a3e6037747cfdfd9e63fd3789c6d76b05531..1e41520e1790ad4fe001313870f716e0c8e8a40b 100644 (file)
@@ -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 (file)
index 0000000..b5a3c87
--- /dev/null
@@ -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 (file)
index 0000000..8587681
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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;
+}
+
index 0664b2d2c737d5c243cd652d679c36a3cd5b1f70..43eb8ca42901b5b411c14a84f574dd998e3e37b4 100644 (file)
@@ -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
index 7e80ee51357ea07cca31b7a9ee13d0477361b9b4..895363b354ea971777940b6da5e441a96a430213 100644 (file)
@@ -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)
 {