FLTK logo

[master] e4a972c - Windows: fix fl_system() and fl_execvp() (STR 3438)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] e4a972c - Windows: fix fl_system() and fl_execvp() (STR 3438) "Albrecht Schlosser" May 26, 2021  
 
commit e4a972c3f3ab21549ad494a20517e1ec7fecce16
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Wed May 26 21:52:58 2021 +0200
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Wed May 26 21:52:58 2021 +0200

    Windows: fix fl_system() and fl_execvp() (STR 3438)
    
    Use correct conversions to Windows "wide character" encoding and
    use the appropriate wide character functions.

 src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx | 44 +++++++-------------------
 1 file changed, 12 insertions(+), 32 deletions(-)

diff --git src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
index 7e295fb..97a31cd 100644
--- src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
+++ src/drivers/WinAPI/Fl_WinAPI_System_Driver.cxx
@@ -68,7 +68,7 @@ extern "C" {
 }
 
 /*
-  Convert a UTF-8 string to Windows wide character encoding (UTF-16).
+  Convert UTF-8 string to Windows wide character encoding (UTF-16).
 
   This helper function is used throughout this file to convert UTF-8
   strings to Windows specific UTF-16 encoding for filenames, paths, or
@@ -86,15 +86,14 @@ extern "C" {
   'wbuf' is NULL a new buffer is allocated with realloc(). Hence the pointer
   'wbuf' can be shared among multiple calls to this function if it has been
   initialized with NULL (or malloc or realloc) before the first call.
-  Ideally every call to this function has its own static pointer though.
 
   The return value is either the old value of 'wbuf' (if the string fits)
-  or a pointer at the (re)allocated buffer.
+  or a pointer to the (re)allocated buffer.
 
   Pseudo doxygen docs (static function intentionally not documented):
 
   param[in]     utf8    input string (UTF-8)
-  param[in,out] wbuf    in:  pointer to output string buffer
+  param[in,out] wbuf    in:  pointer to output string buffer or NULL
                         out: new string (the pointer may be changed)
   param[in]     lg      optional: input string length (default = -1)
 
@@ -227,43 +226,24 @@ FILE *Fl_WinAPI_System_Driver::fopen(const char *fnam, const char *mode) {
 }
 
 int Fl_WinAPI_System_Driver::system(const char *cmd) {
-# ifdef __MINGW32__
-  return ::system(fl_utf2mbcs(cmd));
-# else
   return _wsystem(utf8_to_wchar(cmd, wbuf));
-# endif
 }
 
 int Fl_WinAPI_System_Driver::execvp(const char *file, char *const *argv) {
-# ifdef __MINGW32__
-  return _execvp(fl_utf2mbcs(file), argv);
-# else
-  wchar_t **ar;
+  int n = 0;
+  while (argv[n]) n++; // count args
+  wchar_t **ar = (wchar_t **)calloc(sizeof(wchar_t *), n + 1);
+  // convert arguments first; trailing NULL provided by calloc()
+  for (int i = 0; i < n; i++)
+    ar[i] = utf8_to_wchar(argv[i], ar[i]); // alloc and assign
+  // convert executable file and execute it ...
   utf8_to_wchar(file, wbuf);
-
-  int i = 0, n = 0;
-  while (argv[i]) {i++; n++;}
-  ar = (wchar_t **)malloc(sizeof(wchar_t *) * (n + 1));
-  i = 0;
-  while (i <= n) {
-    unsigned wn;
-    unsigned len = (unsigned)strlen(argv[i]);
-    wn = fl_utf8toUtf16(argv[i], len, NULL, 0) + 1; // Query length
-    ar[i] = (wchar_t *)malloc(sizeof(wchar_t) * wn);
-    wn = fl_utf8toUtf16(argv[i], len, (unsigned short *)ar[i], wn); // Convert string
-    ar[i][wn] = 0;
-    i++;
-  }
-  ar[n] = NULL;
   _wexecvp(wbuf, ar);   // STR #3040
-  i = 0;
-  while (i < n) {
+  // clean up (reached only if _wexecvp() failed)
+  for (int i = 0; i < n; i++)
     free(ar[i]);
-    i++;
-  }
   free(ar);
   return -1;            // STR #3040
-#endif
 }
 
 int Fl_WinAPI_System_Driver::chmod(const char *fnam, int mode) {
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.