summaryrefslogtreecommitdiff
path: root/lib/sethostname.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-12-03 14:09:14 +0100
committerBruno Haible <bruno@clisp.org>2011-12-03 14:09:14 +0100
commit1917369d72235bea2b78b7d61f5ece0ea8e98d7e (patch)
treeda049e39589116fcfddedf99fbc14497bee3e278 /lib/sethostname.c
parentab8fbf44bf4633d1c326940ce1d0715fb63936d4 (diff)
downloadgnulib-1917369d72235bea2b78b7d61f5ece0ea8e98d7e.tar.gz
Tweak last commit.
* lib/sethostname.c: Don't include <string.h>. (sethostname): No need to copy the argument string to the stack. Don't call clearerr. Preserve errno when fprintf failed. * m4/sethostname.m4 (gl_FUNC_SETHOSTNAME): Comment about HOST_NAME_MAX. Don't invoke AC_REPLACE_FUNCS. * modules/sethostname (Link): Remove empty section. * doc/glibc-functions/sethostname.texi: Gnulib does not fix the ENOSYS failure problem.
Diffstat (limited to 'lib/sethostname.c')
-rw-r--r--lib/sethostname.c27
1 files changed, 12 insertions, 15 deletions
diff --git a/lib/sethostname.c b/lib/sethostname.c
index e1b7d74db5..3c2ec3c0c5 100644
--- a/lib/sethostname.c
+++ b/lib/sethostname.c
@@ -26,7 +26,6 @@
#include <errno.h>
#include <stdio.h>
-#include <string.h>
#include <limits.h>
/* Set up to LEN chars of NAME as system hostname.
@@ -43,12 +42,6 @@ sethostname (const char *name, size_t len)
return -1;
}
- /* NAME does not need to be null terminated so leave room to terminate
- regardless of input. */
- char hostname[HOST_NAME_MAX + 1];
- memcpy ((void *) hostname, (const void *) name, len);
- hostname[len] = '\0';
-
#ifdef __minix /* Minix */
{
FILE *hostf;
@@ -63,24 +56,28 @@ sethostname (const char *name, size_t len)
r = -1;
else
{
- fprintf (hostf, "%s\n", hostname);
+ fprintf (hostf, "%.*s\n", (int) len, name);
if (ferror (hostf))
{
- clearerr (hostf);
+ /* Close hostf, preserving the errno from the fprintf call. */
+ int saved_errno = errno;
+ fclose (hostf);
+ errno = saved_errno;
r = -1;
}
-
- /* use return value of fclose for function return value as it
- matches our needs. fclose will also set errno on
- failure */
- r = fclose (hostf);
+ else
+ {
+ if (fclose (hostf))
+ /* fclose sets errno on failure. */
+ r = -1;
+ }
}
return r;
}
#else
/* For platforms that we don't have a better option for, simply bail
- out */
+ out. */
errno = ENOSYS;
return -1;
#endif