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