From e76249c813192aa082060d024b66f11d95d09b51 Mon Sep 17 00:00:00 2001 From: Stefan Hoffmann Date: Sat, 5 Apr 2014 13:51:48 +0200 Subject: Moved mch_get_host_name and renamed it to os_get_hostanme --- src/eval.c | 6 +++--- src/memline.c | 2 +- src/os/env.c | 22 ++++++++++++++++++++++ src/os/os.h | 7 +++++++ src/os_unix.c | 27 --------------------------- src/os_unix.h | 1 - src/os_unixx.h | 4 ---- 7 files changed, 33 insertions(+), 36 deletions(-) (limited to 'src') 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 #endif +#ifdef HAVE_SYS_UTSNAME_H +#include +#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 #endif -#ifdef HAVE_SYS_UTSNAME_H -# include -#endif - #ifdef HAVE_SYS_SYSTEMINFO_H /* * foolish Sinix uses SYS_NMLN but doesn't include -- cgit