Re: AF_LOCAL vs AF_UNIX

2012-04-12 Thread Bruno Haible
Hi Nikos,

Nikos Mavrogiannopoulos wrote:
>  I've noticed that some systems define AF_LOCAL in sys/socket.h while
> others AF_UNIX and most define both. Could gnulib provide both
> definitions (e.g. by this patch) to simplify their usage on various
> platforms?

Gnulib is meant to allow people to program according to POSIX (and, to a
lesser extent, GNU) APIs. Since POSIX [1] specifies the existence of AF_UNIX
but not of AF_LOCAL, there is no point for Gnulib to support AF_LOCAL.

Which are the platforms which provide AF_LOCAL but not AF_UNIX? I can't see
any.

I see some platforms which provide AF_UNIX but not AF_LOCAL (Minix, AIX,
HP-UX 10, OSF/1, mingw, MSVC 9, Plan9). But since Gnulib's objective is
to provide AF_UNIX, not AF_LOCAL, this list of platforms is not relevant.

Also, which kind of program can use AF_UNIX without using ?
According to gnulib/doc/posix-headers/sys_un.texi,  is missing
on native Windows, and Gnulib provides no replacement for it. The reason
is that Gnulib cannot just invent a new kind of socket, and things that
are done with AF_UNIX sockets on Unix are often better done with pipes
on Windows.

Bruno

[1] http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html




Re: ioctl.c

2012-04-12 Thread Bruno Haible
Hi,

> gl tests fail to compile, but after patching (i've attached the patch)
> they all pass too.

Thanks for the report. I can reproduce the compilation failure with a
gnulib testdir for just the module 'ioctl':

gcc-3 -mno-cygwin -DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1 
-I/usr/local/mingw/include -Wall  -g -O2 -MT ioctl.o -MD -MP -MF 
.deps/ioctl.Tpo -c -o ioctl.o ioctl.c
ioctl.c: In function `primary_ioctl':
ioctl.c:60: error: `HANDLE' undeclared (first use in this function)
ioctl.c:60: error: (Each undeclared identifier is reported only once
ioctl.c:60: error: for each function it appears in.)
ioctl.c:60: error: parse error before "_get_osfhandle"
make[3]: *** [ioctl.o] Error 1

I'm applying this patch, based on yours.


2012-04-12  LRN(tiny change)

ioctl: Fix compilation error on mingw.
* lib/ioctl.c: Include .
Also reported by Ray Satiro .

--- lib/ioctl.c.origThu Apr 12 12:44:11 2012
+++ lib/ioctl.c Thu Apr 12 12:36:59 2012
@@ -46,6 +46,10 @@
 
 # include 
 
+/* Get HANDLE.  */
+# define WIN32_LEAN_AND_MEAN
+# include 
+
 # include "fd-hook.h"
 /* Get _get_osfhandle.  */
 # include "msvc-nothrow.h"




Re: mingw and AC_SYS_LARGEFILE

2012-04-12 Thread Bruno Haible
Hi,

Ray Satiro wrote:
> The gnulib fstat doc says:
> "On platforms where off_t is a 32-bit type, stat may not correctly report the 
> size of files or block devices larger than 2 GB. The fix is to use the 
> AC_SYS_LARGEFILE macro. "
> 
> Recently it was discovered that the latest versions of wget mingw were not 
> handling large files properly. I'm overriding the gnulib fstat in my own 
> builds by doing redefines like this for the stat functions and structs
> #if defined fstat
> #undef fstat
> #endif
> #define fstat _fstati64
> 
> There's no _FILE_OFFSET_BITS stuff in my mingw headers. Do you have any 
> advice for this situation? I'm not subscribed to the list so please cc me.

Supporting 64-bit off_t requires, AFAICS, changes or overrides in the
following types and functions that exist in mingw:
  off_t
  : creat, open
  : fstat, lstat, stat
  : ftruncate, lseek
  : fgetpos, fopen, freopen, fseek, fsetpos, ftell, tmpfile
  : opendir, readdir, rewinddir, seekdir, telldir

Indeed native Windows has some primitives for 64-bit files, such as
fstat alternatives [1], lseek alternatives [2], fseek alternatives [3],
ftell alternatives [4].

My advice would be first to ask the currently active mingw development
group, namely the mingw64 project [5], whether they plan to add
"Large File Support" to their header files.

If no, then it could also be done in Gnulib. But obviously, more programs
can benefit from LFS support if it is done in mingw64.

Bruno

[1] http://msdn.microsoft.com/en-us/library/221w8e43.aspx
[2] http://msdn.microsoft.com/en-us/library/1yee101t.aspx
[3] http://msdn.microsoft.com/en-us/library/75yw9bf3.aspx
[4] http://msdn.microsoft.com/en-us/library/0ys3hc0b.aspx
[5] http://mingw-w64.sourceforge.net/




new module 'sys_resource'

2012-04-12 Thread Bruno Haible
It looks reasonable to define a getrusage() replacement for native Windows
in gnulib. libgfortran (in GCC) has such a replacement already.

First comes a  substitute.
Reference: 
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_resource.h.html


2012-04-12  Bruno Haible  

New module 'sys_resource'.
* lib/sys_resource.in.h: New file.
* m4/sys_resource_h.m4: New file.
* modules/sys_resource: New file.
* doc/posix-headers/sys_resource.texi: Mention the new module.

 lib/sys_resource.in.h 
/* Substitute for .
   Copyright (C) 2012 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
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see .  */

# if __GNUC__ >= 3
@PRAGMA_SYSTEM_HEADER@
# endif
@PRAGMA_COLUMNS@

#ifndef _@GUARD_PREFIX@_SYS_RESOURCE_H

#if @HAVE_SYS_RESOURCE_H@

/* On FreeBSD 5.0,  assumes prior inclusion of 
   and .  */
# include 
# include 

/* The include_next requires a split double-inclusion guard.  */
# @INCLUDE_NEXT@ @NEXT_SYS_RESOURCE_H@

#endif

#ifndef _@GUARD_PREFIX@_SYS_RESOURCE_H
#define _@GUARD_PREFIX@_SYS_RESOURCE_H

#if !@HAVE_SYS_RESOURCE_H@
/* A platform that lacks .  */

/* Get 'struct timeval'.  */
# include 

/* Define the RUSAGE_* constants.  */
# define RUSAGE_SELF 0
# define RUSAGE_CHILDREN -1

# ifdef __cplusplus
extern "C" {
# endif

# if !GNULIB_defined_struct_rusage
/* All known platforms that lack  also lack any declaration
   of struct rusage in any other header.  */
struct rusage
{
  struct timeval ru_utime;  /* CPU time used in user mode */
  struct timeval ru_stime;  /* CPU time used in system mode (kernel) */
  long ru_maxrss;
  long ru_ixrss;
  long ru_idrss;
  long ru_isrss;
  long ru_minflt;
  long ru_majflt;
  long ru_nswap;
  long ru_inblock;
  long ru_oublock;
  long ru_msgsnd;
  long ru_msgrcv;
  long ru_nsignals;
  long ru_nvcsw;
  long ru_nivcsw;
};
#  define GNULIB_defined_struct_rusage 1
# endif

# ifdef __cplusplus
}
# endif

#endif

#endif /* _@GUARD_PREFIX@_SYS_RESOURCE_H */
#endif /* _@GUARD_PREFIX@_SYS_RESOURCE_H */
 m4/sys_resource_h.m4 =
# sys_resource_h.m4 serial 1
dnl Copyright (C) 2012 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_HEADER_SYS_RESOURCE],
[
  AC_REQUIRE([gl_SYS_RESOURCE_H_DEFAULTS])
  dnl  is always overridden, because of GNULIB_POSIXCHECK.
  gl_CHECK_NEXT_HEADERS([sys/resource.h])
  if test $ac_cv_header_sys_resource_h = yes; then
HAVE_SYS_RESOURCE_H=1
  else
HAVE_SYS_RESOURCE_H=0
  fi
  AC_SUBST([HAVE_SYS_RESOURCE_H])
])

AC_DEFUN([gl_SYS_RESOURCE_MODULE_INDICATOR],
[
  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
  AC_REQUIRE([gl_SYS_RESOURCE_H_DEFAULTS])
  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
  dnl Define it also as a C macro, for the benefit of the unit tests.
  gl_MODULE_INDICATOR_FOR_TESTS([$1])
])

AC_DEFUN([gl_SYS_RESOURCE_H_DEFAULTS],
[
])
 modules/sys_resource =
Description:
A POSIX-like .

Files:
lib/sys_resource.in.h
m4/sys_resource_h.m4

Depends-on:
include_next
sys_time

configure.ac:
gl_HEADER_SYS_RESOURCE
AC_PROG_MKDIR_P

Makefile.am:
BUILT_SOURCES += sys/resource.h

