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