Mercurial > hg > octave-nkf
annotate liboctave/lo-sysdep.cc @ 10250:2d47356a7a1a
use gnulib getcwd module
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 03 Feb 2010 03:07:06 -0500 |
parents | 0522a65bcd56 |
children | 65b41bc71f09 |
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 #include <sys/types.h> | |
32 #include <unistd.h> | |
33 | |
6321 | 34 #ifdef HAVE_FCNTL_H |
35 #include <fcntl.h> | |
36 #endif | |
37 | |
7695
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
38 #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
|
39 #include <windows.h> |
9941
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
40 #ifdef max |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
41 # undef min |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
42 # undef max |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
43 #endif |
7695
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 |
10250 | 58 // Using the gnulib getcwd module ensures that we have a getcwd that |
59 // will allocate a buffer as large as necessary if buf and size are | |
60 // both 0. | |
3069 | 61 |
10250 | 62 char *tmp = getcwd (0, 0); |
2926 | 63 |
64 if (tmp) | |
10250 | 65 { |
66 retval = tmp; | |
67 free (tmp); | |
68 } | |
3069 | 69 else |
70 (*current_liboctave_error_handler) ("unable to find current directory"); | |
2926 | 71 |
72 return retval; | |
73 } | |
74 | |
75 int | |
5872 | 76 octave_chdir (const std::string& path_arg) |
2926 | 77 { |
5872 | 78 std::string path = file_ops::tilde_expand (path_arg); |
79 | |
2926 | 80 #if defined (__EMX__) |
81 int retval = -1; | |
82 | |
83 char *tmp_path = strsave (path.c_str ()); | |
84 | |
85 if (path.length () == 2 && path[1] == ':') | |
86 { | |
87 char *upper_case_dir_name = strupr (tmp_path); | |
88 _chdrive (upper_case_dir_name[0]); | |
89 if (_getdrive () == upper_case_dir_name[0]) | |
90 retval = _chdir2 ("/"); | |
91 } | |
92 else | |
93 retval = _chdir2 (tmp_path); | |
94 | |
95 delete [] tmp_path; | |
96 | |
97 return retval; | |
98 #else | |
6244 | 99 |
100 #if defined (__WIN32__) && ! defined (__CYGWIN__) | |
101 if (path.length() == 2 && path[1] == ':') | |
102 path += "\\"; | |
103 #endif | |
104 | |
2926 | 105 return chdir (path.c_str ()); |
106 #endif | |
107 } | |
108 | |
6321 | 109 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
110 | |
111 pid_t | |
112 octave_popen2 (const std::string& cmd, const string_vector& args, bool sync_mode, | |
113 int *fildes, std::string& msg) | |
114 { | |
115 pid_t pid; | |
116 PROCESS_INFORMATION pi; | |
117 STARTUPINFO si; | |
118 std::string command = "\"" + cmd + "\""; | |
119 HANDLE hProcess = GetCurrentProcess(), childRead, childWrite, parentRead, parentWrite; | |
120 DWORD pipeMode; | |
121 | |
122 ZeroMemory (&pi, sizeof (pi)); | |
123 ZeroMemory (&si, sizeof (si)); | |
124 si.cb = sizeof (si); | |
125 | |
126 if (! CreatePipe (&childRead, &parentWrite, 0, 0) || | |
127 ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
128 { | |
129 msg = "popen2: pipe creation failed"; | |
130 return -1; | |
131 } | |
132 if (! CreatePipe (&parentRead, &childWrite, 0, 0) || | |
133 ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
134 { | |
135 msg = "popen2: pipe creation failed"; | |
136 return -1; | |
137 } | |
138 if (! sync_mode) | |
139 { | |
140 pipeMode = PIPE_NOWAIT; | |
141 SetNamedPipeHandleState (parentRead, &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 | |
6123 | 178 struct __DIR |
6093 | 179 { |
180 HANDLE hnd; | |
181 WIN32_FIND_DATA fd; | |
182 int dirty; | |
183 struct direct d; | |
6123 | 184 const char *current; |
185 }; | |
6093 | 186 |
187 DIR * | |
188 opendir (const char *name) | |
189 { | |
190 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); | |
191 static char buffer[MAX_PATH]; | |
192 | |
193 strncpy (buffer, name, MAX_PATH); | |
6208 | 194 if (buffer[strnlen(buffer, MAX_PATH)-1] != '\\') |
195 strncat (buffer, "\\*", MAX_PATH); | |
196 else | |
197 strncat (buffer, "*", MAX_PATH); | |
6093 | 198 d->current = buffer; |
199 d->hnd = FindFirstFile (buffer, &(d->fd)); | |
200 if (d->hnd == INVALID_HANDLE_VALUE) | |
201 return 0; | |
202 d->dirty = 1; | |
203 return d; | |
204 } | |
205 | |
206 void | |
6123 | 207 rewinddir (DIR *d) |
6093 | 208 { |
209 if (d->hnd != INVALID_HANDLE_VALUE) | |
210 FindClose (d->hnd); | |
211 d->hnd = FindFirstFile (d->current, &(d->fd)); | |
212 d->dirty = 1; | |
213 } | |
214 | |
215 void | |
216 closedir (DIR *d) | |
217 { | |
218 if (d->hnd != INVALID_HANDLE_VALUE) | |
219 FindClose (d->hnd); | |
220 free (d); | |
221 } | |
222 | |
223 struct direct * | |
224 readdir (DIR *d) | |
225 { | |
226 if (! d->dirty) | |
227 { | |
228 if (! FindNextFile(d->hnd, &(d->fd))) | |
229 return 0; | |
230 } | |
231 d->d.d_name = d->fd.cFileName; | |
232 d->dirty = 0; | |
233 return &(d->d); | |
234 } | |
235 | |
236 #endif |