85c30488d844dfcd75cdaad66831e2fcd5896f9d
[feisty_meow.git] / nucleus / library / application / windoze_helper.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : windoze_helper                                                    *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1994-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "windoze_helper.h"
16 ///#include "array.h"
17 ///#include "build_configuration.h"
18 ///#include "convert_utf.h"
19 ///#include "function.h"
20 ///#include "mutex.h"
21 ///#include "portable.h"
22 ///#include "set.h"
23 ///#include "version_record.h"
24 #include <configuration/application_configuration.h>
25
26 ///#include <errno.h>
27 ///#include <stdlib.h>
28
29 /*
30 #ifdef __UNIX__
31   #include <limits.h>
32   #include <stdio.h>
33   #include <string.h>
34   #include <sys/poll.h>
35   #include <sys/times.h>
36   #include <sys/utsname.h>
37   #include <sys/wait.h>
38   #include <time.h>
39   #include <unistd.h>
40 #endif
41 #ifdef __XWINDOWS__
42 //hmmm: need code for the wait cursor stuff.
43 #endif
44 #ifdef __WIN32__
45   #include <mmsystem.h>
46   #include <process.h>
47   #include <shellapi.h>
48   #include <shlobj.h>
49 #endif
50 */
51
52 using namespace basis;
53 using namespace structures;
54 using namespace configuration;
55
56 #undef static_class_name
57 #define static_class_name() "windoze_helper"
58
59 /*
60 //#define DEBUG_PORTABLE
61   // uncomment for noisy debugging.
62
63 //#define DEBUG_UPTIME
64   // uncomment to get noisier reporting about system and rolling uptime.
65
66 //#define JUMP_TIME_49_DAYS
67   // uncomment to make our uptimes start just before the 32 bit uptime rollover.
68 //#define JUMP_TIME_497_DAYS
69   // uncomment to make our uptimes start just before the jiffies rollover.
70
71 //#define ENABLE_ROLLOVER_BUG
72   // uncomment to get old behavior back where the uptime was not rolling
73   // over properly.  this turns off our remainder calculation and leaves the
74   // conversion as a simple cast, which will fail and get stuck at 2^32-1.
75
76 #define SUPPORT_SHELL_EXECUTE
77   // if this is not commented out, then the ShellExecute version of launch_
78   // -process() is available.  when commented out, ShellExecute is turned off.
79   // disabling this support is the most common because the ShellExecute method
80   // in win32 was only supported for wk203 and wxp, that is only after
81   // windows2000 was already available.  since nt and w2k don't support this,
82   // we just usually don't mess with it.  it didn't answer a single one of our
83   // issues on windows vista (wfista) anyway, so it's not helping.
84
85 // ensure we always have debugging turned on if the jump is enabled.
86 #ifdef JUMP_TIME_49_DAYS
87   #undef DEBUG_UPTIME
88   #define DEBUG_UPTIME
89 #endif
90 #ifdef JUMP_TIME_497_DAYS
91   #undef DEBUG_UPTIME
92   #define DEBUG_UPTIME
93 #endif
94 // the JUMP..DAYS macros are mutually exclusive.  use none or one, not both.
95 #ifdef JUMP_TIME_497_DAYS
96   #ifdef JUMP_TIME_49_DAYS
97     #error One cannot use both 497 day and 49 day bug inducers
98   #endif
99 #endif
100 #ifdef JUMP_TIME_49_DAYS
101   #ifdef JUMP_TIME_497_DAYS
102     #error One cannot use both 49 day and 497 day bug inducers
103   #endif
104 #endif
105 */
106
107
108 namespace application {
109
110 /*
111 mutex BASIS_EXTERN &__uptime_synchronizer();
112   // used by our low-level uptime methods to protect singleton variables.
113 mutex BASIS_EXTERN &__process_synchronizer();
114   // used for synchronizing our records of child processes.
115 #ifdef __UNIX__
116 int_set BASIS_EXTERN &__our_kids();
117   // the static list of processes we've started.
118 #endif
119
120 const int MAXIMUM_COMMAND_LINE = 32 * KILOBYTE;
121   // maximum command line that we'll deal with here.
122
123 #ifdef __UNIX__
124   const char *UPTIME_REPORT_FILE = "/tmp/uptime_report.log";
125 #endif
126 #ifdef __WIN32__
127   const char *UPTIME_REPORT_FILE = "c:/uptime_report.log";
128 #endif
129
130 #undef LOG
131 #define LOG(s) STAMPED_EMERGENCY_LOG(program_wide_logger(), s);
132
133 #define COMPLAIN(to_print) { \
134   guards::write_to_console((isprintf("basis/portable::%s: ", func) + to_print).s()); \
135 }
136
137 static const double __rollover_point = 2.0 * double(MAXINT);
138   // this number is our rollover point for 32 bit integers.
139
140 double rolling_uptime()
141 {
142   auto_synchronizer l(__uptime_synchronizer());
143     // protect our rollover records.
144
145   static u_int __last_ticks = 0;
146   static int __rollovers = 0;
147
148   u_int ticks_up = system_uptime();
149     // acquire the current uptime as a 32 bit unsigned int.
150
151   if (ticks_up < __last_ticks) {
152     // rollover happened.  increment our tracker.
153     __rollovers++;
154   }
155   __last_ticks = ticks_up;
156
157   double to_return = double(__rollovers) * __rollover_point + double(ticks_up);
158
159 #ifdef DEBUG_UPTIME
160   #ifdef __WIN32__
161   static FILE *__outfile = fopen(UPTIME_REPORT_FILE, "a+b");
162   static double old_value = 0;
163   if (absolute_value(old_value - to_return) > 9.9999) {
164     // only report when the time changes by more than 10 ms.
165     fprintf(__outfile, "-> uptime=%.0f\n", to_return);
166     fflush(__outfile);
167     old_value = to_return;
168     if (__rollover_point - to_return <= 40.00001) {
169       fprintf(__outfile, "---> MAXIMUM UPTIME SOON!\n");
170       fflush(__outfile);
171     }
172   }
173   #endif
174 #endif
175
176   return to_return;
177 }
178
179 u_int system_uptime()
180 {
181 #ifdef __WIN32__
182   return timeGetTime();
183 #else
184   auto_synchronizer l(__uptime_synchronizer());
185
186   static clock_t __ctps = sysconf(_SC_CLK_TCK);  // clock ticks per second.
187   static const double __multiplier = 1000.0 / double(__ctps);
188     // the multiplier gives us our full range for the tick counter.
189
190 #ifdef DEBUG_UPTIME
191   static FILE *__outfile = fopen(UPTIME_REPORT_FILE, "wb");
192   if (__multiplier - u_int(__multiplier) > 0.000001) {
193     fprintf(__outfile, "uptime multiplier is "
194         "non-integral (%f)!\n", __multiplier);
195     fflush(__outfile);
196   }
197 #endif
198
199   // read uptime info from the OS.
200   tms uptime;
201   u_int real_ticks = times(&uptime);
202
203 #ifdef JUMP_TIME_497_DAYS
204   static u_int old_value_497 = 0;
205   bool report_497 = (absolute_value(real_ticks - old_value_497) > 99);
206   if (report_497) {
207     old_value_497 = real_ticks;  // update before changing it.
208     fprintf(__outfile, "pre kludge497 tix=%u\n", real_ticks);
209     fflush(__outfile);
210   }
211   real_ticks += u_int(49.0 * double(DAY_ms) + 17.0 * double(HOUR_ms));
212   if (report_497) {
213     fprintf(__outfile, "post kludge497 tix=%u\n", real_ticks);
214     fflush(__outfile);
215   }
216 #endif
217
218   // now turn this into the number of milliseconds.
219   double ticks_up = (double)real_ticks;
220   ticks_up = ticks_up * __multiplier;  // convert to time here.
221
222 #ifdef JUMP_TIME_497_DAYS
223   #ifndef ENABLE_ROLLOVER_BUG
224     // add in additional time so we don't have to wait forever.  we make the
225     // big number above rollover, but that's still got 27.5 or so minutes before
226     // we really rollover.  so we add that part of the fraction (lost in the
227     // haze of the multiplier) in here.  we don't add this in unless they are
228     // not exercising the rollover bug, because we already know that the 497
229     // day bug will show without the addition.  but when we're already fixing
230     // the uptime, we jump time a bit forward so we only have to wait a couple
231     // minutes instead of 27.
232     ticks_up += 25.0 * MINUTE_ms;
233   #endif
234 #endif
235
236 #ifdef JUMP_TIME_49_DAYS
237   static u_int old_value_49 = 0;
238   bool report_49 = (absolute_value(u_int(ticks_up) - old_value_49) > 999);
239   if (report_49) {
240     old_value_49 = u_int(ticks_up);  // update before changing it.
241     fprintf(__outfile, "pre kludge49 up=%f\n", ticks_up);
242     fflush(__outfile);
243   }
244   ticks_up += 49.0 * double(DAY_ms) + 17.0 * double(HOUR_ms);
245   if (report_49) {
246     fprintf(__outfile, "post kludge49 up=%f\n", ticks_up);
247     fflush(__outfile);
248   }
249 #endif
250
251 #ifndef ENABLE_ROLLOVER_BUG
252   // fix the return value if is has actually gone over the 2^32 limit for uint.
253   // casting a double larger than 2*MAXINT to a u_int on some platforms does
254   // not calculate a rolled-over value, but instead leaves the int at 2*MAXINT.
255   // thus we make sure it will be correct, as long as there are no more than
256   // 2^32-1 rollovers, which would be about 584,542 millenia.  it's unlikely
257   // earth will last that long, so this calculation seems safe.
258   u_int divided = u_int(ticks_up / __rollover_point);
259   double to_return = ticks_up - (double(divided) * __rollover_point);
260 #else
261   // we use the previous version of this calculation, which expected a u_int
262   // to double conversion to provide a modulo operation rather than just leaving
263   // the u_int at its maximum value (2^32-1).  however, that expectation is not
264   // guaranteed on some platforms (e.g., ARM processor with floating point
265   // emulation) and thus it becomes a bug around 49 days and 17 hours into
266   // OS uptime because the value gets stuck at 2^32-1 and never rolls over.
267   double to_return = ticks_up;
268 #endif
269
270 #ifdef DEBUG_UPTIME
271   static u_int old_value = 0;
272   int to_print = int(u_int(to_return));
273   if (absolute_value(int(old_value) - to_print) > 9) {
274     // only report when the time changes by more than 10 ms.
275     fprintf(__outfile, "-> uptime=%u\n", to_print);
276     fflush(__outfile);
277     old_value = u_int(to_print);
278     if (__rollover_point - to_return <= 40.00001) {
279       fprintf(__outfile, "---> MAXIMUM UPTIME SOON!\n");
280       fflush(__outfile);
281     }
282   }
283 #endif
284
285   return u_int(to_return);
286 #endif
287 }
288
289 void sleep_ms(u_int msec)
290 {
291 #ifdef __UNIX__
292   usleep(msec * 1000);
293 #endif
294 #ifdef __WIN32__
295   Sleep(msec);
296 #endif
297 }
298
299 istring null_device()
300 {
301 #ifdef __WIN32__
302   return "null:";
303 #else
304   return "/dev/null";
305 #endif
306 }
307
308 #ifdef __WIN32__
309 bool event_poll(MSG &message)
310 {
311   message.hwnd = 0;
312   message.message = 0;
313   message.wParam = 0;
314   message.lParam = 0;
315   if (!PeekMessage(&message, NIL, 0, 0, PM_REMOVE))
316     return false;
317   TranslateMessage(&message);
318   DispatchMessage(&message);
319   return true;
320 }
321 #endif
322
323 // makes a complaint about a failure.
324 #ifndef EMBEDDED_BUILD
325   #define COMPLAIN_QUERY(to_print) { \
326     unlink(tmpfile.s()); \
327     COMPLAIN(to_print); \
328   }
329 #else
330   #define COMPLAIN_QUERY(to_print) { \
331     COMPLAIN(to_print); \
332   }
333 #endif
334
335 #ifdef __UNIX__
336 istring get_cmdline_from_proc()
337 {
338   FUNCDEF("get_cmdline_from_proc");
339   isprintf cmds_filename("/proc/%d/cmdline", portable::process_id());
340   FILE *cmds_file = fopen(cmds_filename.s(), "r");
341   if (!cmds_file) {
342     COMPLAIN("failed to open our process's command line file.\n");
343     return "unknown";
344   }
345   size_t size = 2000;
346   char *buff = new char[size + 1];
347   ssize_t chars_read = getline(&buff, &size, cmds_file);
348     // read the first line, giving ample space for how long it might be.
349   fclose(cmds_file);  // drop the file again.
350   if (!chars_read || negative(chars_read)) {
351     COMPLAIN("failed to get any characters from our process's cmdline file.\n");
352     return "unknown";
353   }
354   istring buffer = buff;
355   delete [] buff;
356   // clean out quote characters from the name.
357   for (int i = buffer.length() - 1; i >= 0; i--) {
358     if (buffer[i] == '"') buffer.zap(i, i);
359   }
360   return buffer;
361 }
362
363 // deprecated; better to use the /proc/pid/cmdline file.
364 istring query_for_process_info()
365 {
366   FUNCDEF("query_for_process_info");
367   istring to_return = "unknown";
368   // we ask the operating system about our process identifier and store
369   // the results in a temporary file.
370   chaos rando;
371   isprintf tmpfile("/tmp/proc_name_check_%d_%d.txt", portable::process_id(),
372       rando.inclusive(0, 128000));
373   isprintf cmd("ps h --format \"%%a\" %d >%s", portable::process_id(),
374       tmpfile.s());
375   // run the command to locate our process info.
376   int sysret = system(cmd.s());
377   if (negative(sysret)) {
378     COMPLAIN_QUERY("failed to run ps command to get process info");
379     return to_return;
380   }
381   // open the output file for reading.
382   FILE *output = fopen(tmpfile.s(), "r");
383   if (!output) {
384     COMPLAIN_QUERY("failed to open the ps output file");
385     return to_return;
386   }
387   // read the file's contents into a string buffer.
388   char buff[MAXIMUM_COMMAND_LINE];
389   size_t size_read = fread(buff, 1, MAXIMUM_COMMAND_LINE, output);
390   if (size_read > 0) {
391     // success at finding some text in the file at least.
392     while (size_read > 0) {
393       const char to_check = buff[size_read - 1];
394       if ( !to_check || (to_check == '\r') || (to_check == '\n')
395           || (to_check == '\t') )
396         size_read--;
397       else break;
398     }
399     to_return.reset(istring::UNTERMINATED, buff, size_read);
400   } else {
401     // couldn't read anything.
402     COMPLAIN_QUERY("could not read output of process list");
403   }
404   unlink(tmpfile.s());
405   return to_return;
406 }
407 #endif
408
409 istring module_name(const void *module_handle)
410 {
411 #ifdef __UNIX__
412 //hmmm: implement module name for linux if that makes sense.
413   if (module_handle) {}
414   return application_name();
415 #elif defined(__WIN32__)
416   flexichar low_buff[MAX_ABS_PATH + 1];
417   GetModuleFileName((HMODULE)module_handle, low_buff, MAX_ABS_PATH - 1);
418   istring buff = from_unicode_temp(low_buff);
419   buff.to_lower();
420   return buff;
421 #else
422   #pragma message("module_name unknown for this operating system.")
423   return application_name();
424 #endif
425 }
426
427 u_int system_error()
428 {
429 #if defined(__UNIX__)
430   return errno;
431 #elif defined(__WIN32__)
432   return GetLastError();
433 #else
434   #pragma error("hmmm: no code for error number for this operating system")
435   return 0;
436 #endif
437 }
438
439 istring system_error_text(u_int to_name)
440 {
441 #if defined(__UNIX__)
442   return strerror(to_name);
443 #elif defined(__WIN32__)
444   char error_text[1000];
445   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NIL, to_name,
446       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)error_text,
447       sizeof(error_text) - 1, NIL);
448   istring to_return = error_text;
449   // trim off the ridiculous carriage return they add.
450   while ( (to_return[to_return.end()] == '\r')
451       || (to_return[to_return.end()] == '\n') )
452     to_return.zap(to_return.end(), to_return.end());
453   return to_return;
454 #else
455   #pragma error("hmmm: no code for error text for this operating system")
456   return "";
457 #endif
458 }
459
460 #ifdef __WIN32__
461
462 bool is_address_valid(const void *address, int size_expected, bool writable)
463 {
464   return address && !IsBadReadPtr(address, size_expected)
465       && !(writable && IsBadWritePtr((void *)address, size_expected));
466 }
467
468 #endif // win32
469
470 version get_OS_version()
471 {
472   version to_return;
473 #ifdef __UNIX__
474   utsname kernel_parms;
475   uname(&kernel_parms);
476   to_return = version(kernel_parms.release);
477 #elif defined(__WIN32__)
478   OSVERSIONINFO info;
479   info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
480   ::GetVersionEx(&info);
481   to_return = version(isprintf("%u.%u.%u.%u", u_short(info.dwMajorVersion),
482       u_short(info.dwMinorVersion), u_short(info.dwPlatformId),
483       u_short(info.dwBuildNumber)));
484 #elif defined(EMBEDDED_BUILD)
485   // no version support.
486 #else
487   #pragma error("hmmm: need version info for this OS!")
488 #endif
489   return to_return;
490 }
491
492 // for non-win32 and non-unix OSes, this might not work.
493 #if defined(__UNIX__) || defined(__WIN32__)
494   u_int process_id() { return getpid(); }
495 #else
496   #pragma error("hmmm: need process id implementation for this OS!")
497   u_int process_id() { return 0; }
498 #endif
499
500 istring current_directory()
501 {
502   istring to_return;
503 #ifdef __UNIX__
504   char buff[MAX_ABS_PATH];
505   getcwd(buff, MAX_ABS_PATH - 1);
506   to_return = buff;
507 #elif defined(__WIN32__)
508   flexichar low_buff[MAX_ABS_PATH + 1];
509   GetCurrentDirectory(MAX_ABS_PATH, low_buff);
510   to_return = from_unicode_temp(low_buff);
511 #else
512   #pragma error("hmmm: need support for current directory on this OS.")
513   to_return = ".";
514 #endif
515   return to_return;
516 }
517
518 timeval fill_timeval_ms(int duration)
519 {
520   timeval time_out;  // timeval has tv_sec=seconds, tv_usec=microseconds.
521   if (!duration) {
522     // duration is immediate for the check; just a quick poll.
523     time_out.tv_sec = 0;
524     time_out.tv_usec = 0;
525 #ifdef DEBUG_PORTABLE
526 //    LOG("no duration specified");
527 #endif
528   } else {
529     // a non-zero duration means we need to compute secs and usecs.
530     time_out.tv_sec = duration / 1000;
531     // set the number of seconds from the input in milliseconds.
532     duration -= time_out.tv_sec * 1000;
533     // now take out the chunk we've already recorded as seconds.
534     time_out.tv_usec = duration * 1000;
535     // set the number of microseconds from the remaining milliseconds.
536 #ifdef DEBUG_PORTABLE
537 //    LOG(isprintf("duration of %d ms went to %d sec and %d usec.", duration,
538 //        time_out.tv_sec, time_out.tv_usec));
539 #endif
540   }
541   return time_out;
542 }
543
544 //hmmm: this doesn't seem to account for quoting properly at all?
545 basis::char_star_array break_line(istring &app, const istring &parameters)
546 {
547   basis::char_star_array to_return;
548   int_array posns;
549   int num = 0;
550   // find the positions of the spaces and count them.
551   for (int j = 0; j < parameters.length(); j++) {
552     if (parameters[j] == ' ') {
553       num++;
554       posns += j;
555     }
556   }
557   // first, add the app name to the list of parms.
558   to_return += new char[app.length() + 1];
559   app.stuff(to_return[0], app.length());
560   int last_posn = 0;
561   // now add each space-separated parameter to the list.
562   for (int i = 0; i < num; i++) {
563     int len = posns[i] - last_posn;
564     to_return += new char[len + 1];
565     parameters.substring(last_posn, posns[i] - 1).stuff(to_return[i + 1], len);
566     last_posn = posns[i] + 1;
567   }
568   // catch anything left after last separator.
569   if (last_posn < parameters.length() - 1) {
570     int len = parameters.length() - last_posn;
571     to_return += new char[len + 1];
572     parameters.substring(last_posn, parameters.length() - 1)
573         .stuff(to_return[to_return.last()], len);
574   }
575   // add the sentinel to the list of strings.
576   to_return += NIL;
577 #ifdef DEBUG_PORTABLE
578   for (int q = 0; to_return[q]; q++) {
579     printf("%d: %s\n", q, to_return[q]);
580   }
581 #endif
582   // now a special detour; fix the app name to remove quotes, which are
583   // not friendly to pass to exec.
584   if (app[0] == '"') app.zap(0, 0);
585   if (app[app.end()] == '"') app.zap(app.end(), app.end());
586   return to_return;
587 }
588
589 #ifdef __UNIX__
590
591 void exiting_child_signal_handler(int sig_num)
592 {
593   FUNCDEF("exiting_child_signal_handler");
594   if (sig_num != SIGCHLD) {
595     // uhhh, this seems wrong.
596   }
597   auto_synchronizer l(__process_synchronizer());
598   for (int i = 0; i < __our_kids().length(); i++) {
599     int status;
600     pid_t exited = waitpid(__our_kids()[i], &status, WNOHANG);
601     if ( (exited == -1) || (exited == __our_kids()[i]) ) {
602       // negative one denotes an error, which we are going to assume means the
603       // process has exited via some other method than our wait.  if the value
604       // is the same as the process we waited for, that means it exited.
605       __our_kids().zap(i, i);
606       i--;
607     } else if (exited != 0) {
608       // zero would be okay; this result we do not understand.
609 #ifdef DEBUG_PORTABLE
610       printf("unknown result %d waiting for process %d", exited,
611           __our_kids()[i]);
612 #endif
613     }
614   }
615 }
616 #endif
617
618 u_int launch_process(const istring &app_name_in, const istring &command_line,
619     int flag, u_int &child_id)
620 {
621   child_id = 0;
622   istring app_name = app_name_in;
623   if (app_name[0] != '"')
624     app_name.insert(0, "\"");
625   if (app_name[app_name.end()] != '"')
626     app_name += "\"";
627 #ifdef __UNIX__
628   // unix / linux implementation.
629   if (flag & RETURN_IMMEDIATELY) {
630     // they want to get back right away.
631     pid_t kid_pid = fork();
632 #ifdef DEBUG_PORTABLE
633     printf("launch fork returned %d\n", kid_pid);
634 #endif
635     if (!kid_pid) {
636       // this is the child; we now need to launch into what we were asked for.
637 #ifdef DEBUG_PORTABLE
638       printf((isprintf("process %d execing ", process_id()) + app_name
639           + " parms " + command_line + "\n").s());
640 #endif
641       basis::char_star_array parms = break_line(app_name, command_line);
642       execv(app_name.s(), parms.observe());
643       // oops.  failed to exec if we got to here.
644 #ifdef DEBUG_PORTABLE
645       printf((isprintf("child of fork (pid %d) failed to exec, "
646           "error is ", process_id()) + system_error_text(system_error())
647           + "\n").s());
648 #endif
649       exit(0);  // leave since this is a failed child process.
650     } else {
651       // this is the parent.  let's see if the launch worked.
652       if (kid_pid == -1) {
653         // failure.
654         u_int to_return = system_error();
655 #ifdef DEBUG_PORTABLE
656         printf((isprintf("parent %d is returning after failing to create, "
657             "error is ", process_id()) + system_error_text(to_return)
658             + "\n").s());
659 #endif
660         return to_return;
661       } else {
662         // yes, launch worked okay.
663         child_id = kid_pid;
664         {
665           auto_synchronizer l(__process_synchronizer());
666           __our_kids() += kid_pid;
667         }
668         // hook in our child exit signal handler.
669         signal(SIGCHLD, exiting_child_signal_handler);
670
671 #ifdef DEBUG_PORTABLE
672         printf((isprintf("parent %d is returning after successfully "
673             "creating %d ", process_id(), kid_pid) + app_name
674             + " parms " + command_line + "\n").s());
675 #endif
676         return 0;
677       }
678     }
679   } else {
680     // assume they want to wait.
681     return system((app_name + " " + command_line).s());
682   }
683 #elif defined(__WIN32__)
684
685 //checking on whether we have admin rights for the launch.
686 //if (IsUserAnAdmin()) {
687 //  MessageBox(0, (istring("IS admin with ") + app_name_in + " " + command_line).s(), "launch process", MB_OK);
688 //} else {
689 //  MessageBox(0, (istring("NOT admin for ") + app_name_in + " " + command_line).s(), "launch process", MB_OK);
690 //}
691
692   PROCESS_INFORMATION process_info;
693   ZeroMemory(&process_info, sizeof(PROCESS_INFORMATION));
694
695 #ifdef SUPPORT_SHELL_EXECUTE
696   if (flag & SHELL_EXECUTE) {
697     // new code for using shell execute method--required on vista for proper
698     // launching with the right security tokens.
699     int show_cmd = 0;
700     if (flag & HIDE_APP_WINDOW) {
701       // magic that hides a console window for mswindows.
702       show_cmd = SW_HIDE;
703     } else {
704       show_cmd = SW_SHOWNORMAL;
705     }
706
707     SHELLEXECUTEINFO exec_info;
708     ZeroMemory(&exec_info, sizeof(SHELLEXECUTEINFO));
709     exec_info.cbSize = sizeof(SHELLEXECUTEINFO);
710     exec_info.fMask = SEE_MASK_NOCLOSEPROCESS  // get the process info.
711         | SEE_MASK_FLAG_NO_UI;  // turn off any visible error dialogs.
712     exec_info.hwnd = GetDesktopWindow();
713 //hmmm: is get desktop window always appropriate?
714     to_unicode_persist(temp_verb, "open");
715 //does "runas" work on xp also?  or anywhere?
716     exec_info.lpVerb = temp_verb;
717     to_unicode_persist(temp_file, app_name);
718     exec_info.lpFile = temp_file;
719     to_unicode_persist(temp_parms, command_line);
720     exec_info.lpParameters = temp_parms;
721     exec_info.nShow = show_cmd;
722 //    exec_info.hProcess = &process_info;
723
724     BOOL worked = ShellExecuteEx(&exec_info);
725     if (!worked)
726       return system_error();
727     // copy out the returned process handle.
728     process_info.hProcess = exec_info.hProcess;
729     process_info.dwProcessId = GetProcessId(exec_info.hProcess);
730   } else {
731 #endif //shell exec
732     // standard windows implementation using CreateProcess.
733     STARTUPINFO startup_info;
734     ZeroMemory(&startup_info, sizeof(STARTUPINFO));
735     startup_info.cb = sizeof(STARTUPINFO);
736     int create_flag = 0;
737     if (flag & HIDE_APP_WINDOW) {
738       // magic that hides a console window for mswindows.
739 //      version ver = portable::get_OS_version();
740 //      version vista_version(6, 0);
741 //      if (ver < vista_version) {
742 //        // we suspect that this flag is hosing us in vista.
743         create_flag = CREATE_NO_WINDOW;
744 //      }
745     }
746     istring parms = app_name + " " + command_line;
747     bool success = CreateProcess(NIL, to_unicode_temp(parms), NIL, NIL, false,
748         create_flag, NIL, NIL, &startup_info, &process_info);
749     if (!success)
750       return system_error();
751     // success then, merge back into stream.
752
753 #ifdef SUPPORT_SHELL_EXECUTE
754   }
755 #endif //shell exec
756
757   // common handling for CreateProcess and ShellExecuteEx.
758   child_id = process_info.dwProcessId;
759   u_long retval = 0;
760   if (flag & AWAIT_VIA_POLLING) {
761     // this type of waiting is done without blocking on the process.
762     while (true) {
763       MSG msg;
764       event_poll(msg);
765       // check if the process is gone yet.
766       BOOL ret = GetExitCodeProcess(process_info.hProcess, &retval);
767       if (!ret) {
768         break;
769       } else {
770         // if they aren't saying it's still active, then we will leave.
771         if (retval != STILL_ACTIVE)
772           break;
773       }
774       sleep_ms(14);
775     }
776   } else if (flag & AWAIT_APP_EXIT) {
777     // they want to wait for the process to exit.
778     WaitForInputIdle(process_info.hProcess, INFINITE); 
779     WaitForSingleObject(process_info.hProcess, INFINITE);
780     GetExitCodeProcess(process_info.hProcess, &retval);
781   }
782   // drop the process and thread handles.
783   if (process_info.hProcess)
784     CloseHandle(process_info.hProcess);
785   if (process_info.hThread)
786     CloseHandle(process_info.hThread);
787   return (u_int)retval;
788 #else
789   #pragma error("hmmm: launch_process: no implementation for this OS.")
790 #endif
791   return 0;
792 }
793
794 ////////////////////////////////////////////////////////////////////////////
795
796 #ifdef __UNIX__
797
798 char *itoa(int to_convert, char *buffer, int radix)
799 {
800   // slow implementation; may need enhancement for serious speed.
801   // ALSO: only supports base 10 and base 16 currently.
802   const char *formatter = "%d";
803   if (radix == 16) formatter = "%x";
804   isprintf printed(formatter, to_convert);
805   printed.stuff(buffer, printed.length() + 1);
806   return buffer;
807 }
808
809 #endif
810 */
811
812 #ifdef __WIN32__
813
814 /*
815 void show_wait_cursor() { SetCursor(LoadCursor(NULL, IDC_WAIT)); }
816
817 void show_normal_cursor() { SetCursor(LoadCursor(NULL, IDC_ARROW)); }
818
819 istring rc_string(UINT id, application_instance instance)
820 {
821   flexichar temp[MAX_ABS_PATH + 1];
822   int ret = LoadString(instance, id, temp, MAX_ABS_PATH);
823   if (!ret) return istring();
824   return istring(from_unicode_temp(temp));
825 }
826
827 istring rc_string(u_int id) { return rc_string(id, GET_INSTANCE_HANDLE()); }
828 */
829
830 const char *opsystem_name(known_operating_systems which)
831 {
832   switch (which) {
833     case WIN_95: return "WIN_95";
834     case WIN_NT: return "WIN_NT";
835     case WIN_2K: return "WIN_2K";
836     case WIN_XP: return "WIN_XP";
837     case WIN_SRV2K3: return "WIN_SRV2K3";
838     case WIN_VISTA: return "WIN_VISTA";
839     case WIN_SRV2K8: return "WIN_SRV2K8";
840     default: return "UNKNOWN_OS";
841   }
842 }
843
844 known_operating_systems determine_OS()
845 {
846   version osver = application_configuration::get_OS_version();
847   if ( (osver.v_major() == 4) && (osver.v_minor() == 0) ) {
848     if (osver.v_revision() == VER_PLATFORM_WIN32_WINDOWS) return WIN_95;
849     if (osver.v_revision() == VER_PLATFORM_WIN32_NT) return WIN_NT;
850   } else if ( (osver.v_major() == 5) && (osver.v_minor() == 0) ) {
851     return WIN_2K;
852   } else if ( (osver.v_major() == 5) && (osver.v_minor() == 1) ) {
853     return WIN_XP;
854   } else if ( (osver.v_major() == 5) && (osver.v_minor() == 2) ) {
855     return WIN_SRV2K3;
856   } else if ( (osver.v_major() == 6) && (osver.v_minor() == 0) ) {
857     return WIN_VISTA;
858   } else if ( (osver.v_major() == 6) && (osver.v_minor() == 1) ) {
859     return WIN_SRV2K8;
860   }
861   return UNKNOWN_OS;
862 }
863
864 #endif // win32
865
866 } // namespace.
867
868 #undef static_class_name
869