# We need the following in order to create  when the system
# doesn't have one.
sys/resource.h: sys_resource.in.h $(top_builddir)/config.status
$(AM_V_at)$(MKDIR_P) sys
$(AM_V_GEN)rm -f $@-t $@ && \
{ echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
  sed -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
  -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
  -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
  -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
  -e 's|@''NEXT_SYS_RESOURCE_H''@|$(NEXT_SYS_RESOURCE_H)|g' \
  -e 's|@''HAVE_SYS_RESOURCE_H''@|$(HAVE_SYS_RESOURCE_H)|g' \
  < $(srcdir)/sys_resource.in.h; \
} > $@-t && \
mv -f $@-t $@
MOSTLYCLEANFILES += sys/resource.h sys/resource.h-t
MOSTLYCLEANDIRS += sys

Include:


License:
LGPL

Maintainer:
Bruno Haible
===

new module 'getrusage'

2012-04-12 Thread Bruno Haible
Here comes the 'getrusage' replacement for systems that don't have it
(Minix, Solaris 2.4, native Windows).

This implementation only fills in the times members of 'struct rusage',
because
  1. the other members are not standardized,
  2. the other members don't correspond to information that can be retrieved
 through native Windows calls.


2012-04-12  Bruno Haible  

New module 'getrusage'.
* lib/sys_resource.in.h: Include arg-nonnull.h, c++defs.h,
warn-on-use.h.
(getrusage): New declaration.
* lib/getrusage.c: New file.
* m4/getrusage.m4: New file.
* m4/sys_resource_h.m4 (gl_HEADER_SYS_RESOURCE): Test whether getrusage
is declared.
(gl_SYS_RESOURCE_H_DEFAULTS): Initialize GNULIB_GETRUSAGE,
HAVE_GETRUSAGE.
* modules/sys_resource (Depends-on): Add snippet/arg-nonnull,
snippet/c++defs, snippet/warn-on-use.
(Makefile.am): Update generation of sys/resource.h. Substitute
GNULIB_GETRUSAGE, HAVE_GETRUSAGE.
* modules/getrusage: New file.
* doc/posix-functions/getrusage.texi: Mention the new module.

=== lib/getrusage.c ===
/* getrusage replacement for systems which lack it.

   Copyright (C) 2012 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
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see .  */

/* Written by Bruno Haible, 2012.  */

#include 

/* Specification.  */
#include 

#include 
#include 

/* Get uint64_t.  */
#include 

#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__

# define WIN32_LEAN_AND_MEAN
# include 

#else

# include 
# include 
#endif

int
getrusage (int who, struct rusage *usage_p)
{
  if (who == RUSAGE_SELF || who == RUSAGE_CHILDREN)
{
  /* Clear all unsupported members of 'struct rusage'.  */
  memset (usage_p, '\0', sizeof (struct rusage));

#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  if (who == RUSAGE_SELF)
{
  /* Fill in the ru_utime and ru_stime members.  */
  FILETIME creation_time;
  FILETIME exit_time;
  FILETIME kernel_time;
  FILETIME user_time;

  if (GetProcessTimes (GetCurrentProcess (),
   &creation_time, &exit_time,
   &kernel_time, &user_time))
{
  /* Convert to microseconds, rounding.  */
  uint64_t kernel_usec =
uint64_t) kernel_time.dwHighDateTime << 32)
  | (uint64_t) kernel_time.dwLowDateTime)
 + 5) / 10;
  uint64_t user_usec =
uint64_t) user_time.dwHighDateTime << 32)
  | (uint64_t) user_time.dwLowDateTime)
 + 5) / 10;

  usage_p->ru_utime.tv_sec = user_usec / 100U;
  usage_p->ru_utime.tv_usec = user_usec % 100U;
  usage_p->ru_stime.tv_sec = kernel_usec / 100U;
  usage_p->ru_stime.tv_usec = kernel_usec % 100U;
}
}
#else
  /* Fill in the ru_utime and ru_stime members.  */
  {
struct tms time;

if (times (&time) != (clock_t) -1)
  {
/* Number of clock ticks per second.  */
unsigned int clocks_per_second = sysconf (_SC_CLK_TCK);

if (clocks_per_second > 0)
  {
clock_t user_ticks;
clock_t system_ticks;

uint64_t user_usec;
uint64_t system_usec;

if (who == RUSAGE_CHILDREN)
  {
user_ticks   = time.tms_cutime;
system_ticks = time.tms_cstime;
  }
else
  {
user_ticks   = time.tms_utime;
system_ticks = time.tms_stime;
  }

user_usec =
  (((uint64_t) user_ticks * (uint64_t) 100U)
   + clocks_per_second / 2) / clocks_per_second;
system_usec =
  (((uint64_t) system_ticks * (uint64_t) 100U)
   + clocks_per_second / 2) / clocks_per_second;

usage_p->ru_utime.tv_sec = user_usec / 100U;
usage_p->ru_utime.tv_usec = user_usec % 100U;
usage_p->ru_stime.tv_sec = system_u

obstack_free(obs, addr) not portable when addr != NULL

2012-04-12 Thread Jeffrey Kegler
Studying the code for obstack_free(), I cannot convince myself that
obstack_free(obs, addr) is C89 conformant when addr != NULL.  To determine
if addr is in the current chunk, the code does pointer comparisons with the
addresses of the chunk.  These are only well-defined if addr is in the
chunk.  As I read Section A7.9 (C programming language, 2nd ed., p. 206) if
addr is not in the chunk an implementation's behavior is undefined -- it
could abend or to return a spurious positive.  In the case of a spurious
positive, the pointer would be copied into the chunk's header, and C89
would not require its behavior when de-referenced to be consistent with its
behavior under pointer comparison -- or even for its behavior in future
pointer comparisons to be consistent with previous ones.

I recognize this is a bit of a quibble, since in current practice the
pointers will all come from the arena of a single memory allocator, and
comparisons will (almost?) always behave as if from a flat address space.
But C89 does not seem to require this.  The memory allocator specified for
obstack COULD return addresses from multiple incomparable address spaces --
not likely, but it could.  Or a future architecture might return to the
strangely segmented address spaces of yesteryear.

Possible fixes: 1)  Document that the behavior is unportable, and under
what conditions it can be expected to work.  At a minimum, describe the
behavior required of the memory allocator.  2.) Remove/replace the feature.

Thanks, jeffrey kegler


Re: obstack_free(obs, addr) not portable when addr != NULL

2012-04-12 Thread Paul Eggert
On 04/12/2012 05:48 PM, Jeffrey Kegler wrote:
> 1)  Document that the behavior is unportable, and under what conditions it 
> can be expected to work.
> At a minimum, describe the behavior required of the memory allocator.  2.) 
> Remove/replace the feature.

(1) is probably safer, given how much the feature is used.
For now, I pushed this:

>From f8fea966d67a6ba06231689e63f668bd55ee5797 Mon Sep 17 00:00:00 2001
From: Paul Eggert 
Date: Thu, 12 Apr 2012 18:56:54 -0700
Subject: [PATCH] README: document pointer comparison assumption

* README (Portability guidelines): Document assumption about
pointer comparisons, in response to a recent bug-gnulib comment by
Jeffrey Kegler.
---
 ChangeLog |7 +++
 README|   22 +-
 2 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ed9b98c..ce6d19a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2012-04-12  Paul Eggert  
+
+   README: document pointer comparison assumption
+   * README (Portability guidelines): Document assumption about
+   pointer comparisons, in response to a recent bug-gnulib comment by
+   Jeffrey Kegler.
+
 2012-04-12  Bruno Haible  
 
Tests for module 'getrusage'.
diff --git a/README b/README
index 672964f..4bf10dd 100644
--- a/README
+++ b/README
@@ -258,15 +258,19 @@ as well.  Gnulib code makes the following additional 
assumptions:
  * There are no "holes" in integer values: all the bits of an integer
contribute to its value in the usual way.
 
- * If two nonoverlapping objects have sizes S and T represented as
-   size_t values, then S + T cannot overflow.  This assumption is true
-   for all practical hosts with flat address spaces, but it is not
-   always true for hosts with segmented address spaces.
-
- * If an existing object has size S, and if T is sufficiently small
-   (e.g., 8 KiB), then S + T cannot overflow.  Overflow in this case
-   would mean that the rest of your program fits into T bytes, which
-   can't happen in realistic flat-address-space hosts.
+ * Addresses and sizes behave as if objects reside in a flat address space.
+   In particular:
+
+   - If two nonoverlapping objects have sizes S and T represented as
+ size_t values, then S + T cannot overflow.
+
+   - A pointer P points within an object O if and only if
+ (char *) &O <= (char *) P && (char *) P < (char *) (&O + 1).
+
+   - If an existing object has size S, and if T is sufficiently small
+ (e.g., 8 KiB), then S + T cannot overflow.  Overflow in this case
+ would mean that the rest of your program fits into T bytes, which
+ can't happen in realistic flat-address-space hosts.
 
  * Objects with all bits zero are treated as 0 or NULL.  For example,
memset (A, 0, sizeof A) initializes an array A of pointers to NULL.
-- 
1.7.6.5