From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Nelson H. F. Beebe" Newsgroups: gmane.lisp.guile.devel Subject: Re: gethostname max len Date: Fri, 26 Mar 2004 07:47:42 -0700 (MST) Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: NNTP-Posting-Host: deer.gmane.org X-Trace: sea.gmane.org 1080312790 14991 80.91.224.253 (26 Mar 2004 14:53:10 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 26 Mar 2004 14:53:10 +0000 (UTC) Cc: beebe@math.utah.edu Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Fri Mar 26 15:52:58 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1B6shc-0000ca-00 for ; Fri, 26 Mar 2004 15:52:57 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1B6sfF-0001pI-9z for guile-devel@m.gmane.org; Fri, 26 Mar 2004 09:50:29 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.30) id 1B6sdD-0001SY-Ki for guile-devel@gnu.org; Fri, 26 Mar 2004 09:48:23 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.30) id 1B6scg-0001J0-Cj for guile-devel@gnu.org; Fri, 26 Mar 2004 09:48:21 -0500 Original-Received: from [128.110.198.2] (helo=sunshine.math.utah.edu) by monty-python.gnu.org with esmtp (TLSv1:DES-CBC3-SHA:168) (Exim 4.30) id 1B6scf-0001Id-Sz for guile-devel@gnu.org; Fri, 26 Mar 2004 09:47:50 -0500 Original-Received: from psi.math.utah.edu (IDENT:GGIKaOHDQ8RI81SOk2u7r4RnKAcB1YuU@psi.math.utah.edu [128.110.198.32]) by sunshine.math.utah.edu (8.12.10/8.12.10) with ESMTP id i2QElg1q013965; Fri, 26 Mar 2004 07:47:42 -0700 (MST) Original-Received: from psi.math.utah.edu (IDENT:1rC/s26nwTgZgtk2k3/pgmnT0LqUJoVm@localhost [127.0.0.1]) by psi.math.utah.edu (8.12.10/8.12.10) with ESMTP id i2QElgqp004263; Fri, 26 Mar 2004 07:47:42 -0700 (MST) Original-Received: (from beebe@localhost) by psi.math.utah.edu (8.12.10/8.12.10/Submit) id i2QElgJc004261; Fri, 26 Mar 2004 07:47:42 -0700 (MST) Original-To: guile-devel@gnu.org X-US-Mail: "Center for Scientific Computing, Department of Mathematics, 110 LCB, University of Utah, 155 S 1400 E RM 233, Salt Lake City, UT 84112-0090, USA" X-Telephone: +1 801 581 5254 X-FAX: +1 801 585 1640, +1 801 581 4148 X-URL: http://www.math.utah.edu/~beebe X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:3575 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:3575 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 /* 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 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 #include #include 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 PCC-20 MAXPATH Turbo C 2.0, C and C++ 3.0, and TopSpeed C _MAX_PATH Microsoft C 5.0, 6.0, 7.0 and TopSpeed C MAX_PATHLEN 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 SYS V (Silicon Graphics) PATH_MAX POSIX, DEC Alpha (OSF/1) FILENAME_MAX 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