From 71c6bcbb0b298483c7e3abab7e5d88a25c4d0c7c Mon Sep 17 00:00:00 2001 From: "Christopher J. Morrone" <morrone2@llnl.gov> Date: Mon, 17 Jul 2006 23:16:26 +0000 Subject: [PATCH] Add xshort_hostname function. --- src/common/xstring.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/common/xstring.c b/src/common/xstring.c index ae1b93d3f76..65bfe544fbb 100644 --- a/src/common/xstring.c +++ b/src/common/xstring.c @@ -68,6 +68,7 @@ strong_alias(xstrdup, slurm_xstrdup); strong_alias(xstrndup, slurm_xstrndup); strong_alias(xbasename, slurm_xbasename); strong_alias(_xstrsubstitute, slurm_xstrsubstitute); +strong_alias(xshort_hostname, slurm_xshort_hostname); /* * Ensure that a string has enough space to add 'needed' characters. @@ -314,3 +315,29 @@ void _xstrsubstitute(char **str, const char *pattern, const char *replacement) strcpy((*str)+pat_offset+rep_len, end_copy); xfree(end_copy); } + +/* xshort_hostname + * Returns an xmalloc'd string containing the hostname + * of the local machine. The hostname contains only + * the short version of the hostname (e.g. "linux123.foo.bar" + * becomes "linux123") + * + * Returns NULL on error. + */ +char *xshort_hostname(void) +{ + int error_code; + char *dot_ptr, path_name[1024]; + + error_code = gethostname (path_name, sizeof(path_name)); + if (error_code) + return NULL; + + dot_ptr = strchr (path_name, '.'); + if (dot_ptr == NULL) + dot_ptr = path_name + strlen(path_name); + else + dot_ptr[0] = '\0'; + + return xstrdup(path_name); +} -- GitLab