diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-04-02 10:13:12 +0000 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-04-02 10:13:12 +0000 |
commit | 6026de46486913c40a9469267c429b2dfa57d806 (patch) | |
tree | 6fede1e55d6cef6544f263aad812b38afa87f320 /libssh/options.c | |
parent | a9ef024f1045f696e424c77d7bbde8ce324916ac (diff) | |
download | libssh-6026de46486913c40a9469267c429b2dfa57d806.tar.gz libssh-6026de46486913c40a9469267c429b2dfa57d806.tar.xz libssh-6026de46486913c40a9469267c429b2dfa57d806.zip |
Improve ssh_options_set_host().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@347 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/options.c')
-rw-r--r-- | libssh/options.c | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/libssh/options.c b/libssh/options.c index 09eb0b3..f38951c 100644 --- a/libssh/options.c +++ b/libssh/options.c @@ -211,24 +211,50 @@ void ssh_options_free(SSH_OPTIONS *opt) { SAFE_FREE(opt); } -/** \brief set destination hostname - * \param opt option structure - * \param hostname host name to connect +/** + * @brief Set destination hostname + * + * @param opt The option structure to use. + * + * @param hostname The host name to connect. + * + * @return 0 on succes, < 0 on error. */ -void ssh_options_set_host(SSH_OPTIONS *opt, const char *hostname){ - char *ptr=strdup(hostname); - char *ptr2=strchr(ptr,'@'); - if(opt->host) // don't leak memory - free(opt->host); - if(ptr2){ - *ptr2=0; - opt->host=strdup(ptr2+1); - if(opt->username) - free(opt->username); - opt->username=strdup(ptr); - free(ptr); - } else - opt->host=ptr; +int ssh_options_set_host(SSH_OPTIONS *opt, const char *hostname){ + char *h; + char *p; + + h = strdup(hostname); + if (h == NULL) { + return -1; + } + p = strchr(h, '@'); + + if (opt->host) { + SAFE_FREE(opt->host); + } + + if (p) { + *p = '\0'; + opt->host = strdup(p + 1); + if (opt->host == NULL) { + SAFE_FREE(h); + return -1; + } + + if (opt->username) { + SAFE_FREE(opt->username); + } + opt->username = strdup(h); + SAFE_FREE(h); + if (opt->username == NULL) { + return -1; + } + } else { + opt->host = h; + } + + return 0; } /** \brief set username for authentication |