From: Nick Rozinsky <[email protected]>

Signed-off-by: Nick Rozinsky <[email protected]>
---
 libgcobol/Makefile.am                       |  6 +-
 libgcobol/posix/cpy/posix-clock-gettime.cpy | 17 ++++++
 libgcobol/posix/shim/gettime.cc             | 55 ++++++++++++++++++
 libgcobol/posix/shim/timespec.h             | 10 ++++
 libgcobol/posix/t/Makefile                  |  4 +-
 libgcobol/posix/t/gettime.cbl               | 62 +++++++++++++++++++++
 libgcobol/posix/udf/posix-clock-gettime.cbl | 60 ++++++++++++++++++++
 7 files changed, 212 insertions(+), 2 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 18f04bad0d3..400667e1886 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              \
@@ -97,7 +98,9 @@ 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 =                 \
@@ -111,6 +114,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/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..f370fe79dbd
--- /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..4d3f6a99984 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)
@@ -24,6 +24,8 @@ errno: ../udf/posix-mkdir.cbl
 
 stat: ../udf/posix-stat.cbl 
 
+gettime: ../udf/posix-gettime.cbl 
+
 localtime: ../udf/posix-stat.cbl 
 
 # Run the tests
diff --git a/libgcobol/posix/t/gettime.cbl b/libgcobol/posix/t/gettime.cbl
new file mode 100644
index 00000000000..a34e6624fb8
--- /dev/null
+++ b/libgcobol/posix/t/gettime.cbl
@@ -0,0 +1,62 @@
+        >> 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.
+        Display tv_sec IN Lk-timespec
+        Display tv_nsec IN Lk-timespec
+        Display 'finished successfully ...'
+        Goback.
+        >> POP SOURCE FORMAT
\ No newline at end of file
diff --git a/libgcobol/posix/udf/posix-clock-gettime.cbl 
b/libgcobol/posix/udf/posix-clock-gettime.cbl
new file mode 100644
index 00000000000..1a8d446c66b
--- /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.43.0

Reply via email to