https://gcc.gnu.org/g:260e0a389a70994d2728b389e6865528557e3933

commit r15-8250-g260e0a389a70994d2728b389e6865528557e3933
Author: Jose E. Marchesi <jose.march...@oracle.com>
Date:   Mon Mar 17 16:51:31 2025 +0100

    cobol: use ldirname in cdf-copy.cc
    
    This patch changes gcc/cobol/cdf-copy.cc to use the new ldirname from
    libibertay rather than the host's dirname.  This removes an include
    for libgen.h.
    
    Regtested in x86_64-linux-gnu by running make check-cobol.
    
    gcc/cobol/ChangeLog
    
            * cdf-copy.cc (copybook_elem_t::open_file): Use ldirname rather
            than dirname.

Diff:
---
 gcc/cobol/cdf-copy.cc | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/gcc/cobol/cdf-copy.cc b/gcc/cobol/cdf-copy.cc
index dfa3f57315db..179dbacea930 100644
--- a/gcc/cobol/cdf-copy.cc
+++ b/gcc/cobol/cdf-copy.cc
@@ -40,7 +40,6 @@
 #include "copybook.h"
 
 #include <glob.h>
-#include <libgen.h>
 
 #define COUNT_OF(X) (sizeof(X) / sizeof(X[0]))
 
@@ -268,24 +267,33 @@ int
 copybook_elem_t::open_file( const char directory[], bool literally ) {
   int erc;
   char  *pattern, *copier = xstrdup(cobol_filename());
-  if( ! directory ) {
-    directory = dirname(copier);
-    if( 0 == strcmp(".", directory) ) directory = NULL;
+  char *dname = NULL;
+
+  if ( directory ) {
+    dname = xstrdup(directory);
+  } else {
+    dname = ldirname(copier);
+    gcc_assert (dname != NULL); /* out of memory  */
+    if( '\0' == dname[0] ) {
+      free (dname);
+      dname = NULL;
+    }
   }
 
   char *path = NULL;
 
-  if( directory || library.name ) {
-    if( directory && library.name ) {
-      path = xasprintf( "%s/%s/%s", directory, library.name, source.name );
+  if( dname || library.name ) {
+    if( dname && library.name ) {
+      path = xasprintf( "%s/%s/%s", dname, library.name, source.name );
     } else {
-      const char *dir = directory? directory : library.name;
+      const char *dir = dname? dname : library.name;
       path = xasprintf( "%s/%s", dir, source.name );
     }
   } else {
     path = xasprintf( "%s", source.name );
   }
 
+  free(dname);
   gcc_assert(path);
 
   if( literally ) {

Reply via email to