lo-sysdep.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <iostream>
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <sys/types.h>
00032 #include <unistd.h>
00033 
00034 #include <fcntl.h>
00035 
00036 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00037 #define WIN32_LEAN_AND_MEAN
00038 #include <windows.h>
00039 #endif
00040 
00041 #include "file-ops.h"
00042 #include "lo-error.h"
00043 #include "pathlen.h"
00044 #include "lo-sysdep.h"
00045 #include "str-vec.h"
00046 #include "oct-locbuf.h"
00047 
00048 std::string
00049 octave_getcwd (void)
00050 {
00051   std::string retval;
00052 
00053   // Using the gnulib getcwd module ensures that we have a getcwd that
00054   // will allocate a buffer as large as necessary if buf and size are
00055   // both 0.
00056 
00057   char *tmp = gnulib::getcwd (0, 0);
00058 
00059   if (tmp)
00060     {
00061       retval = tmp;
00062       free (tmp);
00063     }
00064   else
00065     (*current_liboctave_error_handler) ("unable to find current directory");
00066 
00067   return retval;
00068 }
00069 
00070 int
00071 octave_chdir (const std::string& path_arg)
00072 {
00073   std::string path = file_ops::tilde_expand (path_arg);
00074 
00075 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00076   if (path.length() == 2 && path[1] == ':')
00077     path += "\\";
00078 #endif
00079 
00080   return gnulib::chdir (path.c_str ());
00081 }
00082 
00083 #if defined (__WIN32__) && ! defined (__CYGWIN__)
00084 
00085 pid_t
00086 octave_popen2 (const std::string& cmd, const string_vector& args, bool sync_mode,
00087     int *fildes, std::string& msg)
00088 {
00089   pid_t pid;
00090   PROCESS_INFORMATION pi;
00091   STARTUPINFO si;
00092   std::string command = "\"" + cmd + "\"";
00093   HANDLE hProcess = GetCurrentProcess(), childRead, childWrite, parentRead, parentWrite;
00094   DWORD pipeMode;
00095 
00096   ZeroMemory (&pi, sizeof (pi));
00097   ZeroMemory (&si, sizeof (si));
00098   si.cb = sizeof (si);
00099 
00100   if (! CreatePipe (&childRead, &parentWrite, 0, 0) ||
00101       ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
00102     {
00103       msg = "popen2: pipe creation failed";
00104       return -1;
00105     }
00106   if (! CreatePipe (&parentRead, &childWrite, 0, 0) ||
00107       ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
00108     {
00109       msg = "popen2: pipe creation failed";
00110       return -1;
00111     }
00112   if (! sync_mode)
00113     {
00114       pipeMode = PIPE_NOWAIT;
00115       SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0);
00116     }
00117   fildes[1] = _open_osfhandle (reinterpret_cast<long> (parentRead), _O_RDONLY | _O_BINARY);
00118   fildes[0] = _open_osfhandle (reinterpret_cast<long> (parentWrite), _O_WRONLY | _O_BINARY);
00119   si.dwFlags |= STARTF_USESTDHANDLES;
00120   si.hStdInput = childRead;
00121   si.hStdOutput = childWrite;
00122 
00123   // Ignore first arg as it is the command
00124   for (int k=1; k<args.length(); k++)
00125     command += " \"" + args[k] + "\"";
00126   OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1);
00127   strcpy (c_command, command.c_str ());
00128   if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi))
00129     {
00130       msg = "popen2: process creation failed";
00131       return -1;
00132     }
00133   pid = pi.dwProcessId;
00134 
00135   CloseHandle (childRead);
00136   CloseHandle (childWrite);
00137   CloseHandle (pi.hProcess);
00138   CloseHandle (pi.hThread);
00139 
00140   return pid;
00141 }
00142 
00143 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines