From 9b498c9f0fc4faf6314a8c48125aa0afac26c2ba Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Thu, 15 Sep 2016 11:20:18 +0200 Subject: [PATCH] Refactor to avoid unnecessary preprocessor logic Reviewed-by: Andy Polyakov Reviewed-by: Rich Salz (cherry picked from commit 51e5133d551b4c132f72fc2ff5bbe076f5a3e052) --- apps/apps.c | 39 +++++++++++++++++++++++++++++++++++++-- apps/apps.h | 2 ++ apps/s_client.c | 46 +++++++++------------------------------------- apps/s_server.c | 45 ++++++--------------------------------------- 4 files changed, 54 insertions(+), 78 deletions(-) diff --git a/apps/apps.c b/apps/apps.c index 522db71322..6ca0f2b1bc 100644 --- a/apps/apps.c +++ b/apps/apps.c @@ -2300,6 +2300,36 @@ int app_isdir(const char *name) #endif /* raw_read|write section */ +#if defined(__VMS) +# include "vms_term_sock.h" +static int stdin_sock = -1; + +static void close_stdin_sock(void) +{ + TerminalSocket (TERM_SOCK_DELETE, &stdin_sock); +} + +int fileno_stdin(void) +{ + if (stdin_sock == -1) { + TerminalSocket(TERM_SOCK_CREATE, &stdin_sock); + atexit(close_stdin_sock); + } + + return stdin_sock; +} +#else +int fileno_stdin(void) +{ + return fileno(stdin); +} +#endif + +int fileno_stdout(void) +{ + return fileno(stdout); +} + #if defined(_WIN32) && defined(STD_INPUT_HANDLE) int raw_read_stdin(void *buf, int siz) { @@ -2309,10 +2339,15 @@ int raw_read_stdin(void *buf, int siz) else return (-1); } +#elif defined(__VMS) +int raw_read_stdin(void *buf, int siz) +{ + return recv(fileno_stdin(), buf, siz, 0); +} #else int raw_read_stdin(void *buf, int siz) { - return read(fileno(stdin), buf, siz); + return read(fileno_stdin(), buf, siz); } #endif @@ -2328,7 +2363,7 @@ int raw_write_stdout(const void *buf, int siz) #else int raw_write_stdout(const void *buf, int siz) { - return write(fileno(stdout), buf, siz); + return write(fileno_stdout(), buf, siz); } #endif diff --git a/apps/apps.h b/apps/apps.h index 85b65191bd..27e182c580 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -548,6 +548,8 @@ void store_setup_crl_download(X509_STORE *st); int app_isdir(const char *); int app_access(const char *, int flag); +int fileno_stdin(void); +int fileno_stdout(void); int raw_read_stdin(void *, int); int raw_write_stdout(const void *, int); diff --git a/apps/s_client.c b/apps/s_client.c index 820bffe8b4..0627ae8bf6 100644 --- a/apps/s_client.c +++ b/apps/s_client.c @@ -43,9 +43,6 @@ #ifndef OPENSSL_NO_SOCK -#ifdef OPENSSL_SYS_VMS -# include "vms_term_sock.h" -#endif /* * With IPv6, it looks like Digital has mixed up the proper order of * recursive header file inclusion, resulting in the compiler complaining @@ -861,10 +858,6 @@ int s_client_main(int argc, char **argv) int c_nbio = 0, c_msg = 0, c_ign_eof = 0, c_brief = 0; int c_tlsextdebug = 0, c_status_req = 0; BIO *bio_c_msg = NULL; -#if defined(OPENSSL_SYS_VMS) - int stdin_sock; - TerminalSocket(TERM_SOCK_CREATE, &stdin_sock); -#endif FD_ZERO(&readfds); FD_ZERO(&writefds); @@ -1828,14 +1821,11 @@ int s_client_main(int argc, char **argv) SSL_set_connect_state(con); /* ok, lets connect */ -#if defined(OPENSSL_SYS_VMS) - if (stdin_sock > SSL_get_fd(con)) - width = stdin_sock + 1; + if (fileno_stdin() > SSL_get_fd(con)) + width = fileno_stdin() + 1; else width = SSL_get_fd(con) + 1; -#else - width = SSL_get_fd(con) + 1; -#endif + read_tty = 1; write_tty = 0; tty_on = 0; @@ -2162,14 +2152,11 @@ int s_client_main(int argc, char **argv) * and EOF satisfies that. To avoid a CPU-hogging loop, * set the flag so we exit. */ -#if defined(OPENSSL_SYS_VMS) - if (read_tty && !at_eof) - openssl_fdset(stdin_sock, &readfds); -#else if (read_tty && !at_eof) - openssl_fdset(fileno(stdin), &readfds); + openssl_fdset(fileno_stdin(), &readfds); +#if !defined(OPENSSL_SYS_VMS) if (write_tty) - openssl_fdset(fileno(stdout), &writefds); + openssl_fdset(fileno_stdout(), &writefds); #endif } if (read_ssl) @@ -2300,7 +2287,7 @@ int s_client_main(int argc, char **argv) /* Assume Windows/DOS/BeOS can always write */ else if (!ssl_pending && write_tty) #else - else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds)) + else if (!ssl_pending && FD_ISSET(fileno_stdout(), &writefds)) #endif { #ifdef CHARSET_EBCDIC @@ -2388,21 +2375,14 @@ int s_client_main(int argc, char **argv) /* OPENSSL_SYS_MSDOS includes OPENSSL_SYS_WINDOWS */ #if defined(OPENSSL_SYS_MSDOS) else if (has_stdin_waiting()) -#elif defined(OPENSSL_SYS_VMS) - else if (FD_ISSET(stdin_sock, &readfds)) #else - else if (FD_ISSET(fileno(stdin), &readfds)) + else if (FD_ISSET(fileno_stdin(), &readfds)) #endif { if (crlf) { int j, lf_num; -#if defined(OPENSSL_SYS_VMS) - i = recv(stdin_sock, cbuf, BUFSIZZ / 2, 0); -#else i = raw_read_stdin(cbuf, BUFSIZZ / 2); -#endif - lf_num = 0; /* both loops are skipped when i <= 0 */ for (j = 0; j < i; j++) @@ -2417,13 +2397,8 @@ int s_client_main(int argc, char **argv) } } assert(lf_num == 0); - } else { -#if defined(OPENSSL_SYS_VMS) - i = recv(stdin_sock, cbuf, BUFSIZZ, 0); -#else + } else i = raw_read_stdin(cbuf, BUFSIZZ); -#endif - } #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) if (i == 0) at_eof = 1; @@ -2508,9 +2483,6 @@ int s_client_main(int argc, char **argv) bio_c_out = NULL; BIO_free(bio_c_msg); bio_c_msg = NULL; -#if defined(OPENSSL_SYS_VMS) - TerminalSocket(TERM_SOCK_DELETE, &stdin_sock); -#endif return (ret); } diff --git a/apps/s_server.c b/apps/s_server.c index 060a0bb24f..ebab7a2137 100644 --- a/apps/s_server.c +++ b/apps/s_server.c @@ -90,11 +90,6 @@ typedef unsigned int u_int; #include #endif -#ifdef OPENSSL_SYS_VMS -# include "vms_term_sock.h" -#endif - - static int not_resumable_sess_cb(SSL *s, int is_forward_secure); static int sv_body(int s, int stype, unsigned char *context); static int www_body(int s, int stype, unsigned char *context); @@ -2012,10 +2007,6 @@ static int sv_body(int s, int stype, unsigned char *context) #else struct timeval *timeoutp; #endif -#if defined(OPENSSL_SYS_VMS) - int stdin_sock; - TerminalSocket (TERM_SOCK_CREATE, &stdin_sock); -#endif buf = app_malloc(bufsize, "server buffer"); if (s_nbio) { @@ -2116,15 +2107,10 @@ static int sv_body(int s, int stype, unsigned char *context) SSL_set_tlsext_debug_arg(con, bio_s_out); } - -#if defined(OPENSSL_SYS_VMS) - if (stdin_sock > s) - width = stdin_sock + 1; + if (fileno_stdin() > s) + width = fileno_stdin() + 1; else width = s + 1; -#else - width = s + 1; -#endif for (;;) { int read_from_terminal; int read_from_sslcon; @@ -2136,11 +2122,7 @@ static int sv_body(int s, int stype, unsigned char *context) if (!read_from_sslcon) { FD_ZERO(&readfds); #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) -# if defined(OPENSSL_SYS_VMS) - openssl_fdset(stdin_sock, &readfds); -# else - openssl_fdset(fileno(stdin), &readfds); -# endif + openssl_fdset(fileno_stdin(), &readfds); #endif openssl_fdset(s, &readfds); /* @@ -2180,11 +2162,7 @@ static int sv_body(int s, int stype, unsigned char *context) if (i <= 0) continue; -# if defined(OPENSSL_SYS_VMS) - if (FD_ISSET(stdin_sock, &readfds)) -# else - if (FD_ISSET(fileno(stdin), &readfds)) -# endif + if (FD_ISSET(fileno_stdin(), &readfds)) read_from_terminal = 1; #endif if (FD_ISSET(s, &readfds)) @@ -2194,11 +2172,7 @@ static int sv_body(int s, int stype, unsigned char *context) if (s_crlf) { int j, lf_num; -#if defined(OPENSSL_SYS_VMS) - i=recv(stdin_sock, buf, bufsize / 2, 0); -#else i = raw_read_stdin(buf, bufsize / 2); -#endif lf_num = 0; /* both loops are skipped when i <= 0 */ for (j = 0; j < i; j++) @@ -2213,13 +2187,9 @@ static int sv_body(int s, int stype, unsigned char *context) } } assert(lf_num == 0); - } else { -#if defined(OPENSSL_SYS_VMS) - i = recv(stdin_sock, buf, bufsize, 0); -#else + } else i = raw_read_stdin(buf, bufsize); -#endif - } + if (!s_quiet && !s_brief) { if ((i <= 0) || (buf[0] == 'Q')) { BIO_printf(bio_s_out, "DONE\n"); @@ -2433,9 +2403,6 @@ static int sv_body(int s, int stype, unsigned char *context) OPENSSL_clear_free(buf, bufsize); if (ret >= 0) BIO_printf(bio_s_out, "ACCEPT\n"); -#if defined(OPENSSL_SYS_VMS) - TerminalSocket (TERM_SOCK_DELETE, &stdin_sock); -#endif (void)BIO_flush(bio_s_out); return (ret); } -- 2.25.1