[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
coreutils hostname / gethostname fixes
From: |
Paul Eggert |
Subject: |
coreutils hostname / gethostname fixes |
Date: |
Mon, 02 Aug 2004 14:56:01 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) |
I installed these fixes for coreutils hostname, including its use of
gethostname. The code didn't work for host names longer than INT_MAX
(possible on GNU/Hurd, perhaps?), and while I was fixing this I
noticed that it didn't properly work around the SunOS 5.5 bug in some
cases. There are a couple of other very minor cleanups (e.g., with
the patch it no longer assumesthat errno can't ever validly equal
9999).
2004-08-02 Paul Eggert <address@hidden>
* lib/xgethostname.c: Don't include <sys/types.h> or "exit.h";
no longer needed.
(errno): Remove decl; we now assume C89 or better.
Include unistd.h if available, for gethostname.
(ENAMETOOLONG): Define to 0, not 9999, to avoid colliding with
existing errno values if any.
(gethostname): Remove decl, since unistd.h declares it (or doesn't,
in which case it's an older system and it should just work).
(xgethostname): Don't assume host name length is less than INT_MAX.
Exit if malloc fails, just as the comment says.
* src/hostname.c: Include "xgethostname.h".
(xgethostname): Remove decl; xgethostname.h has it.
(sethostname) [!defined(HAVE_SETHOSTNAME) && defined(HAVE_SYSINFO)
&& defined (HAVE_SYS_SYSTEMINFO_H) && defined(HAVE_LIMITS_H)]: Use
prototypes rather than K&R form. Assume any negative value from
sysinfo denotes failure, not just -1.
(main): Simplify use of sethostname.
Index: lib/xgethostname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/lib/xgethostname.c,v
retrieving revision 1.19
diff -p -u -r1.19 xgethostname.c
--- lib/xgethostname.c 16 Oct 2003 06:26:56 -0000 1.19
+++ lib/xgethostname.c 2 Aug 2004 21:47:55 -0000
@@ -1,5 +1,7 @@
/* xgethostname.c -- return current hostname with unlimited length
- Copyright (C) 1992, 1996, 2000, 2001, 2003 Free Software Foundation, Inc.
+
+ Copyright (C) 1992, 1996, 2000, 2001, 2003, 2004 Free Software
+ Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -25,60 +27,56 @@
#include "xgethostname.h"
#include <stdlib.h>
-#include <sys/types.h>
-
#include <errno.h>
-#ifndef errno
-extern int errno;
+
+#if HAVE_UNISTD_H
+# include <unistd.h>
#endif
#include "error.h"
-#include "exit.h"
#include "xalloc.h"
#ifndef ENAMETOOLONG
-# define ENAMETOOLONG 9999
+# define ENAMETOOLONG 0
#endif
-int gethostname ();
-
#ifndef INITIAL_HOSTNAME_LENGTH
# define INITIAL_HOSTNAME_LENGTH 34
#endif
/* Return the current hostname in malloc'd storage.
If malloc fails, exit.
- Upon any other failure, return NULL. */
+ Upon any other failure, return NULL and set errno. */
char *
xgethostname (void)
{
- char *hostname;
- size_t size;
+ char *hostname = NULL;
+ size_t size = INITIAL_HOSTNAME_LENGTH;
- size = INITIAL_HOSTNAME_LENGTH;
- /* Use size + 1 here rather than size to work around the bug
- in SunOS 5.5's gethostname whereby it NUL-terminates HOSTNAME
- even when the name is longer than the supplied buffer. */
- hostname = xmalloc (size + 1);
while (1)
{
- int k = size - 1;
- int err;
-
+ /* Use SIZE_1 here rather than SIZE to work around the bug in
+ SunOS 5.5's gethostname whereby it NUL-terminates HOSTNAME
+ even when the name is as long as the supplied buffer. */
+ size_t size_1;
+
+ hostname = x2realloc (hostname, &size);
+ size_1 = size - 1;
+ hostname[size_1 - 1] = '\0';
errno = 0;
- hostname[k] = '\0';
- err = gethostname (hostname, size);
- if (err >= 0 && hostname[k] == '\0')
- break;
- else if (err < 0 && errno != ENAMETOOLONG && errno != 0)
+
+ if (gethostname (hostname, size_1) == 0)
+ {
+ if (! hostname[size_1 - 1])
+ break;
+ }
+ else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL)
{
int saved_errno = errno;
free (hostname);
errno = saved_errno;
return NULL;
}
- size *= 2;
- hostname = xrealloc (hostname, size + 1);
}
return hostname;
Index: src/hostname.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/hostname.c,v
retrieving revision 1.52
diff -p -u -r1.52 hostname.c
--- src/hostname.c 21 Jun 2004 15:03:35 -0000 1.52
+++ src/hostname.c 2 Aug 2004 21:45:11 -0000
@@ -25,6 +25,7 @@
#include "long-options.h"
#include "error.h"
#include "quote.h"
+#include "xgethostname.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "hostname"
@@ -40,23 +41,15 @@ int sethostname ();
# include <sys/systeminfo.h>
int
-sethostname (name, namelen)
- char *name;
- int namelen;
+sethostname (char *name, size_t namelen)
{
/* Using sysinfo() is the SVR4 mechanism to set a hostname. */
- int result;
-
- result = sysinfo (SI_SET_HOSTNAME, name, namelen);
-
- return (result == -1 ? result : 0);
+ return (sysinfo (SI_SET_HOSTNAME, name, namelen) < 0 ? -1 : 0);
}
# define HAVE_SETHOSTNAME 1 /* Now we have it... */
#endif
-char *xgethostname ();
-
/* The name this program was run with. */
char *program_name;
@@ -109,11 +102,8 @@ main (int argc, char **argv)
#ifdef HAVE_SETHOSTNAME
if (argc == 2)
{
- int err;
-
/* Set hostname to argv[1]. */
- err = sethostname (argv[1], strlen (argv[1]));
- if (err != 0)
+ if (sethostname (argv[1], strlen (argv[1])) != 0)
error (EXIT_FAILURE, errno, _("cannot set hostname to `%s'"), argv[1]);
exit (EXIT_SUCCESS);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- coreutils hostname / gethostname fixes,
Paul Eggert <=