summaryrefslogtreecommitdiff
path: root/lib/xgethostname.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>1996-11-05 04:38:41 +0000
committerJim Meyering <jim@meyering.net>1996-11-05 04:38:41 +0000
commit4d091498a4075bd6a91e7374dc569fd6ee00aaa8 (patch)
tree8a548cc44c2ac6c66a2abcb94572ca051ae84911 /lib/xgethostname.c
parent1e3de0b6d98c9810629e0348805b3c69bfe8c68f (diff)
downloadcoreutils-4d091498a4075bd6a91e7374dc569fd6ee00aaa8.tar.xz
Thu Oct 31 19:32:32 1996 Miles Bader <miles@gnu.ai.mit.edu>
[ENAMETOOLONG] (xgethostname): If gethostname returns an error other than buffer overflow, exit with an error message instead of allocating infinite amounts of space. [!EXIT_FAILURE] (EXIT_FAILURE): New macro. <errno.h>: New include. [!errno] (errno): New declaration.
Diffstat (limited to 'lib/xgethostname.c')
-rw-r--r--lib/xgethostname.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/lib/xgethostname.c b/lib/xgethostname.c
index d20967574..7038ea42c 100644
--- a/lib/xgethostname.c
+++ b/lib/xgethostname.c
@@ -1,5 +1,5 @@
/* xgethostname.c -- return current hostname with unlimited length
- Copyright (C) 1992 Free Software Foundation, Inc.
+ Copyright (C) 1992, 1996 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
@@ -23,6 +23,17 @@
#include <sys/types.h>
+#include <errno.h>
+#ifndef errno
+extern int errno;
+#endif
+
+#include "error.h"
+
+#ifndef EXIT_FAILURE
+# define EXIT_FAILURE 1
+#endif
+
int gethostname ();
char *xmalloc ();
char *xrealloc ();
@@ -42,10 +53,15 @@ xgethostname ()
hostname = xmalloc (size);
while (1)
{
+ errno = 0;
hostname[size - 1] = '\0';
err = gethostname (hostname, size);
if (err == 0 && hostname[size - 1] == '\0')
break;
+#ifdef ENAMETOOLONG
+ else if (err != 0 && errno != ENAMETOOLONG && errno != 0)
+ error (EXIT_FAILURE, errno, "gethostname");
+#endif
size *= 2;
hostname = xrealloc (hostname, size);
}