2022-10-03  Juan Manuel Guerrero <juan.guerrero@gmx.de>


	* check/check-cflags: DJGPP specif fix.

	* check/check-define-variable: DJGPP specif fix.

	* check/check-includedir: DJGPP specif fix.

	* check/check-libs: DJGPP specif fix.

	* check/check-libs-private: DJGPP specif fix.

	* check/check-requires-private: DJGPP specif fix.

	* glib-1.2.10/glib.h [__DJGPP__]: For DJGPP replace the hard coded values for
	G_SEARCHPATH_SEPARATOR and G_SEARCHPATH_SEPARATOR_S by the appropriate values
	taken from string dos_path_separator.

	* main.c [G_OS_DJGPP]: For DJGPP add an constructor that will call canonicalize_pkg_paths()
	and sets restore_changed_pkg_paths for atexit().

	* parse.c [G_OS_DJGPP]: For DJGPP replace the call to getc_unlocked, flockfile and
	funlockfile by macros.  These MT file locking functions are not provided by DJGPP.

	* pkg.c [G_OS_DJGPP]: For DJGPP define dos_path_separator, canonicalize_pkg_paths()
	and restore_changed_pkg_paths().

	* pkg.h [G_OS_DJGPP]: For DJGPP provide canonicalize_pkg_paths() and
	restore_changed_pkg_paths() prototypes.

	* popt.c [G_OS_DJGPP]: For DJGPP replace the call to setreuid by a macro.  It does
	nothing than returning without error.






diff -aprNU5 pkg-config-0.23.orig/check/check-cflags pkg-config-0.23/check/check-cflags
--- pkg-config-0.23.orig/check/check-cflags	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/check/check-cflags	2022-09-29 23:47:00 +0000
@@ -1,9 +1,9 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@" 
 fi
 
 set -e
 
diff -aprNU5 pkg-config-0.23.orig/check/check-define-variable pkg-config-0.23/check/check-define-variable
--- pkg-config-0.23.orig/check/check-define-variable	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/check/check-define-variable	2022-09-29 23:47:00 +0000
@@ -1,9 +1,9 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@" 
 fi
 
 set -e
 
diff -aprNU5 pkg-config-0.23.orig/check/check-includedir pkg-config-0.23/check/check-includedir
--- pkg-config-0.23.orig/check/check-includedir	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/check/check-includedir	2022-09-29 23:47:00 +0000
@@ -1,9 +1,9 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@"
 fi
 
 set -e
 
diff -aprNU5 pkg-config-0.23.orig/check/check-libs pkg-config-0.23/check/check-libs
--- pkg-config-0.23.orig/check/check-libs	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/check/check-libs	2022-10-03 17:46:00 +0000
@@ -1,15 +1,17 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@" 
 fi
 
 set -e
 
 . ${srcdir}/common
 
+# This will fail for DJGPP port
+# because it uses --static by default.
 ARGS="--libs simple"
 RESULT="-lsimple"
 
 run_test
diff -aprNU5 pkg-config-0.23.orig/check/check-libs-private pkg-config-0.23/check/check-libs-private
--- pkg-config-0.23.orig/check/check-libs-private	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/check/check-libs-private	2022-09-29 23:47:00 +0000
@@ -1,9 +1,9 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@" 
 fi
 
 set -e
 
diff -aprNU5 pkg-config-0.23.orig/check/check-requires-private pkg-config-0.23/check/check-requires-private
--- pkg-config-0.23.orig/check/check-requires-private	2008-01-04 12:38:42 +0000
+++ pkg-config-0.23/check/check-requires-private	2022-09-29 23:47:00 +0000
@@ -1,9 +1,9 @@
 #! /bin/sh
 
 # Do the Solaris Dance:
-if [ ! -d ~root ]  ; then
+if [ ! -d ~root ] && [ ! -d /dev/env/DJDIR ]  ; then
     exec /usr/xpg4/bin/sh $0 "$@" 
 fi
 
 set -e
 
diff -aprNU5 pkg-config-0.23.orig/glib-1.2.10/glib.h pkg-config-0.23/glib-1.2.10/glib.h
--- pkg-config-0.23.orig/glib-1.2.10/glib.h	2001-02-14 17:37:22 +0000
+++ pkg-config-0.23/glib-1.2.10/glib.h	2022-10-02 16:32:42 +0000
@@ -87,17 +87,29 @@
 #define G_SEARCHPATH_SEPARATOR_S ";"
 
 #else  /* !NATIVE_WIN32 */
 
 #ifndef __EMX__
+#ifdef __DJGPP__
+/* DJGPP/MS-DOS/FreeDOS */
+
+extern char dos_path_separator[2];
+
+#define G_DIR_SEPARATOR '/'
+#define G_DIR_SEPARATOR_S "/"
+#define G_SEARCHPATH_SEPARATOR   (dos_path_separator[0])
+#define G_SEARCHPATH_SEPARATOR_S (dos_path_separator)
+
+#else
 /* Unix */
 
 #define G_DIR_SEPARATOR '/'
 #define G_DIR_SEPARATOR_S "/"
 #define G_SEARCHPATH_SEPARATOR ':'
 #define G_SEARCHPATH_SEPARATOR_S ":"
 
