unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Re: gethostname max len
@ 2004-03-26 14:47 Nelson H. F. Beebe
  2004-03-27 22:48 ` Kevin Ryde
  0 siblings, 1 reply; 6+ messages in thread
From: Nelson H. F. Beebe @ 2004-03-26 14:47 UTC (permalink / raw)
  Cc: beebe

Discussions on this list earlier this week debated how to handle the
return from gethostname(), which might produce a string of
platform-dependent size.

One correspondent suggested using PATH_MAX.

This is, I believe, unwise, for at least these reasons:

(1) There has been great historic variability in the name of the
    parameter that holds the size of the longest possible path; see
    below. 

(2) The ISO Standard C name for that parameter is not PATH_MAX, but
    this one (from section 7.19.1 of the 1999 Standard):

>> ...
>> 		FILENAME_MAX
>> 
>>      which expands to an integer constant expression that is the size
>>      needed for an array of char large enough to hold the longest file
>>      name string that the implementation
>> ...

(3) More seriously, that limit is not O/S or compiler dependent, but
    rather, filesystem dependent.  A Unix system might support
    4096-character paths in its own filesystem, then mount a PC DOS
    system with 8+3 limits on filenames, and a 128-character limit on
    the path.

The proper way to handle this is via the POSIX (IEEE Std 1003.1-2001)
pathconf() and fpathconf() library calls:

	#include <unistd.h>	/* POSIX header file */

	long int n;

	n = pathconf("/path/to/file", _PC_PATH_MAX);
	n = fpathconf(fileno(stdin),  _PC_PATH_MAX);

Only if these routines, their header file, and the symbol
_PC_PATH_MAX, are not available (as determined by GNU autoconf),
should code fall back to using FILENAME_MAX, and then further caution
must be applied, because HP-UX 10 and 11 define this in <stdio.h> with
the value 14, far below the value of 1024 that their filesystems
support.

The following tests show that the return values from the POSIX
functions also need to be checked carefully:

% cat pathconf.c
/***********************************************************************
[26-Mar-2004]
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
show_fpathconf(int fd)
{
    printf("fpathconf(): maximum path for file descriptor %d\t:\t%7ld\n", fd, (long)fpathconf(fd, _PC_PATH_MAX));
}

void
show_pathconf(const char *s)
{
    printf("pathconf(): maximum path in %-15s\t:\t%7ld\n", s, (long)pathconf(s, _PC_PATH_MAX));
}

int
main(void)
{
    printf("FILENAME_MAX\t\t\t\t\t:\t%7d\n", FILENAME_MAX);

    show_pathconf("/");
    show_pathconf("/tmp");
    show_pathconf("/var/tmp");
    show_pathconf("./");
    show_fpathconf(fileno(stdin));
    show_fpathconf(fileno(stdout));
    show_fpathconf(fileno(stderr));

    return (EXIT_SUCCESS);
}

The following experiments illustrate some of the variations to be
found; return values of -1 mean that the value is indeterminate (so
the caller must be prepared to handle a dynamically-sized string):

On HP-UX 11.11 PA-RISC 8700:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :            14
	pathconf(): maximum path in /                   :          1023
	pathconf(): maximum path in /tmp                :          1023
	pathconf(): maximum path in /var/tmp            :          1023
	pathconf(): maximum path in ./                  :          1024
	fpathconf(): maximum path for file descriptor 0 :            -1
	fpathconf(): maximum path for file descriptor 1 :            -1
	fpathconf(): maximum path for file descriptor 2 :            -1

On FreeBSD 5.1 Alpha:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :          1024
	pathconf(): maximum path in /                   :          1024
	pathconf(): maximum path in /tmp                :          1024
	pathconf(): maximum path in /var/tmp            :          1024
	pathconf(): maximum path in ./                  :            -1
	fpathconf(): maximum path for file descriptor 0 :          1024
	fpathconf(): maximum path for file descriptor 1 :          1024
	fpathconf(): maximum path for file descriptor 2 :          1024

On GNU/Linux IA-64:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :          4096
	pathconf(): maximum path in /                   :          4096
	pathconf(): maximum path in /tmp                :          4096
	pathconf(): maximum path in /var/tmp            :          4096
	pathconf(): maximum path in ./                  :          4096
	fpathconf(): maximum path for file descriptor 0 :          4096
	fpathconf(): maximum path for file descriptor 1 :          4096
	fpathconf(): maximum path for file descriptor 2 :          4096

On SGI IRIX MIPS and Solaris 9 SPARC:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :          1024
	pathconf(): maximum path in /                   :          1024
	pathconf(): maximum path in /tmp                :          1024
	pathconf(): maximum path in /var/tmp            :          1024
	pathconf(): maximum path in ./                  :          1024
	fpathconf(): maximum path for file descriptor 0 :          1024
	fpathconf(): maximum path for file descriptor 1 :          1024
	fpathconf(): maximum path for file descriptor 2 :          1024

On Compaq/DEC Alpha OSF/1 4.0 and 5.1:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :           255
	pathconf(): maximum path in /                   :          1023
	pathconf(): maximum path in /tmp                :          1023
	pathconf(): maximum path in /var/tmp            :          1023
	pathconf(): maximum path in ./                  :          1024
	fpathconf(): maximum path for file descriptor 0 :            -1
	fpathconf(): maximum path for file descriptor 1 :            -1
	fpathconf(): maximum path for file descriptor 2 :            -1

On IBM RS/6000 AIX 4.2:
	cc pathconf.c && ./a.out
	FILENAME_MAX                                    :           255
	pathconf(): maximum path in /                   :          1023
	pathconf(): maximum path in /tmp                :          1023
	pathconf(): maximum path in /var/tmp            :          1023
	pathconf(): maximum path in ./                  :          1024
	fpathconf(): maximum path for file descriptor 0 :          1023
	fpathconf(): maximum path for file descriptor 1 :          1023
	fpathconf(): maximum path for file descriptor 2 :          1023

Here is a snippet from my own code that notes the historical
variation:

--------	-------------	------------------------------------------
Name		Definition	System
--------	-------------	------------------------------------------
FNMAX		<stdio.h>	PCC-20
MAXPATH		<dir.h>		Turbo C 2.0, C and C++ 3.0, and TopSpeed C
_MAX_PATH	<stdlib.h>	Microsoft C 5.0, 6.0, 7.0 and TopSpeed C
MAX_PATHLEN	<sys/param.h>	Sun OS (4.2BSD), 4.3BSD, Gould UTX/32,
				HPUX, KCC-20, AIX (RT, RS, PS/2, 370),
				HP/Apollo DomainOS, DEC Alpha (OSF/1)
PATH_MAX	<stdio.h>	SYS V (Silicon Graphics)
PATH_MAX	<limits.h>	POSIX, DEC Alpha (OSF/1)
FILENAME_MAX	<stdio.h>	Intel RMX, NeXT Mach, Turbo C/C++ 3.0,
				Standard C
--------	-------------	------------------------------------------

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe  -
-------------------------------------------------------------------------------


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread
* gethostname max len
@ 2004-03-21 22:22 Kevin Ryde
  2004-03-22  1:05 ` Wolfgang Jaehrling
  0 siblings, 1 reply; 6+ messages in thread
