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