Mercurial > hg > octave-nkf
annotate liboctave/lo-sysdep.cc @ 11584:cda4aa780d58
Another round of initialising members in the constructor initialisation list
author | Pascal Dupuis <Pascal.Dupuis@uclouvain.be> |
---|---|
date | Thu, 20 Jan 2011 17:07:26 -0500 |
parents | fd0a3ac60b0e |
children | 677be77b684b |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1996-2011 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 |
10411 | 57 char *tmp = gnulib::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 | |
6244 | 75 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
76 if (path.length() == 2 && path[1] == ':') | |
77 path += "\\"; | |
78 #endif | |
79 | |
2926 | 80 return chdir (path.c_str ()); |
81 } | |
82 | |
6321 | 83 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
84 | |
85 pid_t | |
86 octave_popen2 (const std::string& cmd, const string_vector& args, bool sync_mode, | |
87 int *fildes, std::string& msg) | |
88 { | |
89 pid_t pid; | |
90 PROCESS_INFORMATION pi; | |
91 STARTUPINFO si; | |
92 std::string command = "\"" + cmd + "\""; | |
93 HANDLE hProcess = GetCurrentProcess(), childRead, childWrite, parentRead, parentWrite; | |
94 DWORD pipeMode; | |
95 | |
96 ZeroMemory (&pi, sizeof (pi)); | |
97 ZeroMemory (&si, sizeof (si)); | |
98 si.cb = sizeof (si); | |
99 | |
100 if (! CreatePipe (&childRead, &parentWrite, 0, 0) || | |
101 ! DuplicateHandle (hProcess, childRead, hProcess, &childRead, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
102 { | |
103 msg = "popen2: pipe creation failed"; | |
104 return -1; | |
105 } | |
106 if (! CreatePipe (&parentRead, &childWrite, 0, 0) || | |
107 ! DuplicateHandle (hProcess, childWrite, hProcess, &childWrite, 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) | |
108 { | |
109 msg = "popen2: pipe creation failed"; | |
110 return -1; | |
111 } | |
112 if (! sync_mode) | |
113 { | |
114 pipeMode = PIPE_NOWAIT; | |
115 SetNamedPipeHandleState (parentRead, &pipeMode, 0, 0); | |
116 } | |
117 fildes[1] = _open_osfhandle (reinterpret_cast<long> (parentRead), _O_RDONLY | _O_BINARY); | |
118 fildes[0] = _open_osfhandle (reinterpret_cast<long> (parentWrite), _O_WRONLY | _O_BINARY); | |
119 si.dwFlags |= STARTF_USESTDHANDLES; | |
120 si.hStdInput = childRead; | |
121 si.hStdOutput = childWrite; | |
122 | |
123 // Ignore first arg as it is the command | |
124 for (int k=1; k<args.length(); k++) | |
125 command += " \"" + args[k] + "\""; | |
126 OCTAVE_LOCAL_BUFFER (char, c_command, command.length () + 1); | |
127 strcpy (c_command, command.c_str ()); | |
128 if (! CreateProcess (0, c_command, 0, 0, TRUE, 0, 0, 0, &si, &pi)) | |
129 { | |
130 msg = "popen2: process creation failed"; | |
131 return -1; | |
132 } | |
133 pid = pi.dwProcessId; | |
134 | |
135 CloseHandle (childRead); | |
136 CloseHandle (childWrite); | |
137 CloseHandle (pi.hProcess); | |
138 CloseHandle (pi.hThread); | |
139 | |
140 return pid; | |
141 } | |
142 | |
143 #endif | |
144 | |
145 #if defined (_MSC_VER) && ! defined (HAVE_DIRENT_H) | |
6093 | 146 |
147 // FIXME -- it would probably be better to adapt the versions of | |
148 // opendir, readdir, and closedir from Emacs as they appear to be more | |
149 // complete implementations (do the functions below work for network | |
150 // paths, for example)? We can probably get along without rewinddir. | |
151 | |
6123 | 152 struct __DIR |
6093 | 153 { |
154 HANDLE hnd; | |
155 WIN32_FIND_DATA fd; | |
156 int dirty; | |
157 struct direct d; | |
6123 | 158 const char *current; |
159 }; | |
6093 | 160 |
161 DIR * | |
162 opendir (const char *name) | |
163 { | |
164 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); | |
165 static char buffer[MAX_PATH]; | |
166 | |
167 strncpy (buffer, name, MAX_PATH); | |
6208 | 168 if (buffer[strnlen(buffer, MAX_PATH)-1] != '\\') |
169 strncat (buffer, "\\*", MAX_PATH); | |
170 else | |
171 strncat (buffer, "*", MAX_PATH); | |
6093 | 172 d->current = buffer; |
173 d->hnd = FindFirstFile (buffer, &(d->fd)); | |
174 if (d->hnd == INVALID_HANDLE_VALUE) | |
175 return 0; | |
176 d->dirty = 1; | |
177 return d; | |
178 } | |
179 | |
180 void | |
6123 | 181 rewinddir (DIR *d) |
6093 | 182 { |
183 if (d->hnd != INVALID_HANDLE_VALUE) | |
184 FindClose (d->hnd); | |
185 d->hnd = FindFirstFile (d->current, &(d->fd)); | |
186 d->dirty = 1; | |
187 } | |
188 | |
189 void | |
190 closedir (DIR *d) | |
191 { | |
192 if (d->hnd != INVALID_HANDLE_VALUE) | |
193 FindClose (d->hnd); | |
194 free (d); | |
195 } | |
196 | |
197 struct direct * | |
198 readdir (DIR *d) | |
199 { | |
200 if (! d->dirty) | |
201 { | |
202 if (! FindNextFile(d->hnd, &(d->fd))) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10259
diff
changeset
|
203 return 0; |
6093 | 204 } |
205 d->d.d_name = d->fd.cFileName; | |
206 d->dirty = 0; | |
207 return &(d->d); | |
208 } | |
209 | |
210 #endif |