+#endif
 #else
 /* EMX/OS2 */
 
 #define G_DIR_SEPARATOR '/'
 #define G_DIR_SEPARATOR_S "/"
diff -aprNU5 pkg-config-0.23.orig/main.c pkg-config-0.23/main.c
--- pkg-config-0.23.orig/main.c	2008-01-04 11:59:32 +0000
+++ pkg-config-0.23/main.c	2022-10-03 00:42:00 +0000
@@ -168,10 +168,35 @@ pkg_uninstalled (Package *pkg)
     }
 
   return FALSE;
 }
 
+#ifdef G_OS_DJGPP
+/* This is called before `main' to cononicalize all paths
+   in the environment and to get the path separator.
+   For DJGPP the path separator is infered at run-time by inspection
+   of the PATH_SEPARATOR enviroinment variable or assuming a default.  */
+static void __attribute__((constructor))
+djgpp_pkg_config_startup (void)
+{
+  char *paths[NUMBER_OF_PKG_PATHS] = {
+    /*  Paths from environment.  */
+    "PKG_CONFIG_PATH",
+    "PKG_CONFIG_LIBDIR",
+    "PKG_CONFIG_SYSROOT_DIR",
+    "PKG_CONFIG_TOP_BUILD_DIR",
+    NULL,
+
+    /*  Hard coded paths at compile time.  */
+    PKG_CONFIG_PC_PATH,
+    NULL
+  };
+  canonicalize_pkg_paths (paths);
+  atexit (restore_changed_pkg_paths);
+}
+#endif
+
 int
 main (int argc, char **argv)
 {
   int want_my_version = 0;
   int want_version = 0;
diff -aprNU5 pkg-config-0.23.orig/parse.c pkg-config-0.23/parse.c
--- pkg-config-0.23.orig/parse.c	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/parse.c	2022-10-01 14:24:06 +0000
@@ -43,10 +43,26 @@ int msvc_syntax = FALSE;
 #ifndef G_IS_DIR_SEPARATOR
 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
 #endif
 #endif
 
+#ifdef G_OS_DJGPP
+/*  DJGPP does not provide neither of these.  */
+# undef  getc_unlocked
+# define getc_unlocked(filehandle)    \
+  (__gnuc_extension__                 \
+    ({                                \
+       int __c = getc((filehandle));  \
+       __c;                           \
+    })                                \
+  )
+# undef  flockfile
+# define flockfile(filehandle)    /* void */
+# undef  funlockfile
+# define funlockfile(filehandle)  /* void */
+#endif
+
 /**
  * Read an entire line from a file into a buffer. Lines may
  * be delimited with '\n', '\r', '\n\r', or '\r\n'. The delimiter
  * is not written into the buffer. Text after a '#' character is treated as
  * a comment and skipped. '\' can be used to escape a # character.
diff -aprNU5 pkg-config-0.23.orig/pkg.c pkg-config-0.23/pkg.c
--- pkg-config-0.23.orig/pkg.c	2008-01-04 11:52:32 +0000
+++ pkg-config-0.23/pkg.c	2022-10-03 01:11:42 +0000
@@ -55,10 +55,133 @@ static int scanned_dir_count = 0;
 
 gboolean disable_uninstalled = FALSE;
 gboolean ignore_requires = FALSE;
 gboolean ignore_private_libs = TRUE;
 
+#ifdef G_OS_DJGPP
+const char *orig_paths[NUMBER_OF_PKG_PATHS][2];
+char dos_path_separator[2];
+
+#define COLON_SEPARATOR       (0)
+#define SEMI_COLON_SEPARATOR  (1)
+#define GET_PATH_SEPARATOR(dos_path_separator)                          \
+ (__gnuc_extension__                                                    \
+   ({                                                                   \
+      int _type;                                                        \
+      char *_path_separator = getenv("PATH_SEPARATOR");                 \
+      if (!_path_separator)                                             \
+        _type = (SEMI_COLON_SEPARATOR);                                 \
+      else                                                              \
+      if (_path_separator[0] == ':' && _path_separator[1] == '\0')      \
+        _type = (COLON_SEPARATOR);                                      \
+      else                                                              \
+        _type = (SEMI_COLON_SEPARATOR);                                 \
+      (dos_path_separator)[0] = _type = (COLON_SEPARATOR) ? ':' : ';';  \
+      (dos_path_separator)[1] = '\0';                                   \
+   })                                                                   \
+ )
+
+void
+canonicalize_pkg_paths (char *paths[NUMBER_OF_PKG_PATHS])
+{
+#define PATH_LENGTH  1024
+  char real_path[PATH_LENGTH], **search_dirs, **iter;
+  int i;
+
+  GET_PATH_SEPARATOR (dos_path_separator);
+
+  /* Create full expanded paths with x: replaced by /dev/x
+     and backslashes replaced by slashes from environment variables.  */
+  for (i = 0; paths[i]; i++)
+    {
+      char *path = getenv (orig_paths[i][0] = paths[i]);
+      if (path)
+        {
+          char *real_dir = real_path;
+          int changed = 0;
+
+          search_dirs = g_strsplit (path, dos_path_separator, -1);
+          for (iter = search_dirs; *iter; iter++)
+            {
+              char *begin = realpath (*iter, real_dir);
+              if (begin)
+                {
+                  changed = 1;
+                  for (; *real_dir; real_dir++)
+                    ;
+                  if (PATH_LENGTH > (int)(real_dir - real_path))
+                    {
+                      *real_dir = dos_path_separator[0];
+                      if (begin[1] == ':')
+                        {
+#define OFFSET  sizeof("/dev")
+                          char drive = begin[0];
+                          char *src = real_dir, *dst = real_dir + OFFSET - 1;
+
+                          while (*src != ':')
+                            *dst-- = *src--;
+                          *dst-- = drive;
+                          *dst-- = '/';
+                          *dst-- = 'v';
+                          *dst-- = 'e';
+                          *dst-- = 'd';
+                          *dst-- = '/';
+                          real_dir += OFFSET;
+#undef OFFSET
+                        }
+                    }
+                }
+            }
+          if ((int)(real_dir - real_path) > 0)
+            *--real_dir = '\0';
+
+          if (changed)
+            {
+              orig_paths[i][1] = g_strdup (path);
+              setenv (paths[i], real_path, 1);
+            }
+          else
+            orig_paths[i][1] = NULL;
+
+          real_path[0] = '\0';
+
+          g_strfreev (search_dirs);
+        }
+    }
+
+  /* Create full expanded paths with x: replaced by /dev/x
+     and backslashes replaced by slashes from hard coded variables.  */
+  char path_sep = dos_path_separator[0] == ';' ? ':' : ';';
+  for (i++; paths[i]; i++)
+    {
+      int n;
+      for (n = 0; paths[i][n]; n++)
+        {
+          if (paths[i][n] == path_sep)
+            paths[i][n] = dos_path_separator[0];
+          else
+          if (paths[i][n] == '\\')
+            paths[i][n] = '/';
+       }
+    }
+}
+#undef GET_PATH_SEPARATOR
+#undef SEMI_COLON_SEPARATOR
+#undef COLON_SEPARATOR
+
+void
+restore_changed_pkg_paths (void)
+{
+  int i;
+  for (i = 0; i < NUMBER_OF_PKG_PATHS; i++)
+    {
+      if (orig_paths[i][1])
+        setenv (orig_paths[i][0], orig_paths[i][1], 1);
+    }
+}
+#endif /* G_OS_DJGPP */
+
 void
 add_search_dir (const char *path)
 {
   search_dirs = g_slist_append (search_dirs, g_strdup (path));
 }
