add gnulib `times' module
authorAndy Wingo <wingo@pobox.com>
Mon, 18 Feb 2013 18:06:34 +0000 (19:06 +0100)
committerAndy Wingo <wingo@pobox.com>
Mon, 18 Feb 2013 18:06:34 +0000 (19:06 +0100)
* lib: Add `times' module.  Should fix socket.c on MinGW.  Thanks to Eli
  Zaretskii for the report.

lib/Makefile.am
lib/sys_times.in.h [new file with mode: 0644]
lib/times.c [new file with mode: 0644]
m4/gnulib-cache.m4
m4/gnulib-comp.m4
m4/sys_times_h.m4 [new file with mode: 0644]
m4/times.m4 [new file with mode: 0644]

index c99a755..2fe0e82 100644 (file)
@@ -21,7 +21,7 @@
 # the same distribution terms as the rest of that program.
 #
 # Generated by gnulib-tool.
-# Reproduce by: gnulib-tool --import --dir=. --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil clock-time close connect dirfd duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen localcharset locale log1p maintainer-makefile malloc-gnu malloca nl_langinfo nproc open pipe2 putenv recv recvfrom regex rename select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar
+# Reproduce by: gnulib-tool --import --dir=. --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil clock-time close connect dirfd duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen localcharset locale log1p maintainer-makefile malloc-gnu malloca nl_langinfo nproc open pipe2 putenv recv recvfrom regex rename select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat times trunc verify vsnprintf warnings wchar
 
 AUTOMAKE_OPTIONS = 1.5 gnits subdir-objects
 
@@ -2526,6 +2526,37 @@ EXTRA_DIST += sys_time.in.h
 
 ## end   gnulib module sys_time
 
+## begin gnulib module sys_times
+
+BUILT_SOURCES += sys/times.h
+
+# We need the following in order to create <sys/times.h> when the system
+# doesn't have one that works with the given compiler.
+sys/times.h: sys_times.in.h $(top_builddir)/config.status $(WARN_ON_USE_H) $(ARG_NONNULL_H)
+       $(AM_V_at)$(MKDIR_P) sys
+       $(AM_V_GEN)rm -f $@-t $@ && \
+       { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
+         sed -e 's|@''GUARD_PREFIX''@|GL|g' \
+             -e 's/@''HAVE_SYS_TIMES_H''@/$(HAVE_SYS_TIMES_H)/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_TIMES_H''@|$(NEXT_SYS_TIMES_H)|g' \
+             -e 's/@''GNULIB_TIMES''@/$(GNULIB_TIMES)/g' \
+             -e 's|@''HAVE_STRUCT_TMS''@|$(HAVE_STRUCT_TMS)|g' \
+             -e 's|@''HAVE_TIMES''@|$(HAVE_TIMES)|g' \
+             -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
+             -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
+             < $(srcdir)/sys_times.in.h; \
+       } > $@-t && \
+       mv $@-t $@
+MOSTLYCLEANFILES += sys/times.h sys/times.h-t
+MOSTLYCLEANDIRS += sys
+
+EXTRA_DIST += sys_times.in.h
+
+## end   gnulib module sys_times
+
 ## begin gnulib module sys_types
 
 BUILT_SOURCES += sys/types.h
@@ -2628,6 +2659,15 @@ EXTRA_libgnu_la_SOURCES += time_r.c
 
 ## end   gnulib module time_r
 
+## begin gnulib module times
+
+
+EXTRA_DIST += times.c
+
+EXTRA_libgnu_la_SOURCES += times.c
+
+## end   gnulib module times
+
 ## begin gnulib module trunc
 
 
