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