diff options
author | Stefan Hoffmann <stefan991@gmail.com> | 2014-04-05 13:51:48 +0200 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-05 20:19:38 -0300 |
commit | e76249c813192aa082060d024b66f11d95d09b51 (patch) | |
tree | b85521e6135f487f4dd7b7df9d3f4965e37f0246 | |
parent | a8013f2bb1b7e62c273f7a7da58653aa82e9c5ca (diff) | |
download | rneovim-e76249c813192aa082060d024b66f11d95d09b51.tar.gz rneovim-e76249c813192aa082060d024b66f11d95d09b51.tar.bz2 rneovim-e76249c813192aa082060d024b66f11d95d09b51.zip |
Moved mch_get_host_name and renamed it to os_get_hostanme
-rw-r--r-- | src/eval.c | 6 | ||||
-rw-r--r-- | src/memline.c | 2 | ||||
-rw-r--r-- | src/os/env.c | 22 | ||||
-rw-r--r-- | src/os/os.h | 7 | ||||
-rw-r--r-- | src/os_unix.c | 27 | ||||
-rw-r--r-- | src/os_unix.h | 1 | ||||
-rw-r--r-- | src/os_unixx.h | 4 | ||||
-rw-r--r-- | test/unit/os/env.moon | 11 |
8 files changed, 44 insertions, 36 deletions
diff --git a/src/eval.c b/src/eval.c index 7dd578762c..978e3899ad 100644 --- a/src/eval.c +++ b/src/eval.c @@ -10530,11 +10530,11 @@ static void f_hlexists(typval_T *argvars, typval_T *rettv) */ static void f_hostname(typval_T *argvars, typval_T *rettv) { - char_u hostname[256]; + char hostname[256]; - mch_get_host_name(hostname, 256); + os_get_hostname(hostname, 256); rettv->v_type = VAR_STRING; - rettv->vval.v_string = vim_strsave(hostname); + rettv->vval.v_string = vim_strsave((char_u *)hostname); } /* diff --git a/src/memline.c b/src/memline.c index b9de707f60..05bc24e277 100644 --- a/src/memline.c +++ b/src/memline.c @@ -345,7 +345,7 @@ int ml_open(buf_T *buf) set_b0_fname(b0p, buf); (void)os_get_user_name((char *)b0p->b0_uname, B0_UNAME_SIZE); b0p->b0_uname[B0_UNAME_SIZE - 1] = NUL; - mch_get_host_name(b0p->b0_hname, B0_HNAME_SIZE); + os_get_hostname((char *)b0p->b0_hname, B0_HNAME_SIZE); b0p->b0_hname[B0_HNAME_SIZE - 1] = NUL; long_to_char(os_get_pid(), b0p->b0_pid); if (*buf->b_p_key != NUL) diff --git a/src/os/env.c b/src/os/env.c index 034a4ff3a0..a3053e3350 100644 --- a/src/os/env.c +++ b/src/os/env.c @@ -9,6 +9,10 @@ #include <crt_externs.h> #endif +#ifdef HAVE_SYS_UTSNAME_H +#include <sys/utsname.h> +#endif + const char *os_getenv(const char *name) { return getenv(name); @@ -60,3 +64,21 @@ long os_get_pid() #endif } +void os_get_hostname(char *hostname, size_t len) +{ +#ifdef HAVE_SYS_UTSNAME_H + struct utsname vutsname; + + if (uname(&vutsname) < 0) { + *hostname = '\0'; + } else { + strncpy(hostname, vutsname.nodename, len - 1); + hostname[len - 1] = '\0'; + } +#else + // TODO: Implement this for windows. See the implementation used in vim: + // https://code.google.com/p/vim/source/browse/src/os_win32.c?r=6b69d8dde19e32909f4ee3a6337e6a2ecfbb6f72#2899 + *hostname = '\0'; +#endif +} + diff --git a/src/os/os.h b/src/os/os.h index 1e874c29a5..8456f87473 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -87,6 +87,13 @@ char *os_getenvname_at_index(size_t index); /// /// @return the process ID. long os_get_pid(void); + +/// Get the hostname of the machine runing Neovim. +/// +/// @param hostname Buffer to store the hostname. +/// @param len Length of `hostname`. +void os_get_hostname(char *hostname, size_t len); + int os_get_usernames(garray_T *usernames); int os_get_user_name(char *s, size_t len); int os_get_uname(uid_t uid, char *s, size_t len); diff --git a/src/os_unix.c b/src/os_unix.c index f5bd1006b1..e90d52afa5 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -397,33 +397,6 @@ int vim_is_fastterm(char_u *name) || STRNICMP(name, "dtterm", 6) == 0; } -/* - * Insert host name is s[len]. - */ - -#ifdef HAVE_SYS_UTSNAME_H -void mch_get_host_name(char_u *s, int len) -{ - struct utsname vutsname; - - if (uname(&vutsname) < 0) - *s = NUL; - else - vim_strncpy(s, (char_u *)vutsname.nodename, len - 1); -} -#else /* HAVE_SYS_UTSNAME_H */ - -# ifdef HAVE_SYS_SYSTEMINFO_H -# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len) -# endif - -void mch_get_host_name(char_u *s, int len) -{ - gethostname((char *)s, len); - s[len - 1] = NUL; /* make sure it's terminated */ -} -#endif /* HAVE_SYS_UTSNAME_H */ - #if defined(USE_FNAME_CASE) || defined(PROTO) /* * Set the case of the file name, if it already exists. This will cause the diff --git a/src/os_unix.h b/src/os_unix.h index eaa38706ac..2f592c50af 100644 --- a/src/os_unix.h +++ b/src/os_unix.h @@ -19,7 +19,6 @@ int use_xterm_mouse(void); int vim_is_iris(char_u *name); int vim_is_vt300(char_u *name); int vim_is_fastterm(char_u *name); -void mch_get_host_name(char_u *s, int len); void slash_adjust(char_u *p); void fname_case(char_u *name, int len); void mch_copy_sec(char_u *from_file, char_u *to_file); diff --git a/src/os_unixx.h b/src/os_unixx.h index 1c9c8d8bae..576847761b 100644 --- a/src/os_unixx.h +++ b/src/os_unixx.h @@ -58,10 +58,6 @@ # include <sys/stream.h> #endif -#ifdef HAVE_SYS_UTSNAME_H -# include <sys/utsname.h> -#endif - #ifdef HAVE_SYS_SYSTEMINFO_H /* * foolish Sinix <sys/systeminfo.h> uses SYS_NMLN but doesn't include diff --git a/test/unit/os/env.moon b/test/unit/os/env.moon index b8405947d9..ad945f2206 100644 --- a/test/unit/os/env.moon +++ b/test/unit/os/env.moon @@ -9,6 +9,7 @@ const char *os_getenv(const char *name); int os_setenv(const char *name, const char *value, int override); char *os_getenvname_at_index(size_t index); long os_get_pid(void); +void os_get_hostname(char *hostname, size_t len); ]] NULL = ffi.cast 'void*', 0 @@ -104,3 +105,13 @@ describe 'env function', -> -- /proc is not avaliable on all systems, test if pid is nonzero. eq true, (env.os_get_pid! > 0) + describe 'os_get_hostname', -> + + it 'returns the hostname', -> + handle = io.popen 'hostname' + hostname = handle\read '*l' + handle\close! + hostname_buf = cstr 255, '' + env.os_get_hostname hostname_buf, 255 + eq hostname, (ffi.string hostname_buf) + |