diff --git a/lib/sys_times.in.h b/lib/sys_times.in.h
new file mode 100644 (file)
index 0000000..8ea8088
--- /dev/null
@@ -0,0 +1,80 @@
+/* Provide a sys/times.h header file.
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <simon@josefsson.org>, 2008.  */
+
+/* This file is supposed to be used on platforms where <sys/times.h>
+   is missing.  */
+
+#ifndef _@GUARD_PREFIX@_SYS_TIMES_H
+
+# if __GNUC__ >= 3
+@PRAGMA_SYSTEM_HEADER@
+# endif
+@PRAGMA_COLUMNS@
+
+# if @HAVE_SYS_TIMES_H@
+#  @INCLUDE_NEXT@ @NEXT_SYS_TIMES_H@
+# endif
+
+# define _@GUARD_PREFIX@_SYS_TIMES_H
+
+/* Get clock_t.
+   But avoid namespace pollution on glibc systems.  */
+# ifndef __GLIBC__
+#  include <time.h>
+# endif
+
+/* The definition of _GL_ARG_NONNULL is copied here.  */
+
+/* The definition of _GL_WARN_ON_USE is copied here.  */
+
+# ifdef __cplusplus
+extern "C" {
+# endif
+
+# if !@HAVE_STRUCT_TMS@
+#  if !GNULIB_defined_struct_tms
+  /* Structure describing CPU time used by a process and its children.  */
+  struct tms
+  {
+    clock_t tms_utime;          /* User CPU time.  */
+    clock_t tms_stime;          /* System CPU time.  */
+
+    clock_t tms_cutime;         /* User CPU time of dead children.  */
+    clock_t tms_cstime;         /* System CPU time of dead children.  */
+  };
+#   define GNULIB_defined_struct_tms 1
+#  endif
+# endif
+
+# if @GNULIB_TIMES@
+#  if !@HAVE_TIMES@
+  extern clock_t times (struct tms *buffer) _GL_ARG_NONNULL ((1));
+#  endif
+# elif defined GNULIB_POSIXCHECK
+#  undef times
+#  if HAVE_RAW_DECL_TIMES
+_GL_WARN_ON_USE (times, "times is unportable - "
+                 "use gnulib module times for portability");
+#  endif
+# endif
+
+# ifdef __cplusplus
+}
+# endif
+
+#endif                          /* _@GUARD_PREFIX@_SYS_TIMES_H */
diff --git a/lib/times.c b/lib/times.c
new file mode 100644 (file)
index 0000000..cf2fa1d
--- /dev/null
@@ -0,0 +1,66 @@
+/* Get process times
+
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Simon Josefsson <simon@josefsson.org>, 2008.  */
+
+#include <config.h>
+
+/* Get times prototype. */
+#include <sys/times.h>
+
+/* Get round. */
+#include <math.h>
+
+/* Get GetProcessTimes etc. */
+#include <windows.h>
+
+static clock_t
+filetime2clock (FILETIME time)
+{
+  float f;
+
+  /* We have a 64-bit value, in the form of two DWORDS aka unsigned
+     int, counting the number of 100-nanosecond intervals.  We need to
+     convert these to clock ticks.  Older POSIX uses CLK_TCK to
+     indicate the number of clock ticks per second while modern POSIX
+     uses sysconf(_SC_CLK_TCK).  Mingw32 does not appear to have
+     sysconf(_SC_CLK_TCK), but appears to have CLK_TCK = 1000 so we
+     use it.  Note that CLOCKS_PER_SEC constant does not apply here,
+     it is for use with the clock function.  */
+
+  f = (unsigned long long) time.dwHighDateTime << 32;
+  f += time.dwLowDateTime;
+  f = f * CLK_TCK / 10000000;
+  return (clock_t) round (f);
+}
+
+clock_t
+times (struct tms * buffer)
+{
+  FILETIME creation_time, exit_time, kernel_time, user_time;
+
+  if (GetProcessTimes (GetCurrentProcess (), &creation_time, &exit_time,
+                       &kernel_time, &user_time) == 0)
+    return (clock_t) -1;
+
+  buffer->tms_utime = filetime2clock (user_time);
+  buffer->tms_stime = filetime2clock (kernel_time);
+  buffer->tms_cutime = 0;
+  buffer->tms_cstime = 0;
+
+  return filetime2clock (creation_time);
+}
index 9fb3a90..f8a9737 100644 (file)
@@ -27,7 +27,7 @@
 
 
 # Specification in the form of a command-line invocation:
-#   gnulib-tool --import --dir=. --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil clock-time close connect dirfd duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen localcharset locale log1p maintainer-makefile malloc-gnu malloca nl_langinfo nproc open pipe2 putenv recv recvfrom regex rename select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat trunc verify vsnprintf warnings wchar
+#   gnulib-tool --import --dir=. --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --lgpl=3 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap canonicalize-lgpl ceil clock-time close connect dirfd duplocale environ extensions flock floor fpieee frexp full-read full-write func gendocs getaddrinfo getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring listen localcharset locale log1p maintainer-makefile malloc-gnu malloca nl_langinfo nproc open pipe2 putenv recv recvfrom regex rename select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat times trunc verify vsnprintf warnings wchar
 
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
 gl_LOCAL_DIR([gnulib-local])
