libidav/utils.c

changeset 153
272173064319
parent 152
ff854d3df20c
child 158
33237261321e
equal deleted inserted replaced
152:ff854d3df20c 153:272173064319
35 #include <ucx/buffer.h> 35 #include <ucx/buffer.h>
36 #include <ucx/utils.h> 36 #include <ucx/utils.h>
37 #include <libxml/tree.h> 37 #include <libxml/tree.h>
38 #include <curl/curl.h> 38 #include <curl/curl.h>
39 39
40 #ifndef _WIN32 40 #ifdef _WIN32
41 #include <conio.h>
42 #define getpasswordchar() getch()
43 #else
41 #include <termios.h> 44 #include <termios.h>
45 #define getpasswordchar() getchar()
42 #endif 46 #endif
43 47
44 #include <openssl/sha.h> 48 #include <openssl/sha.h>
45 #include <openssl/hmac.h> 49 #include <openssl/hmac.h>
46 #include <openssl/evp.h> 50 #include <openssl/evp.h>
445 449
446 char* util_password_input(char *prompt) { 450 char* util_password_input(char *prompt) {
447 fprintf(stderr, "%s", prompt); 451 fprintf(stderr, "%s", prompt);
448 fflush(stderr); 452 fflush(stderr);
449 453
454 #ifndef _WIN32
450 // hide terminal input 455 // hide terminal input
451 #ifdef _WIN32
452 // TODO
453 #else
454 struct termios oflags, nflags; 456 struct termios oflags, nflags;
455 tcgetattr(fileno(stdin), &oflags); 457 tcgetattr(fileno(stdin), &oflags);
456 nflags = oflags; 458 nflags = oflags;
457 nflags.c_lflag &= ~ECHO; 459 nflags.c_lflag &= ~ECHO;
458 nflags.c_lflag |= ECHONL; 460 nflags.c_lflag |= ECHONL;
462 #endif 464 #endif
463 465
464 // read password input 466 // read password input
465 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); 467 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
466 int c = 0; 468 int c = 0;
467 while((c = getchar()) != EOF) { 469 while((c = getpasswordchar()) != EOF) {
468 if(c == '\n') { 470 if(c == '\n' || c == '\r') {
469 break; 471 break;
470 } 472 }
471 ucx_buffer_putc(buf, c); 473 ucx_buffer_putc(buf, c);
472 } 474 }
473 ucx_buffer_putc(buf, 0); 475 ucx_buffer_putc(buf, 0);
474 476 fflush(stdin);
477
478 #ifndef _WIN32
475 // restore terminal settings 479 // restore terminal settings
476 #ifdef _WIN32
477 // TODO
478 #else
479 if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) { 480 if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
480 perror("tcsetattr"); 481 perror("tcsetattr");
481 } 482 }
482 #endif 483 #endif
483 484

mercurial