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