From: Nick Rozinsky <[email protected]> Date: Mon, 7 Sep 2026 20:58:16 +0300 Subject: [PATCH] cobol: C posix-gettime binding to COBOL
Signed-off-by: Nick Rozinsky <[email protected]> libgcobol/ChangeLog: * Makefile.am: Add gettime.cc and posix-clock-gettime.{cbl,cpy} * Makefile.in: Likewise. * posix/t/Makefile: Add gettime smoke test. * posix/cpy/posix-clock-gettime.cpy: New file. * posix/shim/gettime.cc: New file. * posix/shim/timespec.h: New file. * posix/t/gettime.cbl: New file. * posix/udf/posix-clock-gettime.cbl: New file. --- libgcobol/Makefile.am | 5 +- libgcobol/Makefile.in | 23 +++++--- libgcobol/posix/cpy/posix-clock-gettime.cpy | 17 ++++++ libgcobol/posix/shim/gettime.cc | 55 +++++++++++++++++++ libgcobol/posix/shim/timespec.h | 10 ++++ libgcobol/posix/t/Makefile | 10 ++-- libgcobol/posix/t/gettime.cbl | 60 +++++++++++++++++++++ libgcobol/posix/udf/posix-clock-gettime.cbl | 60 +++++++++++++++++++++ 8 files changed, 229 insertions(+), 11 deletions(-) create mode 100644 libgcobol/posix/cpy/posix-clock-gettime.cpy create mode 100644 libgcobol/posix/shim/gettime.cc create mode 100644 libgcobol/posix/shim/timespec.h create mode 100644 libgcobol/posix/t/gettime.cbl create mode 100644 libgcobol/posix/udf/posix-clock-gettime.cbl diff --git a/libgcobol/Makefile.am b/libgcobol/Makefile.am index 86921dd12ae..8cbddee682d 100644 --- a/libgcobol/Makefile.am +++ b/libgcobol/Makefile.am @@ -66,6 +66,7 @@ libgcobol_la_SOURCES = \ posix/shim/lseek.cc \ posix/shim/open.cc \ posix/shim/stat.cc \ + posix/shim/gettime.cc \ stringbin.cc \ unstring.cc \ valconv.cc \ @@ -99,7 +100,8 @@ libgcobol_posix_la_SOURCES = \ posix/udf/posix-read.cbl \ posix/udf/posix-stat.cbl \ posix/udf/posix-unlink.cbl \ - posix/udf/posix-write.cbl + posix/udf/posix-write.cbl \ + posix/udf/posix-clock-gettime.cbl # Install the COBOL for the POSIX and compatibility libraries. nobase_libsubinclude_HEADERS = \ @@ -113,6 +115,7 @@ nobase_libsubinclude_HEADERS = \ posix/cpy/posix-open.cpy \ posix/cpy/posix-read.cpy \ posix/cpy/posix-stat.cpy \ + posix/cpy/posix-clock-gettime.cpy \ posix/cpy/psx-lseek.cpy \ posix/cpy/psx-open.cpy \ posix/cpy/statbuf.cpy \ diff --git a/libgcobol/Makefile.in b/libgcobol/Makefile.in index 42f91137ee8..72b08363c78 100644 --- a/libgcobol/Makefile.in +++ b/libgcobol/Makefile.in @@ -187,8 +187,9 @@ am__dirstamp = $(am__leading_dot)dirstamp @BUILD_LIBGCOBOL_TRUE@ posix/shim/fstat.lo \ @BUILD_LIBGCOBOL_TRUE@ posix/shim/localtime.lo \ @BUILD_LIBGCOBOL_TRUE@ posix/shim/lseek.lo posix/shim/open.lo \ -@BUILD_LIBGCOBOL_TRUE@ posix/shim/stat.lo stringbin.lo \ -@BUILD_LIBGCOBOL_TRUE@ unstring.lo valconv.lo xmlparse.lo +@BUILD_LIBGCOBOL_TRUE@ posix/shim/stat.lo posix/shim/gettime.lo \ +@BUILD_LIBGCOBOL_TRUE@ stringbin.lo unstring.lo valconv.lo \ +@BUILD_LIBGCOBOL_TRUE@ xmlparse.lo libgcobol_la_OBJECTS = $(am_libgcobol_la_OBJECTS) @BUILD_LIBGCOBOL_TRUE@am_libgcobol_la_rpath = -rpath $(toolexeclibdir) libgcobol_compat_gnu_la_LIBADD = @@ -224,7 +225,8 @@ libgcobol_posix_la_LIBADD = @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-read.lo \ @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-stat.lo \ @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-unlink.lo \ -@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-write.lo +@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-write.lo \ +@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-clock-gettime.lo libgcobol_posix_la_OBJECTS = $(am_libgcobol_posix_la_OBJECTS) @BUILD_LIBGCOBOL_TRUE@am_libgcobol_posix_la_rpath = -rpath \ @BUILD_LIBGCOBOL_TRUE@ $(toolexeclibdir) @@ -279,8 +281,9 @@ am__nobase_libsubinclude_HEADERS_DIST = posix/cpy/posix-errno.cpy \ posix/cpy/posix-ftruncate.cpy posix/cpy/posix-localtime.cpy \ posix/cpy/posix-lseek.cpy posix/cpy/posix-mkdir.cpy \ posix/cpy/posix-open.cpy posix/cpy/posix-read.cpy \ - posix/cpy/posix-stat.cpy posix/cpy/psx-lseek.cpy \ - posix/cpy/psx-open.cpy posix/cpy/statbuf.cpy posix/cpy/tm.cpy \ + posix/cpy/posix-stat.cpy posix/cpy/posix-clock-gettime.cpy \ + posix/cpy/psx-lseek.cpy posix/cpy/psx-open.cpy \ + posix/cpy/statbuf.cpy posix/cpy/tm.cpy \ compat/gnu/cpy/stored-char-length.cpy \ compat/gnu/cpy/cblproto.cpy compat/gnu/cpy/cbltypes.cpy \ compat/gnu/lib/CBL_ALLOC_MEM.cbl \ @@ -492,6 +495,7 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@ posix/shim/lseek.cc \ @BUILD_LIBGCOBOL_TRUE@ posix/shim/open.cc \ @BUILD_LIBGCOBOL_TRUE@ posix/shim/stat.cc \ +@BUILD_LIBGCOBOL_TRUE@ posix/shim/gettime.cc \ @BUILD_LIBGCOBOL_TRUE@ stringbin.cc \ @BUILD_LIBGCOBOL_TRUE@ unstring.cc \ @BUILD_LIBGCOBOL_TRUE@ valconv.cc \ @@ -525,7 +529,8 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-read.cbl \ @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-stat.cbl \ @BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-unlink.cbl \ -@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-write.cbl +@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-write.cbl \ +@BUILD_LIBGCOBOL_TRUE@ posix/udf/posix-clock-gettime.cbl # Install the COBOL for the POSIX and compatibility libraries. @@ -540,6 +545,7 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@ posix/cpy/posix-open.cpy \ @BUILD_LIBGCOBOL_TRUE@ posix/cpy/posix-read.cpy \ @BUILD_LIBGCOBOL_TRUE@ posix/cpy/posix-stat.cpy \ +@BUILD_LIBGCOBOL_TRUE@ posix/cpy/posix-clock-gettime.cpy \ @BUILD_LIBGCOBOL_TRUE@ posix/cpy/psx-lseek.cpy \ @BUILD_LIBGCOBOL_TRUE@ posix/cpy/psx-open.cpy \ @BUILD_LIBGCOBOL_TRUE@ posix/cpy/statbuf.cpy \ @@ -736,6 +742,8 @@ posix/shim/open.lo: posix/shim/$(am__dirstamp) \ posix/shim/$(DEPDIR)/$(am__dirstamp) posix/shim/stat.lo: posix/shim/$(am__dirstamp) \ posix/shim/$(DEPDIR)/$(am__dirstamp) +posix/shim/gettime.lo: posix/shim/$(am__dirstamp) \ + posix/shim/$(DEPDIR)/$(am__dirstamp) libgcobol.la: $(libgcobol_la_OBJECTS) $(libgcobol_la_DEPENDENCIES) $(EXTRA_libgcobol_la_DEPENDENCIES) $(AM_V_GEN)$(libgcobol_la_LINK) $(am_libgcobol_la_rpath) $(libgcobol_la_OBJECTS) $(libgcobol_la_LIBADD) $(LIBS) @@ -808,6 +816,8 @@ posix/udf/posix-unlink.lo: posix/udf/$(am__dirstamp) \ posix/udf/$(DEPDIR)/$(am__dirstamp) posix/udf/posix-write.lo: posix/udf/$(am__dirstamp) \ posix/udf/$(DEPDIR)/$(am__dirstamp) +posix/udf/posix-clock-gettime.lo: posix/udf/$(am__dirstamp) \ + posix/udf/$(DEPDIR)/$(am__dirstamp) libgcobol_posix.la: $(libgcobol_posix_la_OBJECTS) $(libgcobol_posix_la_DEPENDENCIES) $(EXTRA_libgcobol_posix_la_DEPENDENCIES) $(AM_V_GEN)$(libgcobol_posix_la_LINK) $(am_libgcobol_posix_la_rpath) $(libgcobol_posix_la_OBJECTS) $(libgcobol_posix_la_LIBADD) $(LIBS) @@ -839,6 +849,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@compat/gnu/lib/$(DEPDIR)/cbl_gpi.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/errno.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/fstat.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/gettime.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/localtime.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/lseek.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@posix/shim/$(DEPDIR)/open.Plo@am__quote@ diff --git a/libgcobol/posix/cpy/posix-clock-gettime.cpy b/libgcobol/posix/cpy/posix-clock-gettime.cpy new file mode 100644 index 00000000000..ae310fda981 --- /dev/null +++ b/libgcobol/posix/cpy/posix-clock-gettime.cpy @@ -0,0 +1,17 @@ + >>PUSH SOURCE FORMAT + >>SOURCE FIXED + identification division. + function-id. posix-clock-gettime prototype. + data division. + linkage section. + 77 return-value binary-long signed. + 01 lk-timespec. + 05 tv_sec usage is binary-double unsigned. + 05 tv_nsec usage is binary-double unsigned. + 01 lk-clockid_t usage is binary-double. + procedure division using + by value lk-clockid_t + by reference lk-timespec + returning return-value. + end function posix-clock-gettime. + >>POP SOURCE FORMAT diff --git a/libgcobol/posix/shim/gettime.cc b/libgcobol/posix/shim/gettime.cc new file mode 100644 index 00000000000..77a729d0d7b --- /dev/null +++ b/libgcobol/posix/shim/gettime.cc @@ -0,0 +1,55 @@ +#include <assert.h> +#include <stddef.h> +#include <stdio.h> +#include <unistd.h> +#include <time.h> + +#include <sys/types.h> + + +#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) + +extern "C" { + +#include "timespec.h" + +#define offset_assert(name, offset) do { \ + if( offsetof(posix_timespec_t, name) != offset ) { \ + fprintf(stderr, "C posix_timespec_t offset for %s %zu != COBOL offset %d\n", \ + #name, offsetof(posix_timespec_t, name), offset); \ + assert(offsetof(posix_timespec_t, name) == offset); \ + } \ + } while(false); + +int +posix_clock_gettime(int64_t clk_id, posix_timespec_t *tp, size_t size) { + struct timespec sb; + int erc = clock_gettime(clk_id, &sb); + + if( sizeof(timespec) != size ) { + fprintf(stderr, "%s:%d: %lu != received size %lu\n", __func__, __LINE__, + (unsigned long)sizeof(struct timespec), + (unsigned long)size); + fflush(stdout); + fflush(stderr); + } + if( tp == nullptr ) { + fprintf(stderr, "%s:%d: received NULL statbuf\n", __func__, __LINE__); + fflush(stdout); + fflush(stderr); + } + + if( true ) { // Verify last known reported COBOL offsets agree with C offsets. + offset_assert( tv_sec, 0 ); + offset_assert( tv_nsec , 8 ); + } + + assert(tp); + + tp->tv_sec = sb.tv_sec; + tp->tv_nsec = sb.tv_nsec; + + return erc; +} + +} // extern "C" diff --git a/libgcobol/posix/shim/timespec.h b/libgcobol/posix/shim/timespec.h new file mode 100644 index 00000000000..021fab92b89 --- /dev/null +++ b/libgcobol/posix/shim/timespec.h @@ -0,0 +1,10 @@ +#include <cstdint> + +namespace cbl { + typedef int64_t time_t; +}; + +struct posix_timespec_t { + cbl::time_t tv_sec; /* ID of device containing file */ + uint64_t tv_nsec; /* Inode number */ +}; \ No newline at end of file diff --git a/libgcobol/posix/t/Makefile b/libgcobol/posix/t/Makefile index 2da74a7aee8..e36537d32f5 100644 --- a/libgcobol/posix/t/Makefile +++ b/libgcobol/posix/t/Makefile @@ -9,7 +9,7 @@ COBC = gcobol LDFLAGS = -L $$(pwd) -Wl,-rpath -Wl,$$(pwd) -TESTS = errno exit localtime stat +TESTS = errno exit localtime stat gettime # Default target builds the tests all: $(TESTS) @@ -18,13 +18,15 @@ all: $(TESTS) $(COBC) -o $@ $(COBCFLAGS) -I. -I../cpy -I../udf $(LDFLAGS) $< -exit: ../udf/posix-exit.cbl +exit: ../udf/posix-exit.cbl errno: ../udf/posix-mkdir.cbl -stat: ../udf/posix-stat.cbl +stat: ../udf/posix-stat.cbl -localtime: ../udf/posix-stat.cbl +gettime: ../udf/posix-gettime.cbl + +localtime: ../udf/posix-stat.cbl # Run the tests test: $(TESTS) diff --git a/libgcobol/posix/t/gettime.cbl b/libgcobol/posix/t/gettime.cbl new file mode 100644 index 00000000000..efb4280fa9e --- /dev/null +++ b/libgcobol/posix/t/gettime.cbl @@ -0,0 +1,60 @@ + >> PUSH source format + >>SOURCE format is fixed + + * Copyright (c) 2021-2026 Symas Corporation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of the Symas Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + COPY posix-clock-gettime. + COPY posix-errno. + + Identification Division. + Program-ID. test-gettime. + Data Division. + Working-Storage Section. + 77 ClockType Binary-Long Value 0. + 01 Lk-timespec. + 05 tv_sec Usage is Binary-Double Unsigned. + 05 tv_nsec Usage is Binary-Double Unsigned. + 77 Exit-Status Binary-Long Value 1. + 77 error-msg PIC X(100). + + Procedure Division. + Display 'calling posix-clock-gettime ...' + + MOVE FUNCTION posix-clock-gettime(ClockType, Lk-timespec) + TO Exit-Status. + If Exit-Status < 0 then + Display 'errno ', Function posix-errno(error-msg), + ': ' error-msg + Goback + End-If. + Display tv_sec IN Lk-timespec. + Display tv_nsec IN Lk-timespec. + Display 'finished successfully ...'. + End Program test-gettime. + >> POP SOURCE FORMAT diff --git a/libgcobol/posix/udf/posix-clock-gettime.cbl b/libgcobol/posix/udf/posix-clock-gettime.cbl new file mode 100644 index 00000000000..8f64d280aac --- /dev/null +++ b/libgcobol/posix/udf/posix-clock-gettime.cbl @@ -0,0 +1,60 @@ + >>PUSH SOURCE FORMAT + >>SOURCE FIXED + * Copyright (c) 2021-2026 Symas Corporation + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of the Symas Corporation nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + COPY "posix-clock-gettime.cpy". + * int clock_gettime(clockid_t clk_id, const struct timespec *tp); + Identification Division. + Function-ID. posix-clock-gettime. + Data Division. + Working-Storage Section. + 77 bufsize Usage Binary-Long. + Linkage Section. + 77 Return-Value Binary-Long. + 01 Lk-clockid-t Usage is Binary-Double. + 01 Lk-timespec. + 05 tv_sec Usage is Binary-Double Unsigned. + 05 tv_nsec Usage is Binary-Double Unsigned. + + Procedure Division using + By Value Lk-clockid-t + By Reference Lk-timespec, + Returning Return-Value. + + Move Function Byte-Length(Lk-timespec) to bufsize. + + Call "posix_clock_gettime" using + By Value Lk-clockid-t, + By Reference Lk-timespec, + By Value bufsize, + Returning Return-Value. + + Goback. + End Function posix-clock-gettime. + >> POP SOURCE FORMAT -- 2.34.1
