ensure_user() and ensure_password() attempt to obtain the user and
password, respectively, if not already known, before returning it.  This
has resulted in the function definitions being almost entirely contained
within a single if block, executed when the result is not already known.
Such control flow is not the most readable, particularly for longer
functions, due to the added indentation.

Reorder these function definitions to instead return early if the
user/password is already known.  This saves space by removing the
unnecessary indentation and increases readability by immediately
communicating the intent to do nothing if not needed.
---
 src/drv_imap.c | 97 +++++++++++++++++++++++++-------------------------
 1 file changed, 48 insertions(+), 49 deletions(-)

diff --git a/src/drv_imap.c b/src/drv_imap.c
index 9b170ab74311..21daae41c166 100644
--- a/src/drv_imap.c
+++ b/src/drv_imap.c
@@ -2322,63 +2322,62 @@ cred_from_cmd( const char *cred, const char *cmd, const 
char *srv_name )
 static const char *
 ensure_user( imap_server_conf_t *srvc )
 {
-       if (!srvc->user) {
-               if (srvc->user_cmd) {
-                       srvc->user = cred_from_cmd( "UserCmd", srvc->user_cmd, 
srvc->name );
-               } else {
-                       error( "Skipping account %s, no user\n", srvc->name );
-               }
-       }
+       if (srvc->user)
+               return srvc->user;
+       if (srvc->user_cmd)
+               srvc->user = cred_from_cmd( "UserCmd", srvc->user_cmd, 
srvc->name );
+       else
+               error( "Skipping account %s, no user\n", srvc->name );
        return srvc->user;
 }
 
 static const char *
 ensure_password( imap_server_conf_t *srvc )
 {
-       if (!srvc->pass) {
-               if (srvc->pass_cmd) {
-                       srvc->pass = cred_from_cmd( "PassCmd", srvc->pass_cmd, 
srvc->name );
+       if (srvc->pass)
+               return srvc->pass;
+       if (srvc->pass_cmd) {
+               srvc->pass = cred_from_cmd( "PassCmd", srvc->pass_cmd, 
srvc->name );
 #ifdef HAVE_MACOS_KEYCHAIN
-               } else if (srvc->use_keychain) {
-                       void *password_data;
-                       UInt32 password_length;
-                       OSStatus ret = SecKeychainFindInternetPassword(
-                                       NULL,  // keychainOrArray
-                                       strlen( srvc->sconf.host ), 
srvc->sconf.host,
-                                       0, NULL,  // securityDomain
-                                       strlen( srvc->user ), srvc->user,
-                                       0, NULL,  // path
-                                       0,  // port - we could use it, but it 
seems pointless
-                                       kSecProtocolTypeIMAP,
-                                       kSecAuthenticationTypeDefault,
-                                       &password_length, &password_data,
-                                       NULL );  // itemRef
-                       if (ret != errSecSuccess) {
-                               CFStringRef errmsg = SecCopyErrorMessageString( 
ret, NULL );
-                               error( "Looking up Keychain failed: %s\n",
-                                      CFStringGetCStringPtr( errmsg, 
kCFStringEncodingUTF8 ) );
-                               CFRelease( errmsg );
-                               return NULL;
-                       }
-                       srvc->pass = nfstrndup( password_data, password_length 
);
-                       SecKeychainItemFreeContent( NULL, password_data );
-#endif /* HAVE_MACOS_KEYCHAIN */
-               } else {
-                       flushn();
-                       char prompt[80];
-                       sprintf( prompt, "Password (%s): ", srvc->name );
-                       char *pass = getpass( prompt );
-                       if (!pass) {
-                               perror( "getpass" );
-                               exit( 1 );
-                       }
-                       if (!*pass) {
-                               error( "Skipping account %s, no password\n", 
srvc->name );
-                               return NULL;
-                       }
-                       /* getpass() returns a pointer to a static buffer. Make 
a copy for long term storage. */
-                       srvc->pass = nfstrdup( pass );
+       } else if (srvc->use_keychain) {
+               void *password_data;
+               UInt32 password_length;
+               OSStatus ret = SecKeychainFindInternetPassword(
+                               NULL,  // keychainOrArray
+                               strlen( srvc->sconf.host ), srvc->sconf.host,
+                               0, NULL,  // securityDomain
+                               strlen( srvc->user ), srvc->user,
+                               0, NULL,  // path
+                               0,  // port - we could use it, but it seems 
pointless
+                               kSecProtocolTypeIMAP,
+                               kSecAuthenticationTypeDefault,
+                               &password_length, &password_data,
+                               NULL );  // itemRef
+               if (ret != errSecSuccess) {
+                       CFStringRef errmsg = SecCopyErrorMessageString( ret, 
NULL );
+                       error( "Looking up Keychain failed: %s\n",
+                              CFStringGetCStringPtr( errmsg, 
kCFStringEncodingUTF8 ) );
+                       CFRelease( errmsg );
+                       return NULL;
                }
+               srvc->pass = nfstrndup( password_data, password_length );
+               SecKeychainItemFreeContent( NULL, password_data );
+#endif /* HAVE_MACOS_KEYCHAIN */
+       } else {
+               flushn();
+               char prompt[80];
+               sprintf( prompt, "Password (%s): ", srvc->name );
+               char *pass = getpass( prompt );
+               if (!pass) {
+                       perror( "getpass" );
+                       exit( 1 );
+               }
+               if (!*pass) {
+                       error( "Skipping account %s, no password\n", srvc->name 
);
+                       return NULL;
+               }
+               /* getpass() returns a pointer to a static buffer. Make a copy 
for long term storage. */
+               srvc->pass = nfstrdup( pass );
        }
        return srvc->pass;
 }
-- 
2.50.1 (Apple Git-155)



_______________________________________________
isync-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/isync-devel

Reply via email to