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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
1765
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
1802
|
29 #include <cstdio> |
|
30 #include <cstdlib> |
1765
|
31 #include <cstring> |
|
32 |
3503
|
33 #include <iostream> |
4726
|
34 #include <vector> |
3040
|
35 |
2443
|
36 #ifdef HAVE_SYS_TYPES_H |
1765
|
37 #include <sys/types.h> |
2443
|
38 #endif |
2926
|
39 |
|
40 #ifdef HAVE_UNISTD_H |
1765
|
41 #include <unistd.h> |
|
42 #endif |
|
43 |
5476
|
44 #include "dir-ops.h" |
1765
|
45 #include "file-ops.h" |
5476
|
46 #include "file-stat.h" |
2926
|
47 #include "oct-env.h" |
2934
|
48 #include "oct-passwd.h" |
3710
|
49 #include "pathlen.h" |
5476
|
50 #include "quit.h" |
1775
|
51 #include "statdefs.h" |
2926
|
52 #include "str-vec.h" |
1765
|
53 |
2937
|
54 #define NOT_SUPPORTED(nm) \ |
4062
|
55 nm ": not supported on this system" |
2937
|
56 |
4101
|
57 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \ |
|
58 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)) |
|
59 char file_ops::dir_sep_char = '\\'; |
|
60 std::string file_ops::dir_sep_str ("\\"); |
|
61 #else |
|
62 char file_ops::dir_sep_char = '/'; |
|
63 std::string file_ops::dir_sep_str ("/"); |
|
64 #endif |
|
65 |
|
66 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \ |
|
67 && defined (OCTAVE_HAVE_POSIX_FILESYSTEM)) |
|
68 std::string file_ops::dir_sep_chars ("/\\"); |
|
69 #else |
|
70 std::string file_ops::dir_sep_chars (file_ops::dir_sep_str); |
|
71 #endif |
|
72 |
2433
|
73 // We provide a replacement for mkdir(). |
|
74 |
1765
|
75 int |
3504
|
76 file_ops::mkdir (const std::string& name, mode_t mode) |
1765
|
77 { |
3504
|
78 std::string msg; |
2937
|
79 return mkdir (name, mode, msg); |
1765
|
80 } |
|
81 |
2668
|
82 int |
3504
|
83 file_ops::mkdir (const std::string& name, mode_t mode, std::string& msg) |
2668
|
84 { |
3504
|
85 msg = std::string (); |
2668
|
86 |
2937
|
87 int status = -1; |
|
88 |
|
89 #if defined (HAVE_MKDIR) |
4081
|
90 |
|
91 #if defined (MKDIR_TAKES_ONE_ARG) |
|
92 status = ::mkdir (name.c_str ()); |
|
93 #else |
2937
|
94 status = ::mkdir (name.c_str (), mode); |
4081
|
95 #endif |
2668
|
96 |
|
97 if (status < 0) |
3504
|
98 { |
|
99 using namespace std; |
|
100 msg = ::strerror (errno); |
|
101 } |
2937
|
102 #else |
|
103 msg = NOT_SUPPORTED ("mkdir"); |
|
104 #endif |
2668
|
105 |
|
106 return status; |
|
107 } |
|
108 |
2433
|
109 // I don't know how to emulate this on systems that don't provide it. |
|
110 |
1765
|
111 int |
3504
|
112 file_ops::mkfifo (const std::string& name, mode_t mode) |
1765
|
113 { |
3504
|
114 std::string msg; |
2937
|
115 return mkfifo (name, mode, msg); |
1765
|
116 } |
|
117 |
2668
|
118 int |
3504
|
119 file_ops::mkfifo (const std::string& name, mode_t mode, std::string& msg) |
2668
|
120 { |
3504
|
121 msg = std::string (); |
2668
|
122 |
2937
|
123 int status = -1; |
|
124 |
2668
|
125 #if defined (HAVE_MKFIFO) |
2937
|
126 status = ::mkfifo (name.c_str (), mode); |
2668
|
127 |
|
128 if (status < 0) |
3504
|
129 { |
|
130 using namespace std; |
|
131 msg = ::strerror (errno); |
|
132 } |
2937
|
133 #else |
|
134 msg = NOT_SUPPORTED ("mkfifo"); |
|
135 #endif |
2668
|
136 |
|
137 return status; |
|
138 } |
|
139 |
3710
|
140 // I don't know how to emulate this on systems that don't provide it. |
|
141 |
|
142 int |
|
143 file_ops::link (const std::string& old_name, const std::string& new_name) |
|
144 { |
|
145 std::string msg; |
|
146 return link (old_name, new_name, msg); |
|
147 } |
|
148 |
|
149 int |
|
150 file_ops::link (const std::string& old_name, |
|
151 const std::string& new_name, std::string& msg) |
|
152 { |
|
153 msg = std::string (); |
|
154 |
|
155 int status = -1; |
|
156 |
|
157 #if defined (HAVE_LINK) |
|
158 status = ::link (old_name.c_str (), new_name.c_str ()); |
|
159 |
|
160 if (status < 0) |
|
161 { |
|
162 using namespace std; |
|
163 msg = ::strerror (errno); |
|
164 } |
|
165 #else |
|
166 msg = NOT_SUPPORTED ("link"); |
|
167 #endif |
|
168 |
|
169 return status; |
|
170 } |
|
171 |
|
172 // I don't know how to emulate this on systems that don't provide it. |
|
173 |
|
174 int |
|
175 file_ops::symlink (const std::string& old_name, const std::string& new_name) |
|
176 { |
|
177 std::string msg; |
|
178 return symlink (old_name, new_name, msg); |
|
179 } |
|
180 |
|
181 int |
|
182 file_ops::symlink (const std::string& old_name, |
|
183 const std::string& new_name, std::string& msg) |
|
184 { |
|
185 msg = std::string (); |
|
186 |
|
187 int status = -1; |
|
188 |
|
189 #if defined (HAVE_SYMLINK) |
4577
|
190 |
|
191 OCTAVE_LOCAL_BUFFER (char, old_nm, old_name.length ()); |
|
192 OCTAVE_LOCAL_BUFFER (char, new_nm, new_name.length ()); |
|
193 |
|
194 strcpy (old_nm, old_name.c_str ()); |
|
195 strcpy (new_nm, new_name.c_str ()); |
|
196 |
|
197 status = ::symlink (old_nm, new_nm); |
3710
|
198 |
|
199 if (status < 0) |
|
200 { |
|
201 using namespace std; |
|
202 msg = ::strerror (errno); |
|
203 } |
|
204 #else |
|
205 msg = NOT_SUPPORTED ("symlink"); |
|
206 #endif |
|
207 |
|
208 return status; |
|
209 } |
|
210 |
|
211 // We provide a replacement for rename(). |
|
212 |
|
213 int |
|
214 file_ops::readlink (const std::string& path, std::string& result) |
|
215 { |
|
216 std::string msg; |
|
217 return readlink (path, result, msg); |
|
218 } |
|
219 |
|
220 int |
|
221 file_ops::readlink (const std::string& path, std::string& result, |
|
222 std::string& msg) |
|
223 { |
|
224 int status = -1; |
|
225 |
|
226 msg = std::string (); |
|
227 |
4066
|
228 #if defined (HAVE_READLINK) |
3710
|
229 char buf[MAXPATHLEN+1]; |
|
230 |
4577
|
231 OCTAVE_LOCAL_BUFFER (char, p, path.length ()); |
|
232 |
|
233 strcpy (p, path.c_str ()); |
|
234 |
|
235 status = ::readlink (p, buf, MAXPATHLEN); |
3710
|
236 |
|
237 if (status < 0) |
|
238 { |
|
239 using namespace std; |
|
240 msg = ::strerror (errno); |
|
241 } |
|
242 else |
|
243 { |
|
244 buf[status] = '\0'; |
3769
|
245 result = std::string (buf); |
3710
|
246 status = 0; |
|
247 } |
|
248 #else |
|
249 msg = NOT_SUPPORTED ("rename"); |
|
250 #endif |
|
251 |
|
252 return status; |
|
253 } |
|
254 |
2433
|
255 // We provide a replacement for rename(). |
|
256 |
1765
|
257 int |
3504
|
258 file_ops::rename (const std::string& from, const std::string& to) |
1765
|
259 { |
3504
|
260 std::string msg; |
2937
|
261 return rename (from, to, msg); |
1765
|
262 } |
|
263 |
2668
|
264 int |
3504
|
265 file_ops::rename (const std::string& from, const std::string& to, |
|
266 std::string& msg) |
2668
|
267 { |
2937
|
268 int status = -1; |
|
269 |
3504
|
270 msg = std::string (); |
2668
|
271 |
2937
|
272 #if defined (HAVE_RENAME) |
|
273 status = ::rename (from.c_str (), to.c_str ()); |
2668
|
274 |
|
275 if (status < 0) |
3504
|
276 { |
|
277 using namespace std; |
|
278 msg = ::strerror (errno); |
|
279 } |
2937
|
280 #else |
|
281 msg = NOT_SUPPORTED ("rename"); |
|
282 #endif |
2668
|
283 |
|
284 return status; |
|
285 } |
|
286 |
2433
|
287 // We provide a replacement for rmdir(). |
|
288 |
1765
|
289 int |
3504
|
290 file_ops::rmdir (const std::string& name) |
1765
|
291 { |
3504
|
292 std::string msg; |
2937
|
293 return rmdir (name, msg); |
1765
|
294 } |
|
295 |
2668
|
296 int |
3504
|
297 file_ops::rmdir (const std::string& name, std::string& msg) |
2668
|
298 { |
3504
|
299 msg = std::string (); |
2668
|
300 |
2937
|
301 int status = -1; |
|
302 |
|
303 #if defined (HAVE_RMDIR) |
|
304 status = ::rmdir (name.c_str ()); |
2668
|
305 |
|
306 if (status < 0) |
3504
|
307 { |
|
308 using namespace std; |
|
309 msg = ::strerror (errno); |
|
310 } |
2937
|
311 #else |
|
312 msg = NOT_SUPPORTED ("rmdir"); |
|
313 #endif |
2668
|
314 |
|
315 return status; |
|
316 } |
|
317 |
5476
|
318 // And a version that works recursively. |
|
319 |
|
320 int |
|
321 file_ops::recursive_rmdir (const std::string& name) |
|
322 { |
|
323 std::string msg; |
|
324 return recursive_rmdir (name, msg); |
|
325 } |
|
326 |
|
327 int |
|
328 file_ops::recursive_rmdir (const std::string& name, std::string& msg) |
|
329 { |
|
330 msg = std::string (); |
|
331 |
|
332 int status = 0; |
|
333 |
|
334 dir_entry dir (name); |
|
335 |
|
336 if (dir) |
|
337 { |
|
338 string_vector dirlist = dir.read (); |
|
339 |
|
340 for (octave_idx_type i = 0; i < dirlist.length (); i++) |
|
341 { |
|
342 OCTAVE_QUIT; |
|
343 |
|
344 std::string nm = dirlist[i]; |
|
345 |
|
346 // Skip current directory and parent. |
|
347 if (nm == "." || nm == "..") |
|
348 continue; |
|
349 |
|
350 std::string fullnm = name + file_ops::dir_sep_str + nm; |
|
351 |
|
352 // Get info about the file. Don't follow links. |
|
353 file_stat fs (fullnm, false); |
|
354 |
|
355 if (fs) |
|
356 { |
|
357 if (fs.is_dir ()) |
|
358 { |
|
359 status = recursive_rmdir (fullnm, msg); |
|
360 |
|
361 if (status < 0) |
|
362 break; |
|
363 } |
|
364 else |
|
365 { |
|
366 status = unlink (fullnm, msg); |
|
367 |
|
368 if (status < 0) |
|
369 break; |
|
370 } |
|
371 } |
|
372 else |
|
373 { |
|
374 msg = fs.error (); |
|
375 break; |
|
376 } |
|
377 } |
|
378 |
|
379 if (status >= 0) |
6363
|
380 { |
|
381 dir.close (); |
|
382 status = file_ops::rmdir (name, msg); |
|
383 } |
5476
|
384 } |
|
385 else |
|
386 { |
|
387 status = -1; |
|
388 |
|
389 msg = dir.error (); |
|
390 } |
|
391 |
|
392 return status; |
|
393 } |
|
394 |
5138
|
395 std::string |
|
396 file_ops::canonicalize_file_name (const std::string& name) |
|
397 { |
|
398 std::string msg; |
|
399 return canonicalize_file_name (name, msg); |
|
400 } |
|
401 |
|
402 std::string |
|
403 file_ops::canonicalize_file_name (const std::string& name, std::string& msg) |
|
404 { |
|
405 msg = std::string (); |
|
406 |
|
407 std::string retval; |
|
408 |
|
409 #if defined (HAVE_CANONICALIZE_FILE_NAME) |
|
410 |
|
411 char *tmp = ::canonicalize_file_name (name.c_str ()); |
|
412 |
|
413 if (tmp) |
|
414 { |
|
415 retval = tmp; |
|
416 ::free (tmp); |
|
417 } |
|
418 |
|
419 #elif defined (HAVE_RESOLVEPATH) |
|
420 |
|
421 #if !defined (errno) |
|
422 extern int errno; |
|
423 #endif |
|
424 |
|
425 #if !defined (__set_errno) |
|
426 # define __set_errno(Val) errno = (Val) |
|
427 #endif |
|
428 |
|
429 if (name.empty ()) |
|
430 { |
|
431 __set_errno (ENOENT); |
|
432 return retval; |
|
433 } |
|
434 |
|
435 // All known hosts with resolvepath (e.g. Solaris 7) don't turn |
|
436 // relative names into absolute ones, so prepend the working |
|
437 // directory if the path is not absolute. |
|
438 |
5149
|
439 std::string absolute_name |
|
440 = octave_env::make_absolute (name, octave_env::getcwd ()); |
5138
|
441 |
5149
|
442 size_t resolved_size = absolute_name.length (); |
5138
|
443 |
6208
|
444 while (true) |
5138
|
445 { |
|
446 resolved_size = 2 * resolved_size + 1; |
|
447 |
|
448 OCTAVE_LOCAL_BUFFER (char, resolved, resolved_size); |
|
449 |
5149
|
450 int resolved_len |
|
451 = ::resolvepath (absolute_name.c_str (), resolved, resolved_size); |
5138
|
452 |
|
453 if (resolved_len < 0) |
|
454 break; |
|
455 |
|
456 if (resolved_len < resolved_size) |
|
457 { |
|
458 retval = resolved; |
|
459 break; |
|
460 } |
|
461 } |
|
462 |
6208
|
463 #elif defined (__WIN32__) |
|
464 |
|
465 int n = 1024; |
|
466 |
|
467 std::string win_path (n, '\0'); |
|
468 |
|
469 while (true) |
|
470 { |
|
471 int status = GetFullPathName (name.c_str (), n, &win_path[0], NULL); |
|
472 |
|
473 if (status == 0) |
|
474 break; |
|
475 else if (status < n) |
|
476 { |
|
477 win_path.resize (status); |
|
478 retval = win_path; |
|
479 break; |
|
480 } |
|
481 else |
|
482 { |
|
483 n *= 2; |
|
484 win_path.resize (n); |
|
485 } |
|
486 } |
|
487 |
6271
|
488 #elif defined (HAVE_REALPATH) |
|
489 |
|
490 #if !defined (__set_errno) |
|
491 # define __set_errno(Val) errno = (Val) |
|
492 #endif |
|
493 |
|
494 if (name.empty ()) |
|
495 { |
|
496 __set_errno (ENOENT); |
|
497 return retval; |
|
498 } |
|
499 |
|
500 OCTAVE_LOCAL_BUFFER (char, buf, PATH_MAX); |
|
501 |
6273
|
502 if (::realpath (name.c_str (), buf)) |
|
503 retval = buf; |
6271
|
504 |
5138
|
505 #else |
|
506 |
5775
|
507 // FIXME -- provide replacement here... |
5138
|
508 retval = name; |
|
509 |
|
510 #endif |
|
511 |
|
512 if (retval.empty ()) |
|
513 { |
|
514 using namespace std; |
|
515 msg = ::strerror (errno); |
|
516 } |
|
517 |
|
518 return retval; |
|
519 } |
|
520 |
2433
|
521 // We provide a replacement for tempnam(). |
|
522 |
3504
|
523 std::string |
|
524 file_ops::tempnam (const std::string& dir, const std::string& pfx) |
1802
|
525 { |
3504
|
526 std::string msg; |
2937
|
527 return tempnam (dir, pfx, msg); |
|
528 } |
|
529 |
3504
|
530 std::string |
|
531 file_ops::tempnam (const std::string& dir, const std::string& pfx, |
|
532 std::string& msg) |
2937
|
533 { |
3504
|
534 msg = std::string (); |
2937
|
535 |
3504
|
536 std::string retval; |
2937
|
537 |
|
538 const char *pdir = dir.empty () ? 0 : dir.c_str (); |
1802
|
539 |
2937
|
540 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
|
541 |
|
542 char *tmp = ::tempnam (pdir, ppfx); |
1802
|
543 |
|
544 if (tmp) |
|
545 { |
|
546 retval = tmp; |
|
547 |
2937
|
548 ::free (tmp); |
1802
|
549 } |
|
550 else |
3504
|
551 { |
|
552 using namespace std; |
|
553 msg = ::strerror (errno); |
|
554 } |
1802
|
555 |
|
556 return retval; |
|
557 } |
|
558 |
3040
|
559 // The following tilde-expansion code was stolen and adapted from |
|
560 // readline. |
2926
|
561 |
3040
|
562 // The default value of tilde_additional_prefixes. This is set to |
|
563 // whitespace preceding a tilde so that simple programs which do not |
|
564 // perform any word separation get desired behaviour. |
|
565 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 }; |
|
566 |
|
567 // The default value of tilde_additional_suffixes. This is set to |
|
568 // whitespace or newline so that simple programs which do not perform |
|
569 // any word separation get desired behaviour. |
|
570 static const char *default_suffixes[] = { " ", "\n", ":", 0 }; |
|
571 |
|
572 // If non-null, this contains the address of a function that the |
|
573 // application wants called before trying the standard tilde |
|
574 // expansions. The function is called with the text sans tilde, and |
|
575 // returns a malloc()'ed string which is the expansion, or a NULL |
|
576 // pointer if the expansion fails. |
|
577 file_ops::tilde_expansion_hook file_ops::tilde_expansion_preexpansion_hook = 0; |
|
578 |
|
579 // If non-null, this contains the address of a function to call if the |
|
580 // standard meaning for expanding a tilde fails. The function is |
|
581 // called with the text (sans tilde, as in "foo"), and returns a |
|
582 // malloc()'ed string which is the expansion, or a NULL pointer if |
|
583 // there is no expansion. |
|
584 file_ops::tilde_expansion_hook file_ops::tilde_expansion_failure_hook = 0; |
|
585 |
|
586 // When non-null, this is a NULL terminated array of strings which are |
|
587 // duplicates for a tilde prefix. Bash uses this to expand `=~' and |
|
588 // `:~'. |
|
589 string_vector file_ops::tilde_additional_prefixes = default_prefixes; |
|
590 |
|
591 // When non-null, this is a NULL terminated array of strings which |
|
592 // match the end of a username, instead of just "/". Bash sets this |
|
593 // to `:' and `=~'. |
|
594 string_vector file_ops::tilde_additional_suffixes = default_suffixes; |
|
595 |
|
596 // Find the start of a tilde expansion in S, and return the index |
|
597 // of the tilde which starts the expansion. Place the length of the |
|
598 // text which identified this tilde starter in LEN, excluding the |
|
599 // tilde itself. |
|
600 |
|
601 static size_t |
3504
|
602 tilde_find_prefix (const std::string& s, size_t& len) |
3040
|
603 { |
|
604 len = 0; |
|
605 |
|
606 size_t s_len = s.length (); |
|
607 |
|
608 if (s_len == 0 || s[0] == '~') |
|
609 return 0; |
|
610 |
|
611 string_vector prefixes = file_ops::tilde_additional_prefixes; |
|
612 |
|
613 if (! prefixes.empty ()) |
|
614 { |
|
615 for (size_t i = 0; i < s_len; i++) |
|
616 { |
|
617 for (int j = 0; j < prefixes.length (); j++) |
|
618 { |
|
619 size_t pfx_len = prefixes[j].length (); |
|
620 |
|
621 if (prefixes[j].compare (s.substr (i, pfx_len)) == 0) |
|
622 { |
|
623 len = pfx_len - 1; |
|
624 return i + len; |
|
625 } |
|
626 } |
|
627 } |
|
628 } |
|
629 |
|
630 return s_len; |
|
631 } |
|
632 |
|
633 // Find the end of a tilde expansion in S, and return the index |
|
634 // of the character which ends the tilde definition. |
|
635 |
|
636 static size_t |
3504
|
637 tilde_find_suffix (const std::string& s) |
3040
|
638 { |
|
639 size_t s_len = s.length (); |
|
640 |
|
641 string_vector suffixes = file_ops::tilde_additional_suffixes; |
|
642 |
|
643 size_t i = 0; |
|
644 |
|
645 for ( ; i < s_len; i++) |
|
646 { |
4101
|
647 if (s[i] == file_ops::dir_sep_char) |
3040
|
648 break; |
|
649 |
|
650 if (! suffixes.empty ()) |
|
651 { |
|
652 for (int j = 0; j < suffixes.length (); j++) |
|
653 { |
|
654 size_t sfx_len = suffixes[j].length (); |
|
655 |
|
656 if (suffixes[j].compare (s.substr (i, sfx_len)) == 0) |
|
657 return i; |
|
658 } |
|
659 } |
|
660 } |
|
661 |
|
662 return i; |
|
663 } |
|
664 |
|
665 // Take FNAME and return the tilde prefix we want expanded. |
|
666 |
3504
|
667 static std::string |
|
668 isolate_tilde_prefix (const std::string& fname) |
3040
|
669 { |
|
670 size_t f_len = fname.length (); |
|
671 |
|
672 size_t len = 1; |
|
673 |
4101
|
674 while (len < f_len && fname[len] != file_ops::dir_sep_char) |
3040
|
675 len++; |
|
676 |
|
677 return fname.substr (1, len); |
|
678 } |
|
679 |
|
680 // Do the work of tilde expansion on FILENAME. FILENAME starts with a |
|
681 // tilde. |
|
682 |
3504
|
683 static std::string |
|
684 tilde_expand_word (const std::string& filename) |
3040
|
685 { |
|
686 size_t f_len = filename.length (); |
|
687 |
|
688 if (f_len == 0 || filename[0] != '~') |
|
689 return filename; |
|
690 |
|
691 // A leading `~/' or a bare `~' is *always* translated to the value |
|
692 // of $HOME or the home directory of the current user, regardless of |
|
693 // any preexpansion hook. |
|
694 |
4101
|
695 if (f_len == 1 || filename[1] == file_ops::dir_sep_char) |
3040
|
696 return octave_env::get_home_directory () + filename.substr (1); |
|
697 |
3504
|
698 std::string username = isolate_tilde_prefix (filename); |
3040
|
699 |
|
700 size_t user_len = username.length (); |
|
701 |
3504
|
702 std::string dirname; |
3040
|
703 |
|
704 if (file_ops::tilde_expansion_preexpansion_hook) |
|
705 { |
3504
|
706 std::string expansion |
3040
|
707 = file_ops::tilde_expansion_preexpansion_hook (username); |
|
708 |
|
709 if (! expansion.empty ()) |
3074
|
710 return expansion + filename.substr (user_len+1); |
3040
|
711 } |
|
712 |
|
713 // No preexpansion hook, or the preexpansion hook failed. Look in the |
|
714 // password database. |
|
715 |
|
716 octave_passwd pw = octave_passwd::getpwnam (username); |
|
717 |
|
718 if (! pw) |
|
719 { |
|
720 // If the calling program has a special syntax for expanding tildes, |
|
721 // and we couldn't find a standard expansion, then let them try. |
|
722 |
|
723 if (file_ops::tilde_expansion_failure_hook) |
|
724 { |
3504
|
725 std::string expansion |
3040
|
726 = file_ops::tilde_expansion_failure_hook (username); |
|
727 |
|
728 if (! expansion.empty ()) |
3074
|
729 dirname = expansion + filename.substr (user_len+1); |
3040
|
730 } |
|
731 |
|
732 // If we don't have a failure hook, or if the failure hook did not |
|
733 // expand the tilde, return a copy of what we were passed. |
|
734 |
|
735 if (dirname.length () == 0) |
|
736 dirname = filename; |
|
737 } |
|
738 else |
3074
|
739 dirname = pw.dir () + filename.substr (user_len+1); |
3040
|
740 |
|
741 return dirname; |
|
742 } |
|
743 |
|
744 // If NAME has a leading ~ or ~user, Unix-style, expand it to the |
|
745 // user's home directory. If no ~, or no <pwd.h>, just return NAME. |
|
746 |
3504
|
747 std::string |
|
748 file_ops::tilde_expand (const std::string& name) |
2926
|
749 { |
3504
|
750 std::string result; |
2926
|
751 |
3040
|
752 size_t name_len = name.length (); |
2926
|
753 |
3040
|
754 // Scan through S expanding tildes as we come to them. |
2926
|
755 |
3040
|
756 size_t pos = 0; |
2926
|
757 |
3040
|
758 while (1) |
|
759 { |
|
760 if (pos > name_len) |
|
761 break; |
2926
|
762 |
3040
|
763 size_t len; |
|
764 |
|
765 // Make START point to the tilde which starts the expansion. |
2926
|
766 |
3040
|
767 size_t start = tilde_find_prefix (name.substr (pos), len); |
|
768 |
|
769 result.append (name.substr (pos, start)); |
2926
|
770 |
3040
|
771 // Advance STRING to the starting tilde. |
|
772 |
|
773 pos += start; |
2926
|
774 |
3040
|
775 // Make FINI be the index of one after the last character of the |
|
776 // username. |
2926
|
777 |
3040
|
778 size_t fini = tilde_find_suffix (name.substr (pos)); |
2926
|
779 |
3040
|
780 // If both START and FINI are zero, we are all done. |
2926
|
781 |
3040
|
782 if (! (start || fini)) |
|
783 break; |
2926
|
784 |
3040
|
785 // Expand the entire tilde word, and copy it into RESULT. |
2947
|
786 |
3504
|
787 std::string tilde_word = name.substr (pos, fini); |
3040
|
788 |
|
789 pos += fini; |
2926
|
790 |
3504
|
791 std::string expansion = tilde_expand_word (tilde_word); |
3040
|
792 |
|
793 result.append (expansion); |
2926
|
794 } |
|
795 |
3040
|
796 return result; |
2926
|
797 } |
|
798 |
|
799 // A vector version of the above. |
|
800 |
|
801 string_vector |
|
802 file_ops::tilde_expand (const string_vector& names) |
|
803 { |
|
804 string_vector retval; |
|
805 |
|
806 int n = names.length (); |
|
807 |
|
808 retval.resize (n); |
|
809 |
|
810 for (int i = 0; i < n; i++) |
|
811 retval[i] = file_ops::tilde_expand (names[i]); |
|
812 |
|
813 return retval; |
|
814 } |
1802
|
815 |
1765
|
816 int |
2926
|
817 file_ops::umask (mode_t mode) |
1765
|
818 { |
|
819 #if defined (HAVE_UMASK) |
2926
|
820 return ::umask (mode); |
1765
|
821 #else |
|
822 return 0; |
|
823 #endif |
|
824 } |
|
825 |
1773
|
826 int |
3504
|
827 file_ops::unlink (const std::string& name) |
1773
|
828 { |
3504
|
829 std::string msg; |
2937
|
830 return unlink (name, msg); |
1773
|
831 } |
|
832 |
2668
|
833 int |
3504
|
834 file_ops::unlink (const std::string& name, std::string& msg) |
2668
|
835 { |
3504
|
836 msg = std::string (); |
2668
|
837 |
2937
|
838 int status = -1; |
|
839 |
|
840 #if defined (HAVE_UNLINK) |
|
841 status = ::unlink (name.c_str ()); |
2668
|
842 |
|
843 if (status < 0) |
3504
|
844 { |
|
845 using namespace std; |
|
846 msg = ::strerror (errno); |
|
847 } |
2937
|
848 #else |
|
849 msg = NOT_SUPPORTED ("unlink"); |
|
850 #endif |
2668
|
851 |
|
852 return status; |
|
853 } |
|
854 |
4097
|
855 bool |
|
856 file_ops::is_dir_sep (char c) |
|
857 { |
4101
|
858 return dir_sep_chars.find (c) != NPOS; |
4097
|
859 } |
|
860 |
1765
|
861 /* |
|
862 ;;; Local Variables: *** |
|
863 ;;; mode: C++ *** |
|
864 ;;; End: *** |
|
865 */ |