1765
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1765
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
1802
|
28 #include <cstdio> |
|
29 #include <cstdlib> |
1765
|
30 #include <cstring> |
|
31 |
3040
|
32 #include <iostream.h> |
|
33 |
2443
|
34 #ifdef HAVE_SYS_TYPES_H |
1765
|
35 #include <sys/types.h> |
2443
|
36 #endif |
2926
|
37 |
|
38 #ifdef HAVE_UNISTD_H |
1765
|
39 #include <unistd.h> |
|
40 #endif |
|
41 |
|
42 #include "file-ops.h" |
2926
|
43 #include "oct-env.h" |
2934
|
44 #include "oct-passwd.h" |
1775
|
45 #include "statdefs.h" |
2926
|
46 #include "str-vec.h" |
1765
|
47 |
2937
|
48 #define NOT_SUPPORTED(nm) \ |
|
49 nm ## ": not supported on this system" |
|
50 |
2433
|
51 // We provide a replacement for mkdir(). |
|
52 |
1765
|
53 int |
2926
|
54 file_ops::mkdir (const string& name, mode_t mode) |
1765
|
55 { |
2937
|
56 string msg; |
|
57 return mkdir (name, mode, msg); |
1765
|
58 } |
|
59 |
2668
|
60 int |
2926
|
61 file_ops::mkdir (const string& name, mode_t mode, string& msg) |
2668
|
62 { |
|
63 msg = string (); |
|
64 |
2937
|
65 int status = -1; |
|
66 |
|
67 #if defined (HAVE_MKDIR) |
|
68 status = ::mkdir (name.c_str (), mode); |
2668
|
69 |
|
70 if (status < 0) |
2926
|
71 msg = ::strerror (errno); |
2937
|
72 #else |
|
73 msg = NOT_SUPPORTED ("mkdir"); |
|
74 #endif |
2668
|
75 |
|
76 return status; |
|
77 } |
|
78 |
2433
|
79 // I don't know how to emulate this on systems that don't provide it. |
|
80 |
1765
|
81 int |
2926
|
82 file_ops::mkfifo (const string& name, mode_t mode) |
1765
|
83 { |
2937
|
84 string msg; |
|
85 return mkfifo (name, mode, msg); |
1765
|
86 } |
|
87 |
2668
|
88 int |
2926
|
89 file_ops::mkfifo (const string& name, mode_t mode, string& msg) |
2668
|
90 { |
|
91 msg = string (); |
|
92 |
2937
|
93 int status = -1; |
|
94 |
2668
|
95 #if defined (HAVE_MKFIFO) |
2937
|
96 status = ::mkfifo (name.c_str (), mode); |
2668
|
97 |
|
98 if (status < 0) |
2926
|
99 msg = ::strerror (errno); |
2937
|
100 #else |
|
101 msg = NOT_SUPPORTED ("mkfifo"); |
|
102 #endif |
2668
|
103 |
|
104 return status; |
|
105 } |
|
106 |
2433
|
107 // We provide a replacement for rename(). |
|
108 |
1765
|
109 int |
2926
|
110 file_ops::rename (const string& from, const string& to) |
1765
|
111 { |
2937
|
112 string msg; |
|
113 return rename (from, to, msg); |
1765
|
114 } |
|
115 |
2668
|
116 int |
2926
|
117 file_ops::rename (const string& from, const string& to, string& msg) |
2668
|
118 { |
2937
|
119 int status = -1; |
|
120 |
2668
|
121 msg = string (); |
|
122 |
2937
|
123 #if defined (HAVE_RENAME) |
|
124 status = ::rename (from.c_str (), to.c_str ()); |
2668
|
125 |
|
126 if (status < 0) |
2926
|
127 msg = ::strerror (errno); |
2937
|
128 #else |
|
129 msg = NOT_SUPPORTED ("rename"); |
|
130 #endif |
2668
|
131 |
|
132 return status; |
|
133 } |
|
134 |
2433
|
135 // We provide a replacement for rmdir(). |
|
136 |
1765
|
137 int |
2926
|
138 file_ops::rmdir (const string& name) |
1765
|
139 { |
2937
|
140 string msg; |
|
141 return rmdir (name, msg); |
1765
|
142 } |
|
143 |
2668
|
144 int |
2926
|
145 file_ops::rmdir (const string& name, string& msg) |
2668
|
146 { |
|
147 msg = string (); |
|
148 |
2937
|
149 int status = -1; |
|
150 |
|
151 #if defined (HAVE_RMDIR) |
|
152 status = ::rmdir (name.c_str ()); |
2668
|
153 |
|
154 if (status < 0) |
2926
|
155 msg = ::strerror (errno); |
2937
|
156 #else |
|
157 msg = NOT_SUPPORTED ("rmdir"); |
|
158 #endif |
2668
|
159 |
|
160 return status; |
|
161 } |
|
162 |
2433
|
163 // We provide a replacement for tempnam(). |
|
164 |
1802
|
165 string |
2937
|
166 file_ops::tempnam (const string& dir, const string& pfx) |
1802
|
167 { |
2937
|
168 string msg; |
|
169 return tempnam (dir, pfx, msg); |
|
170 } |
|
171 |
|
172 string |
|
173 file_ops::tempnam (const string& dir, const string& pfx, string& msg) |
|
174 { |
|
175 msg = string (); |
|
176 |
1802
|
177 string retval; |
2937
|
178 |
|
179 const char *pdir = dir.empty () ? 0 : dir.c_str (); |
1802
|
180 |
2937
|
181 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
|
182 |
|
183 char *tmp = ::tempnam (pdir, ppfx); |
1802
|
184 |
|
185 if (tmp) |
|
186 { |
|
187 retval = tmp; |
|
188 |
2937
|
189 ::free (tmp); |
1802
|
190 } |
|
191 else |
2937
|
192 msg = ::strerror (errno); |
1802
|
193 |
|
194 return retval; |
|
195 } |
|
196 |
3040
|
197 // The following tilde-expansion code was stolen and adapted from |
|
198 // readline. |
2926
|
199 |
|
200 // XXX FIXME XXX |
|
201 #define DIR_SEP_CHAR '/' |
|
202 |
3040
|
203 // The default value of tilde_additional_prefixes. This is set to |
|
204 // whitespace preceding a tilde so that simple programs which do not |
|
205 // perform any word separation get desired behaviour. |
|
206 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 }; |
|
207 |
|
208 // The default value of tilde_additional_suffixes. This is set to |
|
209 // whitespace or newline so that simple programs which do not perform |
|
210 // any word separation get desired behaviour. |
|
211 static const char *default_suffixes[] = { " ", "\n", ":", 0 }; |
|
212 |
|
213 // If non-null, this contains the address of a function that the |
|
214 // application wants called before trying the standard tilde |
|
215 // expansions. The function is called with the text sans tilde, and |
|
216 // returns a malloc()'ed string which is the expansion, or a NULL |
|
217 // pointer if the expansion fails. |
|
218 file_ops::tilde_expansion_hook file_ops::tilde_expansion_preexpansion_hook = 0; |
|
219 |
|
220 // If non-null, this contains the address of a function to call if the |
|
221 // standard meaning for expanding a tilde fails. The function is |
|
222 // called with the text (sans tilde, as in "foo"), and returns a |
|
223 // malloc()'ed string which is the expansion, or a NULL pointer if |
|
224 // there is no expansion. |
|
225 file_ops::tilde_expansion_hook file_ops::tilde_expansion_failure_hook = 0; |
|
226 |
|
227 // When non-null, this is a NULL terminated array of strings which are |
|
228 // duplicates for a tilde prefix. Bash uses this to expand `=~' and |
|
229 // `:~'. |
|
230 string_vector file_ops::tilde_additional_prefixes = default_prefixes; |
|
231 |
|
232 // When non-null, this is a NULL terminated array of strings which |
|
233 // match the end of a username, instead of just "/". Bash sets this |
|
234 // to `:' and `=~'. |
|
235 string_vector file_ops::tilde_additional_suffixes = default_suffixes; |
|
236 |
|
237 // Find the start of a tilde expansion in S, and return the index |
|
238 // of the tilde which starts the expansion. Place the length of the |
|
239 // text which identified this tilde starter in LEN, excluding the |
|
240 // tilde itself. |
|
241 |
|
242 static size_t |
|
243 tilde_find_prefix (const string& s, size_t& len) |
|
244 { |
|
245 len = 0; |
|
246 |
|
247 size_t s_len = s.length (); |
|
248 |
|
249 if (s_len == 0 || s[0] == '~') |
|
250 return 0; |
|
251 |
|
252 string_vector prefixes = file_ops::tilde_additional_prefixes; |
|
253 |
|
254 if (! prefixes.empty ()) |
|
255 { |
|
256 for (size_t i = 0; i < s_len; i++) |
|
257 { |
|
258 for (int j = 0; j < prefixes.length (); j++) |
|
259 { |
|
260 size_t pfx_len = prefixes[j].length (); |
|
261 |
|
262 if (prefixes[j].compare (s.substr (i, pfx_len)) == 0) |
|
263 { |
|
264 len = pfx_len - 1; |
|
265 return i + len; |
|
266 } |
|
267 } |
|
268 } |
|
269 } |
|
270 |
|
271 return s_len; |
|
272 } |
|
273 |
|
274 // Find the end of a tilde expansion in S, and return the index |
|
275 // of the character which ends the tilde definition. |
|
276 |
|
277 static size_t |
|
278 tilde_find_suffix (const string& s) |
|
279 { |
|
280 size_t s_len = s.length (); |
|
281 |
|
282 string_vector suffixes = file_ops::tilde_additional_suffixes; |
|
283 |
|
284 size_t i = 0; |
|
285 |
|
286 for ( ; i < s_len; i++) |
|
287 { |
|
288 if (s[i] == DIR_SEP_CHAR) |
|
289 break; |
|
290 |
|
291 if (! suffixes.empty ()) |
|
292 { |
|
293 for (int j = 0; j < suffixes.length (); j++) |
|
294 { |
|
295 size_t sfx_len = suffixes[j].length (); |
|
296 |
|
297 if (suffixes[j].compare (s.substr (i, sfx_len)) == 0) |
|
298 return i; |
|
299 } |
|
300 } |
|
301 } |
|
302 |
|
303 return i; |
|
304 } |
|
305 |
|
306 // Take FNAME and return the tilde prefix we want expanded. |
|
307 |
|
308 static string |
|
309 isolate_tilde_prefix (const string& fname) |
|
310 { |
|
311 size_t f_len = fname.length (); |
|
312 |
|
313 size_t len = 1; |
|
314 |
|
315 while (len < f_len && fname[len] != DIR_SEP_CHAR) |
|
316 len++; |
|
317 |
|
318 return fname.substr (1, len); |
|
319 } |
|
320 |
|
321 // Do the work of tilde expansion on FILENAME. FILENAME starts with a |
|
322 // tilde. |
|
323 |
|
324 static string |
|
325 tilde_expand_word (const string& filename) |
|
326 { |
|
327 size_t f_len = filename.length (); |
|
328 |
|
329 if (f_len == 0 || filename[0] != '~') |
|
330 return filename; |
|
331 |
|
332 // A leading `~/' or a bare `~' is *always* translated to the value |
|
333 // of $HOME or the home directory of the current user, regardless of |
|
334 // any preexpansion hook. |
|
335 |
|
336 if (f_len == 1 || filename[1] == DIR_SEP_CHAR) |
|
337 return octave_env::get_home_directory () + filename.substr (1); |
|
338 |
|
339 string username = isolate_tilde_prefix (filename); |
|
340 |
|
341 size_t user_len = username.length (); |
|
342 |
|
343 string dirname; |
|
344 |
|
345 if (file_ops::tilde_expansion_preexpansion_hook) |
|
346 { |
|
347 string expansion |
|
348 = file_ops::tilde_expansion_preexpansion_hook (username); |
|
349 |
|
350 if (! expansion.empty ()) |
3074
|
351 return expansion + filename.substr (user_len+1); |
3040
|
352 } |
|
353 |
|
354 // No preexpansion hook, or the preexpansion hook failed. Look in the |
|
355 // password database. |
|
356 |
|
357 octave_passwd pw = octave_passwd::getpwnam (username); |
|
358 |
|
359 if (! pw) |
|
360 { |
|
361 // If the calling program has a special syntax for expanding tildes, |
|
362 // and we couldn't find a standard expansion, then let them try. |
|
363 |
|
364 if (file_ops::tilde_expansion_failure_hook) |
|
365 { |
|
366 string expansion |
|
367 = file_ops::tilde_expansion_failure_hook (username); |
|
368 |
|
369 if (! expansion.empty ()) |
3074
|
370 dirname = expansion + filename.substr (user_len+1); |
3040
|
371 } |
|
372 |
|
373 // If we don't have a failure hook, or if the failure hook did not |
|
374 // expand the tilde, return a copy of what we were passed. |
|
375 |
|
376 if (dirname.length () == 0) |
|
377 dirname = filename; |
|
378 } |
|
379 else |
3074
|
380 dirname = pw.dir () + filename.substr (user_len+1); |
3040
|
381 |
|
382 return dirname; |
|
383 } |
|
384 |
|
385 // If NAME has a leading ~ or ~user, Unix-style, expand it to the |
|
386 // user's home directory. If no ~, or no <pwd.h>, just return NAME. |
|
387 |
2926
|
388 string |
|
389 file_ops::tilde_expand (const string& name) |
|
390 { |
3040
|
391 string result; |
2926
|
392 |
3040
|
393 size_t name_len = name.length (); |
2926
|
394 |
3040
|
395 // Scan through S expanding tildes as we come to them. |
2926
|
396 |
3040
|
397 size_t pos = 0; |
2926
|
398 |
3040
|
399 while (1) |
|
400 { |
|
401 if (pos > name_len) |
|
402 break; |
2926
|
403 |
3040
|
404 size_t len; |
|
405 |
|
406 // Make START point to the tilde which starts the expansion. |
2926
|
407 |
3040
|
408 size_t start = tilde_find_prefix (name.substr (pos), len); |
|
409 |
|
410 result.append (name.substr (pos, start)); |
2926
|
411 |
3040
|
412 // Advance STRING to the starting tilde. |
|
413 |
|
414 pos += start; |
2926
|
415 |
3040
|
416 // Make FINI be the index of one after the last character of the |
|
417 // username. |
2926
|
418 |
3040
|
419 size_t fini = tilde_find_suffix (name.substr (pos)); |
2926
|
420 |
3040
|
421 // If both START and FINI are zero, we are all done. |
2926
|
422 |
3040
|
423 if (! (start || fini)) |
|
424 break; |
2926
|
425 |
3040
|
426 // Expand the entire tilde word, and copy it into RESULT. |
2947
|
427 |
3040
|
428 string tilde_word = name.substr (pos, fini); |
|
429 |
|
430 pos += fini; |
2926
|
431 |
3040
|
432 string expansion = tilde_expand_word (tilde_word); |
|
433 |
|
434 result.append (expansion); |
2926
|
435 } |
|
436 |
3040
|
437 return result; |
2926
|
438 } |
|
439 |
|
440 // A vector version of the above. |
|
441 |
|
442 string_vector |
|
443 file_ops::tilde_expand (const string_vector& names) |
|
444 { |
|
445 string_vector retval; |
|
446 |
|
447 int n = names.length (); |
|
448 |
|
449 retval.resize (n); |
|
450 |
|
451 for (int i = 0; i < n; i++) |
|
452 retval[i] = file_ops::tilde_expand (names[i]); |
|
453 |
|
454 return retval; |
|
455 } |
1802
|
456 |
1765
|
457 int |
2926
|
458 file_ops::umask (mode_t mode) |
1765
|
459 { |
|
460 #if defined (HAVE_UMASK) |
2926
|
461 return ::umask (mode); |
1765
|
462 #else |
|
463 return 0; |
|
464 #endif |
|
465 } |
|
466 |
1773
|
467 int |
2926
|
468 file_ops::unlink (const string& name) |
1773
|
469 { |
2937
|
470 string msg; |
|
471 return unlink (name, msg); |
1773
|
472 } |
|
473 |
2668
|
474 int |
2937
|
475 file_ops::unlink (const string& name, string& msg) |
2668
|
476 { |
2937
|
477 msg = string (); |
2668
|
478 |
2937
|
479 int status = -1; |
|
480 |
|
481 #if defined (HAVE_UNLINK) |
|
482 status = ::unlink (name.c_str ()); |
2668
|
483 |
|
484 if (status < 0) |
2937
|
485 msg = ::strerror (errno); |
|
486 #else |
|
487 msg = NOT_SUPPORTED ("unlink"); |
|
488 #endif |
2668
|
489 |
|
490 return status; |
|
491 } |
|
492 |
1765
|
493 /* |
|
494 ;;; Local Variables: *** |
|
495 ;;; mode: C++ *** |
|
496 ;;; End: *** |
|
497 */ |