GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
async-system-wrapper.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 // areadlink is provided by gnulib. We don't include gnulib headers
27 // directly in Octave's C++ source files to avoid problems that may be
28 // caused by the way that gnulib overrides standard library functions.
29 
30 #if defined (HAVE_CONFIG_H)
31 # include "config.h"
32 #endif
33 
34 #if defined (__WIN32__) && ! defined (__CYGWIN__)
35 # include <stdlib.h>
36 # include <string.h>
37 # define WIN32_LEAN_AND_MEAN 1
38 # include <windows.h>
39 #else
40 # include <sys/types.h>
41 # include <unistd.h>
42 #endif
43 
44 #include "async-system-wrapper.h"
45 
46 #if defined (OCTAVE_USE_WINDOWS_API)
47 # include "uniconv-wrappers.h"
48 #endif
49 
50 pid_t
51 octave_async_system_wrapper (const char *cmd)
52 {
53  int retval = -1;
54 
55  if (! cmd)
56  return retval;
57 
58 #if defined (OCTAVE_USE_WINDOWS_API)
59 
60  STARTUPINFOW si;
61  PROCESS_INFORMATION pi;
62 
63  ZeroMemory (&si, sizeof (si));
64  ZeroMemory (&pi, sizeof (pi));
65 
66  wchar_t *xcmd = u8_to_wchar (cmd);
67 
68  if (! CreateProcessW (NULL, xcmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
69  retval = -1;
70  else
71  {
72  retval = pi.dwProcessId;
73 
74  CloseHandle (pi.hProcess);
75  CloseHandle (pi.hThread);
76  }
77 
78  free (xcmd);
79 
80 #else
81 
82  pid_t pid = fork ();
83 
84  if (pid == 0)
85  execl (SHELL_PATH, "sh", "-c", cmd, (char *) (0));
86  else
87  retval = pid;
88 
89 #endif
90 
91  return retval;
92 }
pid_t octave_async_system_wrapper(const char *cmd)
#define SHELL_PATH
Definition: oct-procbuf.cc:61
pid_t fork(std::string &msg)
Definition: oct-syscalls.cc:99
void free(void *)
wchar_t * u8_to_wchar(const char *u8)