GNU Octave  4.0.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-2015 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,
104  0, TRUE,
105  DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
106  {
107  msg = "popen2: pipe creation failed";
108  return -1;
109  }
110  if (! CreatePipe (&parentRead, &childWrite, 0, 0)
111  || ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite,
112  0, TRUE,
113  DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
114  {
115  msg = "popen2: pipe creation failed";
116  return -1;
117  }
118  if (! sync_mode)
119  {
120  pipeMode = PIPE_NOWAIT;
121  SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
122  }
123  fildes[1] = _open_osfhandle (reinterpret_cast<intptr_t> (parentRead),
124  _O_RDONLY | _O_BINARY);
125  fildes[0] = _open_osfhandle (reinterpret_cast<intptr_t> (parentWrite),
126  _O_WRONLY | _O_BINARY);
127  si.dwFlags |= STARTF_USESTDHANDLES;
128  si.hStdInput = childRead;
129  si.hStdOutput = childWrite;
130 
131  // Ignore first arg as it is the command
132  for (int k=1; k<args.length (); k++)
133  command += " \"" + args[k] + "\"";
134  OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1);
135  strcpy (c_command, command.c_str ());
136  if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi))
137  {
138  msg = "popen2: process creation failed";
139  return -1;
140  }
141  pid = pi.dwProcessId;
142 
143  CloseHandle (childRead);
144  CloseHandle (childWrite);
145  CloseHandle (pi.hProcess);
146  CloseHandle (pi.hThread);
147 
148  return pid;
149 }
150 
151 #endif
int octave_chdir(const std::string &path_arg)
Definition: lo-sysdep.cc:71
static std::string tilde_expand(const std::string &)
Definition: file-ops.cc:286
liboctave_error_handler current_liboctave_error_handler
Definition: lo-error.c:38
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
void free(void *)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:197
std::string octave_getcwd(void)
Definition: lo-sysdep.cc:49