diff options
author | Andy Wingo <wingo@pobox.com> | 2013-02-18 19:06:34 +0100 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2013-02-18 19:06:34 +0100 |
commit | 84ebfef4e69130288d68d68c9e872402aa91363d (patch) | |
tree | 10e62c841ade19431f22b15468acf3b3b2bc59ed /lib | |
parent | 3dac6181c18508a194f240d633c8c3f7304adb3f (diff) |
add gnulib `times' module
* lib: Add `times' module. Should fix socket.c on MinGW. Thanks to Eli
Zaretskii for the report.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 42 | ||||
-rw-r--r-- | lib/sys_times.in.h | 80 | ||||
-rw-r--r-- | lib/times.c | 66 |
3 files changed, 187 insertions, 1 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index c99a7550b..2fe0e8288 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 index 000000000..8ea8088c4 --- /dev/null +++ b/lib/sys_times.in.h @@ -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 index 000000000..cf2fa1df9 --- /dev/null +++ b/lib/times.c @@ -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); +} |