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) |
|
380 status = file_ops::rmdir (name, msg); |
|
381 } |
|
382 else |
|
383 { |
|
384 status = -1; |
|
385 |
|
386 msg = dir.error (); |
|
387 } |
|
388 |
|
389 return status; |
|
390 } |
|
391 |
5138
|
392 std::string |
|
393 file_ops::canonicalize_file_name (const std::string& name) |
|
394 { |
|
395 std::string msg; |
|
396 return canonicalize_file_name (name, msg); |
|
397 } |
|
398 |
|
399 std::string |
|
400 file_ops::canonicalize_file_name (const std::string& name, std::string& msg) |
|
401 { |
|
402 msg = std::string (); |
|
403 |
|
404 std::string retval; |
|
405 |
|
406 #if defined (HAVE_CANONICALIZE_FILE_NAME) |
|
407 |
|
408 char *tmp = ::canonicalize_file_name (name.c_str ()); |
|
409 |
|
410 if (tmp) |
|
411 { |
|
412 retval = tmp; |
|
413 ::free (tmp); |
|
414 } |
|
415 |
|
416 #elif defined (HAVE_RESOLVEPATH) |
|
417 |
|
418 #if !defined (errno) |
|
419 extern int errno; |
|
420 #endif |
|
421 |
|
422 #if !defined (__set_errno) |
|
423 # define __set_errno(Val) errno = (Val) |
|
424 #endif |
|
425 |
|
426 if (name.empty ()) |
|
427 { |
|
428 __set_errno (ENOENT); |
|
429 return retval; |
|
430 } |
|
431 |
|
432 // All known hosts with resolvepath (e.g. Solaris 7) don't turn |
|
433 // relative names into absolute ones, so prepend the working |
|
434 // directory if the path is not absolute. |
|
435 |
5149
|
436 std::string absolute_name |
|
437 = octave_env::make_absolute (name, octave_env::getcwd ()); |
5138
|
438 |
5149
|
439 size_t resolved_size = absolute_name.length (); |
5138
|
440 |
6208
|
441 while (true) |
5138
|
442 { |
|
443 resolved_size = 2 * resolved_size + 1; |
|
444 |
|
445 OCTAVE_LOCAL_BUFFER (char, resolved, resolved_size); |
|
446 |
5149
|
447 int resolved_len |
|
448 = ::resolvepath (absolute_name.c_str (), resolved, resolved_size); |
5138
|
449 |
|
450 if (resolved_len < 0) |
|
451 break; |
|
452 |
|
453 if (resolved_len < resolved_size) |
|
454 { |
|
455 retval = resolved; |
|
456 break; |
|
457 } |
|
458 } |
|
459 |
6208
|
460 #elif defined (__WIN32__) |
|
461 |
|
462 int n = 1024; |
|
463 |
|
464 std::string win_path (n, '\0'); |
|
465 |
|
466 while (true) |
|
467 { |
|
468 int status = GetFullPathName (name.c_str (), n, &win_path[0], NULL); |
|
469 |
|
470 if (status == 0) |
|
471 break; |
|
472 else if (status < n) |
|
473 { |
|
474 win_path.resize (status); |
|
475 retval = win_path; |
|
476 break; |
|
477 } |
|
478 else |
|
479 { |
|
480 n *= 2; |
|
481 win_path.resize (n); |
|
482 } |
|
483 } |
|
484 |
6271
|
485 #elif defined (HAVE_REALPATH) |
|
486 |
|
487 #if !defined (__set_errno) |
|
488 # define __set_errno(Val) errno = (Val) |
|
489 #endif |
|
490 |
|
491 if (name.empty ()) |
|
492 { |
|
493 __set_errno (ENOENT); |
|
494 return retval; |
|
495 } |
|
496 |
|
497 OCTAVE_LOCAL_BUFFER (char, buf, PATH_MAX); |
|
498 |
|
499 char *tmp = ::realpath (name.c_str (), buf); |
|
500 |
|
501 if (tmp) |
|
502 { |
|
503 retval = tmp; |
|
504 ::free (tmp); |
|
505 } |
|
506 |
5138
|
507 #else |
|
508 |
5775
|
509 // FIXME -- provide replacement here... |
5138
|
510 retval = name; |
|
511 |
|
512 #endif |
|
513 |
|
514 if (retval.empty ()) |
|
515 { |
|
516 using namespace std; |
|
517 msg = ::strerror (errno); |
|
518 } |
|
519 |
|
520 return retval; |
|
521 } |
|
522 |
2433
|
523 // We provide a replacement for tempnam(). |
|
524 |
3504
|
525 std::string |
|
526 file_ops::tempnam (const std::string& dir, const std::string& pfx) |
1802
|
527 { |
3504
|
528 std::string msg; |
2937
|
529 return tempnam (dir, pfx, msg); |
|
530 } |
|
531 |
3504
|
532 std::string |
|
533 file_ops::tempnam (const std::string& dir, const std::string& pfx, |
|
534 std::string& msg) |
2937
|
535 { |
3504
|
536 msg = std::string (); |
2937
|
537 |
3504
|
538 std::string retval; |
2937
|
539 |
|
540 const char *pdir = dir.empty () ? 0 : dir.c_str (); |
1802
|
541 |
2937
|
542 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
|
543 |
|
544 char *tmp = ::tempnam (pdir, ppfx); |
1802
|
545 |
|
546 if (tmp) |
|
547 { |
|
548 retval = tmp; |
|
549 |
2937
|
550 ::free (tmp); |
1802
|
551 } |
|
552 else |
3504
|
553 { |
|
554 using namespace std; |
|
555 msg = ::strerror (errno); |
|
556 } |
1802
|
557 |
|
558 return retval; |
|
559 } |
|
560 |
3040
|
561 // The following tilde-expansion code was stolen and adapted from |
|
562 // readline. |
2926
|
563 |
3040
|
564 // The default value of tilde_additional_prefixes. This is set to |
|
565 // whitespace preceding a tilde so that simple programs which do not |
|
566 // perform any word separation get desired behaviour. |
|
567 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 }; |
|
568 |
|
569 // The default value of tilde_additional_suffixes. This is set to |
|
570 // whitespace or newline so that simple programs which do not perform |
|
571 // any word separation get desired behaviour. |
|
572 static const char *default_suffixes[] = { " ", "\n", ":", 0 }; |
|
573 |
|
574 // If non-null, this contains the address of a function that the |
|
575 // application wants called before trying the standard tilde |
|
576 // expansions. The function is called with the text sans tilde, and |
|
577 // returns a malloc()'ed string which is the expansion, or a NULL |
|
578 // pointer if the expansion fails. |
|
579 file_ops::tilde_expansion_hook file_ops::tilde_expansion_preexpansion_hook = 0; |
|
580 |
|
581 // If non-null, this contains the address of a function to call if the |
|
582 // standard meaning for expanding a tilde fails. The function is |
|
583 // called with the text (sans tilde, as in "foo"), and returns a |
|
584 // malloc()'ed string which is the expansion, or a NULL pointer if |
|
585 // there is no expansion. |
|
586 file_ops::tilde_expansion_hook file_ops::tilde_expansion_failure_hook = 0; |
|
587 |
|
588 // When non-null, this is a NULL terminated array of strings which are |
|
589 // duplicates for a tilde prefix. Bash uses this to expand `=~' and |
|
590 // `:~'. |
|
591 string_vector file_ops::tilde_additional_prefixes = default_prefixes; |
|
592 |
|
593 // When non-null, this is a NULL terminated array of strings which |
|
594 // match the end of a username, instead of just "/". Bash sets this |
|
595 // to `:' and `=~'. |
|
596 string_vector file_ops::tilde_additional_suffixes = default_suffixes; |
|
597 |
|
598 // Find the start of a tilde expansion in S, and return the index |
|
599 // of the tilde which starts the expansion. Place the length of the |
|
600 // text which identified this tilde starter in LEN, excluding the |
|
601 // tilde itself. |
|
602 |
|
603 static size_t |
3504
|
604 tilde_find_prefix (const std::string& s, size_t& len) |
3040
|
605 { |
|
606 len = 0; |
|
607 |
|
608 size_t s_len = s.length (); |
|
609 |
|
610 if (s_len == 0 || s[0] == '~') |
|
611 return 0; |
|
612 |
|
613 string_vector prefixes = file_ops::tilde_additional_prefixes; |
|
614 |
|
615 if (! prefixes.empty ()) |
|
616 { |
|
617 for (size_t i = 0; i < s_len; i++) |
|
618 { |
|
619 for (int j = 0; j < prefixes.length (); j++) |
|
620 { |
|
621 size_t pfx_len = prefixes[j].length (); |
|
622 |
|
623 if (prefixes[j].compare (s.substr (i, pfx_len)) == 0) |
|
624 { |
|
625 len = pfx_len - 1; |
|
626 return i + len; |
|
627 } |
|
628 } |
|
629 } |
|
630 } |
|
631 |
|
632 return s_len; |
|
633 } |
|
634 |
|
635 // Find the end of a tilde expansion in S, and return the index |
|
636 // of the character which ends the tilde definition. |
|
637 |
|
638 static size_t |
3504
|
639 tilde_find_suffix (const std::string& s) |
3040
|
640 { |
|
641 size_t s_len = s.length (); |
|
642 |
|
643 string_vector suffixes = file_ops::tilde_additional_suffixes; |
|
644 |
|
645 size_t i = 0; |
|
646 |
|
647 for ( ; i < s_len; i++) |
|
648 { |
4101
|
649 if (s[i] == file_ops::dir_sep_char) |
3040
|
650 break; |
|
651 |
|
652 if (! suffixes.empty ()) |
|
653 { |
|
654 for (int j = 0; j < suffixes.length (); j++) |
|
655 { |
|
656 size_t sfx_len = suffixes[j].length (); |
|
657 |
|
658 if (suffixes[j].compare (s.substr (i, sfx_len)) == 0) |
|
659 return i; |
|
660 } |
|
661 } |
|
662 } |
|
663 |
|
664 return i; |
|
665 } |
|
666 |
|
667 // Take FNAME and return the tilde prefix we want expanded. |
|
668 |
3504
|
669 static std::string |
|
670 isolate_tilde_prefix (const std::string& fname) |
3040
|
671 { |
|
672 size_t f_len = fname.length (); |
|
673 |
|
674 size_t len = 1; |
|
675 |
4101
|
676 while (len < f_len && fname[len] != file_ops::dir_sep_char) |
3040
|
677 len++; |
|
678 |
|
679 return fname.substr (1, len); |
|
680 } |
|
681 |
|
682 // Do the work of tilde expansion on FILENAME. FILENAME starts with a |
|
683 // tilde. |
|
684 |
3504
|
685 static std::string |
|
686 tilde_expand_word (const std::string& filename) |
3040
|
687 { |
|
688 size_t f_len = filename.length (); |
|
689 |
|
690 if (f_len == 0 || filename[0] != '~') |
|
691 return filename; |
|
692 |
|
693 // A leading `~/' or a bare `~' is *always* translated to the value |
|
694 // of $HOME or the home directory of the current user, regardless of |
|
695 // any preexpansion hook. |
|
696 |
4101
|
697 if (f_len == 1 || filename[1] == file_ops::dir_sep_char) |
3040
|
698 return octave_env::get_home_directory () + filename.substr (1); |
|
699 |
3504
|
700 std::string username = isolate_tilde_prefix (filename); |
3040
|
701 |
|
702 size_t user_len = username.length (); |
|
703 |
3504
|
704 std::string dirname; |
3040
|
705 |
|
706 if (file_ops::tilde_expansion_preexpansion_hook) |
|
707 { |
3504
|
708 std::string expansion |
3040
|
709 = file_ops::tilde_expansion_preexpansion_hook (username); |
|
710 |
|
711 if (! expansion.empty ()) |
3074
|
712 return expansion + filename.substr (user_len+1); |
3040
|
713 } |
|
714 |
|
715 // No preexpansion hook, or the preexpansion hook failed. Look in the |
|
716 // password database. |
|
717 |
|
718 octave_passwd pw = octave_passwd::getpwnam (username); |
|
719 |
|
720 if (! pw) |
|
721 { |
|
722 // If the calling program has a special syntax for expanding tildes, |
|
723 // and we couldn't find a standard expansion, then let them try. |
|
724 |
|
725 if (file_ops::tilde_expansion_failure_hook) |
|
726 { |
3504
|
727 std::string expansion |
3040
|
728 = file_ops::tilde_expansion_failure_hook (username); |
|
729 |
|
730 if (! expansion.empty ()) |
3074
|
731 dirname = expansion + filename.substr (user_len+1); |
3040
|
732 } |
|
733 |
|
734 // If we don't have a failure hook, or if the failure hook did not |
|
735 // expand the tilde, return a copy of what we were passed. |
|
736 |
|
737 if (dirname.length () == 0) |
|
738 dirname = filename; |
|
739 } |
|
740 else |
3074
|
741 dirname = pw.dir () + filename.substr (user_len+1); |
3040
|
742 |
|
743 return dirname; |
|
744 } |
|
745 |
|
746 // If NAME has a leading ~ or ~user, Unix-style, expand it to the |
|
747 // user's home directory. If no ~, or no <pwd.h>, just return NAME. |
|
748 |
3504
|
749 std::string |
|
750 file_ops::tilde_expand (const std::string& name) |
2926
|
751 { |
3504
|
752 std::string result; |
2926
|
753 |
3040
|
754 size_t name_len = name.length (); |
2926
|
755 |
3040
|
756 // Scan through S expanding tildes as we come to them. |
2926
|
757 |
3040
|
758 size_t pos = 0; |
2926
|
759 |
3040
|
760 while (1) |
|
761 { |
|
762 if (pos > name_len) |
|
763 break; |
2926
|
764 |
3040
|
765 size_t len; |
|
766 |
|
767 // Make START point to the tilde which starts the expansion. |
2926
|
768 |
3040
|
769 size_t start = tilde_find_prefix (name.substr (pos), len); |
|
770 |
|
771 result.append (name.substr (pos, start)); |
2926
|
772 |
3040
|
773 // Advance STRING to the starting tilde. |
|
774 |
|
775 pos += start; |
2926
|
776 |
3040
|
777 // Make FINI be the index of one after the last character of the |
|
778 // username. |
2926
|
779 |
3040
|
780 size_t fini = tilde_find_suffix (name.substr (pos)); |
2926
|
781 |
3040
|
782 // If both START and FINI are zero, we are all done. |
2926
|
783 |
3040
|
784 if (! (start || fini)) |
|
785 break; |
2926
|
786 |
3040
|
787 // Expand the entire tilde word, and copy it into RESULT. |
2947
|
788 |
3504
|
789 std::string tilde_word = name.substr (pos, fini); |
3040
|
790 |
|
791 pos += fini; |
2926
|
792 |
3504
|
793 std::string expansion = tilde_expand_word (tilde_word); |
3040
|
794 |
|
795 result.append (expansion); |
2926
|
796 } |
|
797 |
3040
|
798 return result; |
2926
|
799 } |
|
800 |
|
801 // A vector version of the above. |
|
802 |
|
803 string_vector |
|
804 file_ops::tilde_expand (const string_vector& names) |
|
805 { |
|
806 string_vector retval; |
|
807 |
|
808 int n = names.length (); |
|
809 |
|
810 retval.resize (n); |
|
811 |
|
812 for (int i = 0; i < n; i++) |
|
813 retval[i] = file_ops::tilde_expand (names[i]); |
|
814 |
|
815 return retval; |
|
816 } |
1802
|
817 |
1765
|
818 int |
2926
|
819 file_ops::umask (mode_t mode) |
1765
|
820 { |
|
821 #if defined (HAVE_UMASK) |
2926
|
822 return ::umask (mode); |
1765
|
823 #else |
|
824 return 0; |
|
825 #endif |
|
826 } |
|
827 |
1773
|
828 int |
3504
|
829 file_ops::unlink (const std::string& name) |
1773
|
830 { |
3504
|
831 std::string msg; |
2937
|
832 return unlink (name, msg); |
1773
|
833 } |
|
834 |
2668
|
835 int |
3504
|
836 file_ops::unlink (const std::string& name, std::string& msg) |
2668
|
837 { |
3504
|
838 msg = std::string (); |
2668
|
839 |
2937
|
840 int status = -1; |
|
841 |
|
842 #if defined (HAVE_UNLINK) |
|
843 status = ::unlink (name.c_str ()); |
2668
|
844 |
|
845 if (status < 0) |
3504
|
846 { |
|
847 using namespace std; |
|
848 msg = ::strerror (errno); |
|
849 } |
2937
|
850 #else |
|
851 msg = NOT_SUPPORTED ("unlink"); |
|
852 #endif |
2668
|
853 |
|
854 return status; |
|
855 } |
|
856 |
4097
|
857 bool |
|
858 file_ops::is_dir_sep (char c) |
|
859 { |
4101
|
860 return dir_sep_chars.find (c) != NPOS; |
4097
|
861 } |
|
862 |
1765
|
863 /* |
|
864 ;;; Local Variables: *** |
|
865 ;;; mode: C++ *** |
|
866 ;;; End: *** |
|
867 */ |