From 9ea6ea581d655d81d40efc7c678e8e1f036d533d Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 2 Apr 2009 12:00:45 +0000 Subject: [PATCH] Improve ssh_options_set_ssh_dir(). git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@354 7dcaeef0-15fb-0310-b436-a5af3365683c --- include/libssh/libssh.h | 2 +- libssh/options.c | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index ee14ef28..e89307b2 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -318,7 +318,7 @@ void ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity); void ssh_options_set_status_callback(SSH_OPTIONS *opt, void (*callback) (void *arg, float status), void *arg); void ssh_options_set_timeout(SSH_OPTIONS *opt, long seconds, long usec); -void ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir); +int ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir); void ssh_options_set_known_hosts_file(SSH_OPTIONS *opt, const char *dir); void ssh_options_allow_ssh1(SSH_OPTIONS *opt, int allow); void ssh_options_allow_ssh2(SSH_OPTIONS *opt, int allow); diff --git a/libssh/options.c b/libssh/options.c index 77f44a7f..8c318883 100644 --- a/libssh/options.c +++ b/libssh/options.c @@ -355,18 +355,35 @@ int ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port) { return 0; } -/** the ssh directory is used for files like known_hosts and - * identity (public and private keys)\n - * \brief set the ssh directory - * \param opt options structure - * \param dir directory. It may include "%s" which will be replaced by - * the user home directory - * \see ssh_options_set_user_home_dir() +/** + * @brief Set the ssh directory. + * + * The ssh directory is used for files like known_hosts and identity (public + * and private keys) + * + * @param opt The options structure to use. + * + * @param dir The directory to set. It may include "%s" which will be + * replaced by the user home directory. + * + * @return 0 on success, < 0 on error. + * + * @see ssh_options_set_user_home_dir() */ -void ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir){ - char buffer[1024]; - snprintf(buffer,1024,dir,ssh_get_user_home_dir()); - opt->ssh_dir=strdup(buffer); +int ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir) { + char buffer[1024] = {0}; + + if (opt == NULL || dir == NULL) { + return -1; + } + + snprintf(buffer, 1024, dir, ssh_get_user_home_dir()); + opt->ssh_dir = strdup(buffer); + if (opt->ssh_dir == NULL) { + return -1; + } + + return 0; } /** the known hosts file is used to certify remote hosts are genuine.