Mercurial > octave-nkf
annotate liboctave/lo-sysdep.cc @ 10314:07ebe522dac2
untabify liboctave C++ sources
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 11 Feb 2010 12:23:32 -0500 |
parents | 65b41bc71f09 |
children | 65d5776379c3 |
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__) |
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
37 #include <windows.h> |
9941
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
38 #ifdef max |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
39 # undef min |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
40 # undef max |
1369f13ae6b2
several fixes by M. Goffioul
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
41 #endif |
7695
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
42 #endif |
eacf87a24f55
lo-sysdep.cc: include windows.h if windows and not cygwin
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
43 |
5872 | 44 #include "file-ops.h" |
3069 | 45 #include "lo-error.h" |
2926 | 46 #include "pathlen.h" |
6123 | 47 #include "lo-sysdep.h" |
6321 | 48 #include "str-vec.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
7732
diff
changeset
|
49 #include "oct-locbuf.h" |
2926 | 50 |
3504 | 51 std::string |
2926 | 52 octave_getcwd (void) |
53 { | |
3504 | 54 std::string retval; |
3069 | 55 |
10250 | 56 // Using the gnulib getcwd module ensures that we have a getcwd that |
57 // will allocate a buffer as large as necessary if buf and size are | |
58 // both 0. | |
3069 | 59 |
10250 | 60 char *tmp = getcwd (0, 0); |
2926 | 61 |
62 if (tmp) | |
10250 | 63 { |
64 retval = tmp; | |
65 free (tmp); | |
66 } | |
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]) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
88 retval = _chdir2 ("/"); |
2926 | 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 } | |
141 fildes[1] = _open_osfhandle (reinterpret_cast<long> (parentRead), _O_RDONLY | _O_BINARY); | |
142 fildes[0] = _open_osfhandle (reinterpret_cast<long> (parentWrite), _O_WRONLY | _O_BINARY); | |
143 si.dwFlags |= STARTF_USESTDHANDLES; | |
144 si.hStdInput = childRead; | |
145 si.hStdOutput = childWrite; | |
146 | |
147 // Ignore first arg as it is the command | |
148 for (int k=1; k<args.length(); k++) | |
149 command += " \"" + args[k] + "\""; | |
150 OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); | |
151 strcpy (c_command, command.c_str ()); | |
152 if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) | |
153 { | |
154 msg = "popen2: process creation failed"; | |
155 return -1; | |
156 } | |
157 pid = pi.dwProcessId; | |
158 | |
159 CloseHandle (childRead); | |
160 CloseHandle (childWrite); | |
161 CloseHandle (pi.hProcess); | |
162 CloseHandle (pi.hThread); | |
163 | |
164 return pid; | |
165 } | |
166 | |
167 #endif | |
168 | |
169 #if defined (_MSC_VER) && ! defined (HAVE_DIRENT_H) | |
6093 | 170 |
171 // FIXME -- it would probably be better to adapt the versions of | |
172 // opendir, readdir, and closedir from Emacs as they appear to be more | |
173 // complete implementations (do the functions below work for network | |
174 // paths, for example)? We can probably get along without rewinddir. | |
175 | |
6123 | 176 struct __DIR |
6093 | 177 { |
178 HANDLE hnd; | |
179 WIN32_FIND_DATA fd; | |
180 int dirty; | |
181 struct direct d; | |
6123 | 182 const char *current; |
183 }; | |
6093 | 184 |
185 DIR * | |
186 opendir (const char *name) | |
187 { | |
188 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); | |
189 static char buffer[MAX_PATH]; | |
190 | |
191 strncpy (buffer, name, MAX_PATH); | |
6208 | 192 if (buffer[strnlen(buffer, MAX_PATH)-1] != '\\') |
193 strncat (buffer, "\\*", MAX_PATH); | |
194 else | |
195 strncat (buffer, "*", MAX_PATH); | |
6093 | 196 d->current = buffer; |
197 d->hnd = FindFirstFile (buffer, &(d->fd)); | |
198 if (d->hnd == INVALID_HANDLE_VALUE) | |
199 return 0; | |
200 d->dirty = 1; | |
201 return d; | |
202 } | |
203 | |
204 void | |
6123 | 205 rewinddir (DIR *d) |
6093 | 206 { |
207 if (d->hnd != INVALID_HANDLE_VALUE) | |
208 FindClose (d->hnd); | |
209 d->hnd = FindFirstFile (d->current, &(d->fd)); | |
210 d->dirty = 1; | |
211 } | |
212 | |
213 void | |
214 closedir (DIR *d) | |
215 { | |
216 if (d->hnd != INVALID_HANDLE_VALUE) | |
217 FindClose (d->hnd); | |
218 free (d); | |
219 } | |
220 | |
221 struct direct * | |
222 readdir (DIR *d) | |
223 { | |
224 if (! d->dirty) | |
225 { | |
226 if (! FindNextFile(d->hnd, &(d->fd))) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
227 return 0; |
6093 | 228 } |
229 d->d.d_name = d->fd.cFileName; | |
230 d->dirty = 0; | |
231 return &(d->d); | |
232 } | |
233 | |
234 #endif |