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