GNU Octave  3.8.0
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
lo-sysdep.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 #include <string>
29 #include <vector>
30 
31 #include <sys/types.h>
32 #include <unistd.h>
33 
34 #include <fcntl.h>
35 
36 #if defined (__WIN32__) && ! defined (__CYGWIN__)
37 #define WIN32_LEAN_AND_MEAN
38 #include <windows.h>
39 #endif
40 
41 #include "file-ops.h"
42 #include "lo-error.h"
43 #include "pathlen.h"
44 #include "lo-sysdep.h"
45 #include "str-vec.h"
46 #include "oct-locbuf.h"
47 
48 std::string
50 {
51  std::string retval;
52 
53  // Using the gnulib getcwd module ensures that we have a getcwd that
54  // will allocate a buffer as large as necessary if buf and size are
55  // both 0.
56 
57  char *tmp = gnulib::getcwd (0, 0);
58 
59  if (tmp)
60  {
61  retval = tmp;
62  free (tmp);
63  }
64  else
65  (*current_liboctave_error_handler) ("unable to find current directory");
66 
67  return retval;
68 }
69 
70 int
71 octave_chdir (const std::string& path_arg)
72 {
73  std::string path = file_ops::tilde_expand (path_arg);
74 
75 #if defined (__WIN32__) && ! defined (__CYGWIN__)
76  if (path.length () == 2 && path[1] == ':')
77  path += "\\";
78 #endif
79 
80  return gnulib::chdir (path.c_str ());
81 }
82 
83 #if defined (__WIN32__) && ! defined (__CYGWIN__)
84 
85 pid_t
86 octave_popen2 (const std::string& cmd, const string_vector& args,
87  bool sync_mode,
88  int *fildes, std::string& msg)
89 {
90  pid_t pid;
91  PROCESS_INFORMATION pi;
92  STARTUPINFO si;
93  std::string command = "\"" + cmd + "\"";
94  HANDLE hProcess = GetCurrentProcess ();
95  HANDLE childRead, childWrite, parentRead, parentWrite;
96  DWORD pipeMode;
97 
98  ZeroMemory (&pi, sizeof (pi));
99  ZeroMemory (&si, sizeof (si));
100  si.cb = sizeof (si);
101 
102  if (! CreatePipe (&childRead, &parentWrite, 0, 0) ||
103  ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE,
104  DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
105  {
106  msg = "popen2: pipe creation failed";
107  return -1;
108  }
109  if (! CreatePipe (&parentRead, &childWrite, 0, 0) ||
110  ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE,
111  DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
112  {
113  msg = "popen2: pipe creation failed";
114  return -1;
115  }
116  if (! sync_mode)
117  {
118  pipeMode = PIPE_NOWAIT;
119  SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
120  }
121  fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead),
122  _O_RDONLY | _O_BINARY);
123  fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite),
124  _O_WRONLY | _O_BINARY);
125  si.dwFlags |= STARTF_USESTDHANDLES;
126  si.hStdInput = childRead;
127  si.hStdOutput = childWrite;
128 
129  // Ignore first arg as it is the command
130  for (int k=1; k<args.length (); k++)
131  command += " \"" + args[k] + "\"";
132  OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1);
133  strcpy (c_command, command.c_str ());
134  if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi))
135  {
136  msg = "popen2: process creation failed";
137  return -1;
138  }
139  pid = pi.dwProcessId;
140 
141  CloseHandle (childRead);
142  CloseHandle (childWrite);
143  CloseHandle (pi.hProcess);
144  CloseHandle (pi.hThread);
145 
146  return pid;
147 }
148 
149 #endif