getpass for windows does not work
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Sat, 13 Dec 2008 17:58:13 +0100
changeset 20 5fec678f931b
parent 19 933d86c1ff71
child 21 403742321c65
getpass for windows does not work committer: Markus Bröker <mbroeker@largo.homelinux.org>
db_bridge/Makefile
db_bridge/console.cpp
db_bridge/include/console.h
db_bridge/mysql_db.cpp
db_bridge/postgresql_db.cpp
urandom.c
--- a/db_bridge/Makefile
+++ b/db_bridge/Makefile
@@ -4,6 +4,10 @@
 	mysql_db \
 	postgresql_db
 
+OBJECTS=\
+	main.o \
+	console.o
+
 .SUFFIXES: .cpp
 
 .cpp.o:
@@ -11,13 +15,13 @@
 
 all: $(TARGET)
 
-mysql_db: mysql_db.o main.o
-	$(CC) $< main.o -lmysqlclient -o $@
+mysql_db: mysql_db.o $(OBJECTS)
+	$(CC) $< $(OBJECTS) -lmysqlclient -o $@
 	rm -f main.o
 
-postgresql_db: postgresql_db.o main.cpp
+postgresql_db: postgresql_db.o $(OBJECTS)
 	$(CC) -c $(CFLAGS) -DWITH_POSTGRESQL main.cpp
-	$(CC) $< main.o -lpq -o $@
+	$(CC) $< $(OBJECTS) -lpq -o $@
 	rm -f main.o
 
 .PHONY: clean
--- a/db_bridge/console.cpp
+++ b/db_bridge/console.cpp
@@ -1,5 +1,14 @@
 #include <console.h>
+#include <cstdio>
 
+#ifdef WIN32
+#include <conio.h>
+int Console::getch ()
+{
+    return::getch ();
+}
+#else
+#include <termios.h>
 int Console::getch ()
 {
     static int ch = -1, fd = 0;
@@ -14,6 +23,7 @@
     tcsetattr (fd, TCSANOW, &alt);
     return ch;
 }
+#endif
 
 void Console::getpass (std::string & password)
 {
@@ -21,9 +31,9 @@
     int c;
     char buffer[17];
 
-    while ((c = getch ()) != '\n' && i < 17) {
+    while ((c = Console::getch ()) != '\n' && i < 17) {
         buffer[i++] = c;
-        std::cout << "*";
+        std::cout << "\b*";
     }
     buffer[i] = 0;
 
--- a/db_bridge/include/console.h
+++ b/db_bridge/include/console.h
@@ -2,7 +2,6 @@
 #define CONSOLE_H
 
 #include <iostream>
-#include <termios.h>
 
 namespace Console {
     int getch ();
--- a/db_bridge/mysql_db.cpp
+++ b/db_bridge/mysql_db.cpp
@@ -4,6 +4,7 @@
  */
 
 #include <mysql_db.h>
+#include <console.h>
 
 MySQL_DB::MySQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
 {
@@ -15,7 +16,7 @@
 
     if (server != "" && pwd == "") {
         std::cout << "Passwort von " << user << "@" << server << ": ";
-        std::getline (std::cin, password);
+        Console::getpass (password);
     }
 }
 
--- a/db_bridge/postgresql_db.cpp
+++ b/db_bridge/postgresql_db.cpp
@@ -5,6 +5,7 @@
 
 #include <postgresql_db.h>
 #include <exception>
+#include <console.h>
 
 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
 {
@@ -17,7 +18,7 @@
 
     if (server != "" && pwd == "") {
         std::cout << "Passwort von " << user << "@" << server << ": ";
-        std::getline (std::cin, password);
+        Console::getpass (password);
         conninfo += " host=" + server + " password=" + password;
     }
 }
--- a/urandom.c
+++ b/urandom.c
@@ -1,5 +1,5 @@
 /**
- * test/demos/tree.c
+ * test/demos/urandom.c
  * Copyright (C) 2008 Markus Broeker
  */
 
@@ -28,6 +28,7 @@
 
     elements = read (fd, numbers, 255);
     numbers[elements] = 0;
+    close (fd);
 
     qsort (numbers, elements, sizeof (unsigned char), (void *)&compare);
 
@@ -35,7 +36,5 @@
         printf ("Number: %d\n", numbers[i]);
     }
 
-    close (fd);
-
     return EXIT_SUCCESS;
 }