From: Kevin Ryde @ 2004-03-21 22:22 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 268 bytes --]

        * posix.c (scm_gethostname): Use sysconf(_SC_HOST_NAME_MAX) and
        MAXHOSTNAMELEN when available.

It's possible MAXHOSTNAMELEN is enough in practice, but posix
specifies HOST_NAME_MAX instead of MAXHOSTNAMELEN, so it seems
worthwhile checking that too.


[-- Attachment #2: posix.c.maxhostname.diff --]
[-- Type: text/plain, Size: 1451 bytes --]

--- posix.c.~1.127.~	2004-03-18 14:02:04.000000000 +1000
+++ posix.c	2004-03-22 08:19:40.000000000 +1000
@@ -114,6 +114,14 @@
 #  include <crypt.h>
 #endif
 
+#ifdef HAVE_NETDB_H
+#include <netdb.h>      /* for MAXHOSTNAMELEN on Solaris */
+#endif
+
+#ifdef HAVE_SYS_PARAM_H
+#include <sys/param.h>  /* for MAXHOSTNAMELEN */
+#endif
+
 #if HAVE_SYS_RESOURCE_H
 #  include <sys/resource.h>
 #endif
@@ -1746,12 +1754,31 @@
 	    "Return the host name of the current processor.")
 #define FUNC_NAME s_scm_gethostname
 {
-  /* 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
-     large enough.  */
-  int len = 256, res, save_errno;
+  int len, res, save_errno;
   char *p = scm_malloc (len);
   SCM name;
 
+  /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
+     large enough.  */
+  len = 256;
+
+  /* various systems define MAXHOSTNAMELEN (including Solaris in fact) */
+#ifdef MAXHOSTNAMELEN
+  len = MAXHOSTNAMELEN;
+#endif
+
+  /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
+     which may reflect a particular kernel configuration.
+     Must watch out for this existing but giving -1, as happens for instance
+     in gnu/linux glibc 2.3.2.  */
+#if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
+  {
+    long n = sysconf (_SC_HOST_NAME_MAX);
+    if (n != -1L)
+      len = n;
+  }
+#endif
+
   res = gethostname (p, len);
   while (res == -1 && errno == ENAMETOOLONG)
     {

[-- Attachment #3: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-03-27 22:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-26 14:47 gethostname max len Nelson H. F. Beebe
2004-03-27 22:48 ` Kevin Ryde
  -- strict thread matches above, loose matches on Subject: below --
2004-03-21 22:22 Kevin Ryde
2004-03-22  1:05 ` Wolfgang Jaehrling
2004-03-22  1:02   ` Kevin Ryde
2004-03-22  1:34     ` Wolfgang Jaehrling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).