diff -aprNU5 pkg-config-0.23.orig/pkg.h pkg-config-0.23/pkg.h
--- pkg-config-0.23.orig/pkg.h	2008-01-04 11:20:02 +0000
+++ pkg-config-0.23/pkg.h	2022-10-03 00:42:00 +0000
@@ -118,10 +118,16 @@ void verbose_error (const char *format,
 gboolean name_ends_in_uninstalled (const char *str);
 
 void enable_private_libs(void);
 void disable_private_libs(void);
 
+#ifdef G_OS_DJGPP
+#define NUMBER_OF_PKG_PATHS (5 + 2)
+void canonicalize_pkg_paths (char *paths[NUMBER_OF_PKG_PATHS]);
+void restore_changed_pkg_paths (void);
+#endif
+
 /* If TRUE, do not automatically prefer uninstalled versions */
 extern gboolean disable_uninstalled;
 
 extern char *pcsysrootdir;
 
diff -aprNU5 pkg-config-0.23.orig/popt.c pkg-config-0.23/popt.c
--- pkg-config-0.23.orig/popt.c	2008-01-04 10:35:32 +0000
+++ pkg-config-0.23/popt.c	2022-10-01 14:24:06 +0000
@@ -42,10 +42,23 @@
 
 #include "findme.h"
 #include "popt.h"
 #include "poptint.h"
 
+#ifdef G_OS_DJGPP
+/*  DJGPP does not provide this.  */
+# undef  setreuid
+# define setreuid(ruid, euid)  \
+  (__gnuc_extension__          \
+    ({                         \
+       int __c = 0;            \
+       errno = 0;              \
+       __c;                    \
+    })                         \
+  )
+#endif
+
 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) {
     if (con->execPath) free(con->execPath);
     con->execPath = strdup(path);
     con->execAbsolute = allowAbsolute;
 }
