2926
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 2000, 2001, 2005, 2006, 2007 John W. Eaton |
2926
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
2926
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
2926
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3503
|
27 #include <iostream> |
2926
|
28 #include <string> |
6321
|
29 #include <vector> |
2926
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #ifdef HAVE_SYS_TYPES_H |
|
33 #include <sys/types.h> |
|
34 #endif |
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
6321
|
38 #ifdef HAVE_FCNTL_H |
|
39 #include <fcntl.h> |
|
40 #endif |
|
41 |
5872
|
42 #include "file-ops.h" |
3069
|
43 #include "lo-error.h" |
2926
|
44 #include "pathlen.h" |
6123
|
45 #include "lo-sysdep.h" |
6321
|
46 #include "str-vec.h" |
2926
|
47 |
3504
|
48 std::string |
2926
|
49 octave_getcwd (void) |
|
50 { |
3504
|
51 std::string retval; |
3069
|
52 |
2926
|
53 char buf[MAXPATHLEN]; |
|
54 |
3069
|
55 char *tmp = 0; |
|
56 |
2926
|
57 #if defined (__EMX__) |
3069
|
58 tmp = _getcwd2 (buf, MAXPATHLEN); |
3112
|
59 #elif defined (HAVE_GETCWD) |
|
60 tmp = getcwd (buf, MAXPATHLEN); |
3069
|
61 #elif defined (HAVE_GETWD) |
|
62 tmp = getwd (buf); |
2926
|
63 #endif |
|
64 |
|
65 if (tmp) |
|
66 retval = tmp; |
3069
|
67 else |
|
68 (*current_liboctave_error_handler) ("unable to find current directory"); |
2926
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
|
73 int |
5872
|
74 octave_chdir (const std::string& path_arg) |
2926
|
75 { |
5872
|
76 std::string path = file_ops::tilde_expand (path_arg); |
|
77 |
2926
|
78 #if defined (__EMX__) |
|
79 int retval = -1; |
|
80 |
|
81 char *tmp_path = strsave (path.c_str ()); |
|
82 |
|
83 if (path.length () == 2 && path[1] == ':') |
|
84 { |
|
85 char *upper_case_dir_name = strupr (tmp_path); |
|
86 _chdrive (upper_case_dir_name[0]); |
|
87 if (_getdrive () == upper_case_dir_name[0]) |
|
88 retval = _chdir2 ("/"); |
|
89 } |
|
90 else |
|
91 retval = _chdir2 (tmp_path); |
|
92 |
|
93 delete [] tmp_path; |
|
94 |
|
95 return retval; |
|
96 #else |
6244
|
97 |
|
98 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
|
99 if (path.length() == 2 && path[1] == ':') |
|
100 path += "\\"; |
|
101 #endif |
|
102 |
2926
|
103 return chdir (path.c_str ()); |
|
104 #endif |
|
105 } |
|
106 |
6321
|
107 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
|
108 |
|
109 pid_t |
|
110 octave_popen2 (const std::string& cmd, const string_vector& args, bool sync_mode, |
|
111 int *fildes, std::string& msg) |
|
112 { |
|
113 pid_t pid; |
|
114 PROCESS_INFORMATION pi; |
|
115 STARTUPINFO si; |
|
116 std::string command = "\"" + cmd + "\""; |
|
117 HANDLE hProcess = GetCurrentProcess(), childRead, childWrite, parentRead, parentWrite; |
|
118 DWORD pipeMode; |
|
119 |
|
120 ZeroMemory (&pi, sizeof (pi)); |
|
121 ZeroMemory (&si, sizeof (si)); |
|
122 si.cb = sizeof (si); |
|
123 |
|
124 if (! CreatePipe (&childRead, &parentWrite, 0, 0) || |
|
125 ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) |
|
126 { |
|
127 msg = "popen2: pipe creation failed"; |
|
128 return -1; |
|
129 } |
|
130 if (! CreatePipe (&parentRead, &childWrite, 0, 0) || |
|
131 ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) |
|
132 { |
|
133 msg = "popen2: pipe creation failed"; |
|
134 return -1; |
|
135 } |
|
136 if (! sync_mode) |
|
137 { |
|
138 pipeMode = PIPE_NOWAIT; |
|
139 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); |
|
140 SetNamedPipeHandleState (parentWrite, &pipeMode, 0, 0); |
|
141 } |
|
142 fildes[1] = _open_osfhandle (reinterpret_cast<long> (parentRead), _O_RDONLY | _O_BINARY); |
|
143 fildes[0] = _open_osfhandle (reinterpret_cast<long> (parentWrite), _O_WRONLY | _O_BINARY); |
|
144 si.dwFlags |= STARTF_USESTDHANDLES; |
|
145 si.hStdInput = childRead; |
|
146 si.hStdOutput = childWrite; |
|
147 |
|
148 // Ignore first arg as it is the command |
|
149 for (int k=1; k<args.length(); k++) |
|
150 command += " \"" + args[k] + "\""; |
|
151 OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); |
|
152 strcpy (c_command, command.c_str ()); |
|
153 if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) |
|
154 { |
|
155 msg = "popen2: process creation failed"; |
|
156 return -1; |
|
157 } |
|
158 pid = pi.dwProcessId; |
|
159 |
|
160 CloseHandle (childRead); |
|
161 CloseHandle (childWrite); |
|
162 CloseHandle (pi.hProcess); |
|
163 CloseHandle (pi.hThread); |
|
164 |
|
165 return pid; |
|
166 } |
|
167 |
|
168 #endif |
|
169 |
|
170 #if defined (_MSC_VER) && ! defined (HAVE_DIRENT_H) |
6093
|
171 |
|
172 // FIXME -- it would probably be better to adapt the versions of |
|
173 // opendir, readdir, and closedir from Emacs as they appear to be more |
|
174 // complete implementations (do the functions below work for network |
|
175 // paths, for example)? We can probably get along without rewinddir. |
|
176 |
|
177 #include <windows.h> |
|
178 |
6123
|
179 struct __DIR |
6093
|
180 { |
|
181 HANDLE hnd; |
|
182 WIN32_FIND_DATA fd; |
|
183 int dirty; |
|
184 struct direct d; |
6123
|
185 const char *current; |
|
186 }; |
6093
|
187 |
|
188 DIR * |
|
189 opendir (const char *name) |
|
190 { |
|
191 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); |
|
192 static char buffer[MAX_PATH]; |
|
193 |
|
194 strncpy (buffer, name, MAX_PATH); |
6208
|
195 if (buffer[strnlen(buffer, MAX_PATH)-1] != '\\') |
|
196 strncat (buffer, "\\*", MAX_PATH); |
|
197 else |
|
198 strncat (buffer, "*", MAX_PATH); |
6093
|
199 d->current = buffer; |
|
200 d->hnd = FindFirstFile (buffer, &(d->fd)); |
|
201 if (d->hnd == INVALID_HANDLE_VALUE) |
|
202 return 0; |
|
203 d->dirty = 1; |
|
204 return d; |
|
205 } |
|
206 |
|
207 void |
6123
|
208 rewinddir (DIR *d) |
6093
|
209 { |
|
210 if (d->hnd != INVALID_HANDLE_VALUE) |
|
211 FindClose (d->hnd); |
|
212 d->hnd = FindFirstFile (d->current, &(d->fd)); |
|
213 d->dirty = 1; |
|
214 } |
|
215 |
|
216 void |
|
217 closedir (DIR *d) |
|
218 { |
|
219 if (d->hnd != INVALID_HANDLE_VALUE) |
|
220 FindClose (d->hnd); |
|
221 free (d); |
|
222 } |
|
223 |
|
224 struct direct * |
|
225 readdir (DIR *d) |
|
226 { |
|
227 if (! d->dirty) |
|
228 { |
|
229 if (! FindNextFile(d->hnd, &(d->fd))) |
|
230 return 0; |
|
231 } |
|
232 d->d.d_name = d->fd.cFileName; |
|
233 d->dirty = 0; |
|
234 return &(d->d); |
|
235 } |
|
236 |
|
237 #endif |
|
238 |
2926
|
239 /* |
|
240 ;;; Local Variables: *** |
|
241 ;;; mode: C++ *** |
|
242 ;;; End: *** |
|
243 */ |