@@ -103,6 +103,7 @@ gl_MODULES([
   striconveh
   string
   sys_stat
+  times
   trunc
   verify
   vsnprintf
index ef150cb..8dc2691 100644 (file)
@@ -197,10 +197,12 @@ AC_DEFUN([gl_EARLY],
   # Code from module sys_socket:
   # Code from module sys_stat:
   # Code from module sys_time:
+  # Code from module sys_times:
   # Code from module sys_types:
   # Code from module sys_uio:
   # Code from module time:
   # Code from module time_r:
+  # Code from module times:
   # Code from module trunc:
   # Code from module unistd:
   # Code from module unistr/base:
@@ -685,6 +687,8 @@ AC_SUBST([LTALLOCA])
   AC_PROG_MKDIR_P
   gl_HEADER_SYS_TIME_H
   AC_PROG_MKDIR_P
+  gl_SYS_TIMES_H
+  AC_PROG_MKDIR_P
   gl_SYS_TYPES_H
   AC_PROG_MKDIR_P
   gl_HEADER_SYS_UIO
@@ -696,6 +700,11 @@ AC_SUBST([LTALLOCA])
     gl_PREREQ_TIME_R
   fi
   gl_TIME_MODULE_INDICATOR([time_r])
+  gl_FUNC_TIMES
+  if test $HAVE_TIMES = 0; then
+    AC_LIBOBJ([times])
+  fi
+  gl_SYS_TIMES_MODULE_INDICATOR([times])
   gl_FUNC_TRUNC
   if test $HAVE_DECL_TRUNC = 0 || test $REPLACE_TRUNC = 1; then
     AC_LIBOBJ([trunc])
@@ -1046,10 +1055,12 @@ AC_DEFUN([gl_FILE_LIST], [
   lib/sys_socket.in.h
   lib/sys_stat.in.h
   lib/sys_time.in.h
+  lib/sys_times.in.h
   lib/sys_types.in.h
   lib/sys_uio.in.h
   lib/time.in.h
   lib/time_r.c
+  lib/times.c
   lib/trunc.c
   lib/unistd.c
   lib/unistd.in.h
@@ -1211,10 +1222,12 @@ AC_DEFUN([gl_FILE_LIST], [
   m4/sys_socket_h.m4
   m4/sys_stat_h.m4
   m4/sys_time_h.m4
+  m4/sys_times_h.m4
   m4/sys_types_h.m4
   m4/sys_uio_h.m4
   m4/time_h.m4
   m4/time_r.m4
+  m4/times.m4
   m4/tm_gmtoff.m4
   m4/trunc.m4
   m4/unistd_h.m4
diff --git a/m4/sys_times_h.m4 b/m4/sys_times_h.m4
new file mode 100644 (file)
index 0000000..6006985
--- /dev/null
@@ -0,0 +1,51 @@
+# Configure a replacement for <sys/times.h>.
+# serial 8
+
+# Copyright (C) 2008-2013 Free Software Foundation, Inc.
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# Written by Simon Josefsson.
+
+AC_DEFUN([gl_SYS_TIMES_H],
+[
+  AC_REQUIRE([gl_SYS_TIMES_H_DEFAULTS])
+
+  dnl <sys/times.h> is always overridden, because of GNULIB_POSIXCHECK.
+  gl_CHECK_NEXT_HEADERS([sys/times.h])
+  if test $ac_cv_header_sys_times_h = yes; then
+    HAVE_SYS_TIMES_H=1
+    AC_CHECK_TYPES([struct tms], [], [HAVE_STRUCT_TMS=0], [[
+#include <sys/times.h>
+      ]])
+  else
+    HAVE_SYS_TIMES_H=0
+    HAVE_STRUCT_TMS=0
+  fi
+  AC_SUBST([HAVE_SYS_TIMES_H])
+
+  dnl Check for declarations of anything we want to poison if the
+  dnl corresponding gnulib module is not in use.
+  gl_WARN_ON_USE_PREPARE([[
+/* Some systems have incomplete headers.  */
+#if !(defined __GLIBC__ && !defined __UCLIBC__)
+# include <time.h>
+#endif
+#include <sys/times.h>
+    ]], [times])
+])
+
+AC_DEFUN([gl_SYS_TIMES_MODULE_INDICATOR],
+[
+  dnl Use AC_REQUIRE here, so that the default settings are expanded once only.
+  AC_REQUIRE([gl_SYS_TIMES_H_DEFAULTS])
+  gl_MODULE_INDICATOR_SET_VARIABLE([$1])
+])
+
+AC_DEFUN([gl_SYS_TIMES_H_DEFAULTS],
+[
+  GNULIB_TIMES=0;     AC_SUBST([GNULIB_TIMES])
+  HAVE_STRUCT_TMS=1;  AC_SUBST([HAVE_STRUCT_TMS])
+  HAVE_TIMES=1;       AC_SUBST([HAVE_TIMES])
+])
diff --git a/m4/times.m4 b/m4/times.m4
new file mode 100644 (file)
index 0000000..a0c8667
--- /dev/null
@@ -0,0 +1,14 @@
+# times.m4 serial 2
+dnl Copyright (C) 2009-2013 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_FUNC_TIMES],
+[
+  AC_REQUIRE([gl_SYS_TIMES_H_DEFAULTS])
+  AC_CHECK_FUNCS_ONCE([times])
+  if test $ac_cv_func_times = no; then
+    HAVE_TIMES=0
+  fi
+])