---
 apps/obex_test/obex_test.c        |   26 ++++++++++++++++-----
 apps/obex_test/obex_test.h        |    1 +
 apps/obex_test/obex_test_client.c |   44 +++++++++++++-----------------------
 3 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/apps/obex_test/obex_test.c b/apps/obex_test/obex_test.c
index 4200f9b..9336e2a 100644
--- a/apps/obex_test/obex_test.c
+++ b/apps/obex_test/obex_test.c
@@ -49,6 +49,7 @@ bdaddr_t bluez_compat_bdaddr_any = { BTH_ADDR_NULL };
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 
 #ifndef in_addr_t
 #define in_addr_t unsigned long
@@ -107,6 +108,21 @@ static void obex_event(obex_t *handle, obex_object_t 
*object, int mode,
        }
 }
 
+int read_input(char *answer, size_t size, const char *question, ...)
+{
+       va_list ap;
+       va_start(ap, question);
+       vfprintf(stdout, question, ap);
+       va_end(ap);
+
+       fflush(stdout);
+       if (fgets(answer, size, stdin) == NULL)
+               return -1;
+
+       answer[strlen(answer)-1] = '\0'; /* remove trailing newline */
+       return strlen(answer);
+}      
+
 #if 0
 /*
  * Function get_peer_addr (name, peer)
@@ -153,8 +169,8 @@ static int inet_connect(obex_t *handle)
 //
 int main (int argc, char *argv[])
 {
-       char cmd[10];
-       int num, end = 0;
+       char cmd[3];
+       int end = 0;
        int cobex = FALSE, tcpobex = FALSE, btobex = FALSE, r320 = FALSE, 
usbobex = FALSE;
        obex_t *handle;
 #ifdef HAVE_BLUETOOTH
@@ -349,11 +365,9 @@ int main (int argc, char *argv[])
        printf( "OBEX Interactive test client/server.\n");
 
        while (!end) {
-               printf("> ");
-               num = scanf("%s", cmd);
-               if (num == EOF)
+               if (read_input(cmd, sizeof(cmd), "> ") < 0)
                        break;
-               switch (cmd[0] | 0x20)  {
+               switch(cmd[0]) {
                        case 'h':
                                printf("Commands:\n"
                                       " c - connect\n"
diff --git a/apps/obex_test/obex_test.h b/apps/obex_test/obex_test.h
index a340f75..14d347e 100644
--- a/apps/obex_test/obex_test.h
+++ b/apps/obex_test/obex_test.h
@@ -19,5 +19,6 @@ struct context
        int clientdone;
        char *get_name; /* Name of last get-request */
 };
+int read_input(char *answer, size_t size, const char *question, ...);
 
 #endif
diff --git a/apps/obex_test/obex_test_client.c 
b/apps/obex_test/obex_test_client.c
index 8f0efc7..da92df7 100644
--- a/apps/obex_test/obex_test_client.c
+++ b/apps/obex_test/obex_test_client.c
@@ -227,18 +227,14 @@ void push_client(obex_t *handle)
        unsigned int uname_size;
        char *bfname;
        uint8_t *uname;
-       int num;
 
        obex_headerdata_t hd;
        
        uint8_t *buf;
        int file_size;
 
-       printf("PUSH filename> ");
-       memset(fname, 0, sizeof(fname));
-       num = scanf("%199c", fname);
-       if (num != 1) {
-               perror("scanf");
+       if (read_input(fname, sizeof(fname), "PUSH file> ") <= 0) {
+               perror("Error reading file name");
                return;
        }
 
@@ -294,16 +290,12 @@ void put_client(obex_t *handle)
        char rname[200];
        unsigned int rname_size;
        obex_headerdata_t hd;
-       int num;
        
        uint8_t *buf;
        int file_size;
 
-       printf("PUT file (local)> ");
-       memset(lname, 0, sizeof(lname));
-       num = scanf("%199c", lname);
-       if (num != 1) {
-               perror("scanf:");
+       if (read_input(lname, sizeof(lname), "PUT file (local)> ") <= 0) {
+               perror("Error reading file name");
                return;
        }
 
@@ -314,10 +306,12 @@ void put_client(obex_t *handle)
                return;
        }
 
-       printf("PUT remote filename (default: %s)> ", lname);
-       memset(rname, 0, sizeof(rname));
-       num = scanf("%199c", rname);
-       if (num == 0)
+       if (read_input(rname, sizeof(rname),
+                      "PUT remote filename (default: %s)> ", lname) < 0) {
+               perror("Error reading file name");
+               return;
+       }
+       if (strlen(rname) == 0)
                strcpy(rname, lname);
        printf("Going to send %d bytes\n", file_size);
 
@@ -362,14 +356,11 @@ void get_client(obex_t *handle, struct context *gt)
        obex_object_t *object;
        uint8_t rname[200];
        char req_name[200];
-       int num, rname_size;
+       int rname_size;
        obex_headerdata_t hd;
 
-       printf("GET File> ");
-       memset(req_name, 0, sizeof(req_name));
-       num = scanf("%199c", req_name);
-       if (num != 1) {
-               perror("scanf:");
+       if (read_input(req_name, sizeof(req_name), "GET file> ") <= 0) {
+               perror("Error reading file name");
                return;
        }
 
@@ -438,14 +429,11 @@ void setpath_client(obex_t *handle)
        uint8_t setpath_data[2] = { 0, 0 };
        obex_object_t *object;
        char path[200];
-       int num, path_size;
+       int path_size;
        obex_headerdata_t hd;
 
-       printf("SETPATH> ");
-       memset(path, 0, sizeof(path));
-       num = scanf("%199c", path);
-       if (num != 1) {
-               perror("scanf:");
+       if (read_input(path, sizeof(path), "SETPATH> ") <= 0) {
+               perror("Error reading path");
                return;
        }
 
-- 
1.7.5.4


------------------------------------------------------------------------------
Got Input?   Slashdot Needs You.
Take our quick survey online.  Come on, we don't ask for help often.
Plus, you'll get a chance to win $100 to spend on ThinkGeek.
http://p.sf.net/sfu/slashdot-survey
_______________________________________________
Openobex-users mailing list
Openobex-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/openobex-users

Reply via email to