1765
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1765
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
1802
|
28 #include <cstdio> |
|
29 #include <cstdlib> |
1765
|
30 #include <cstring> |
|
31 |
3503
|
32 #include <iostream> |
3040
|
33 |
2443
|
34 #ifdef HAVE_SYS_TYPES_H |
1765
|
35 #include <sys/types.h> |
2443
|
36 #endif |
2926
|
37 |
|
38 #ifdef HAVE_UNISTD_H |
1765
|
39 #include <unistd.h> |
|
40 #endif |
|
41 |
|
42 #include "file-ops.h" |
2926
|
43 #include "oct-env.h" |
2934
|
44 #include "oct-passwd.h" |
3710
|
45 #include "pathlen.h" |
1775
|
46 #include "statdefs.h" |
2926
|
47 #include "str-vec.h" |
1765
|
48 |
2937
|
49 #define NOT_SUPPORTED(nm) \ |
4062
|
50 nm ": not supported on this system" |
2937
|
51 |
4101
|
52 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \ |
|
53 && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM)) |
|
54 char file_ops::dir_sep_char = '\\'; |
|
55 std::string file_ops::dir_sep_str ("\\"); |
|
56 #else |
|
57 char file_ops::dir_sep_char = '/'; |
|
58 std::string file_ops::dir_sep_str ("/"); |
|
59 #endif |
|
60 |
|
61 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \ |
|
62 && defined (OCTAVE_HAVE_POSIX_FILESYSTEM)) |
|
63 std::string file_ops::dir_sep_chars ("/\\"); |
|
64 #else |
|
65 std::string file_ops::dir_sep_chars (file_ops::dir_sep_str); |
|
66 #endif |
|
67 |
2433
|
68 // We provide a replacement for mkdir(). |
|
69 |
1765
|
70 int |
3504
|
71 file_ops::mkdir (const std::string& name, mode_t mode) |
1765
|
72 { |
3504
|
73 std::string msg; |
2937
|
74 return mkdir (name, mode, msg); |
1765
|
75 } |
|
76 |
2668
|
77 int |
3504
|
78 file_ops::mkdir (const std::string& name, mode_t mode, std::string& msg) |
2668
|
79 { |
3504
|
80 msg = std::string (); |
2668
|
81 |
2937
|
82 int status = -1; |
|
83 |
|
84 #if defined (HAVE_MKDIR) |
4081
|
85 |
|
86 #if defined (MKDIR_TAKES_ONE_ARG) |
|
87 status = ::mkdir (name.c_str ()); |
|
88 #else |
2937
|
89 status = ::mkdir (name.c_str (), mode); |
4081
|
90 #endif |
2668
|
91 |
|
92 if (status < 0) |
3504
|
93 { |
|
94 using namespace std; |
|
95 msg = ::strerror (errno); |
|
96 } |
2937
|
97 #else |
|
98 msg = NOT_SUPPORTED ("mkdir"); |
|
99 #endif |
2668
|
100 |
|
101 return status; |
|
102 } |
|
103 |
2433
|
104 // I don't know how to emulate this on systems that don't provide it. |
|
105 |
1765
|
106 int |
3504
|
107 file_ops::mkfifo (const std::string& name, mode_t mode) |
1765
|
108 { |
3504
|
109 std::string msg; |
2937
|
110 return mkfifo (name, mode, msg); |
1765
|
111 } |
|
112 |
2668
|
113 int |
3504
|
114 file_ops::mkfifo (const std::string& name, mode_t mode, std::string& msg) |
2668
|
115 { |
3504
|
116 msg = std::string (); |
2668
|
117 |
2937
|
118 int status = -1; |
|
119 |
2668
|
120 #if defined (HAVE_MKFIFO) |
2937
|
121 status = ::mkfifo (name.c_str (), mode); |
2668
|
122 |
|
123 if (status < 0) |
3504
|
124 { |
|
125 using namespace std; |
|
126 msg = ::strerror (errno); |
|
127 } |
2937
|
128 #else |
|
129 msg = NOT_SUPPORTED ("mkfifo"); |
|
130 #endif |
2668
|
131 |
|
132 return status; |
|
133 } |
|
134 |
3710
|
135 // I don't know how to emulate this on systems that don't provide it. |
|
136 |
|
137 int |
|
138 file_ops::link (const std::string& old_name, const std::string& new_name) |
|
139 { |
|
140 std::string msg; |
|
141 return link (old_name, new_name, msg); |
|
142 } |
|
143 |
|
144 int |
|
145 file_ops::link (const std::string& old_name, |
|
146 const std::string& new_name, std::string& msg) |
|
147 { |
|
148 msg = std::string (); |
|
149 |
|
150 int status = -1; |
|
151 |
|
152 #if defined (HAVE_LINK) |
|
153 status = ::link (old_name.c_str (), new_name.c_str ()); |
|
154 |
|
155 if (status < 0) |
|
156 { |
|
157 using namespace std; |
|
158 msg = ::strerror (errno); |
|
159 } |
|
160 #else |
|
161 msg = NOT_SUPPORTED ("link"); |
|
162 #endif |
|
163 |
|
164 return status; |
|
165 } |
|
166 |
|
167 // I don't know how to emulate this on systems that don't provide it. |
|
168 |
|
169 int |
|
170 file_ops::symlink (const std::string& old_name, const std::string& new_name) |
|
171 { |
|
172 std::string msg; |
|
173 return symlink (old_name, new_name, msg); |
|
174 } |
|
175 |
|
176 int |
|
177 file_ops::symlink (const std::string& old_name, |
|
178 const std::string& new_name, std::string& msg) |
|
179 { |
|
180 msg = std::string (); |
|
181 |
|
182 int status = -1; |
|
183 |
|
184 #if defined (HAVE_SYMLINK) |
4577
|
185 |
|
186 OCTAVE_LOCAL_BUFFER (char, old_nm, old_name.length ()); |
|
187 OCTAVE_LOCAL_BUFFER (char, new_nm, new_name.length ()); |
|
188 |
|
189 strcpy (old_nm, old_name.c_str ()); |
|
190 strcpy (new_nm, new_name.c_str ()); |
|
191 |
|
192 status = ::symlink (old_nm, new_nm); |
3710
|
193 |
|
194 if (status < 0) |
|
195 { |
|
196 using namespace std; |
|
197 msg = ::strerror (errno); |
|
198 } |
|
199 #else |
|
200 msg = NOT_SUPPORTED ("symlink"); |
|
201 #endif |
|
202 |
|
203 return status; |
|
204 } |
|
205 |
|
206 // We provide a replacement for rename(). |
|
207 |
|
208 int |
|
209 file_ops::readlink (const std::string& path, std::string& result) |
|
210 { |
|
211 std::string msg; |
|
212 return readlink (path, result, msg); |
|
213 } |
|
214 |
|
215 int |
|
216 file_ops::readlink (const std::string& path, std::string& result, |
|
217 std::string& msg) |
|
218 { |
|
219 int status = -1; |
|
220 |
|
221 msg = std::string (); |
|
222 |
4066
|
223 #if defined (HAVE_READLINK) |
3710
|
224 char buf[MAXPATHLEN+1]; |
|
225 |
4577
|
226 OCTAVE_LOCAL_BUFFER (char, p, path.length ()); |
|
227 |
|
228 strcpy (p, path.c_str ()); |
|
229 |
|
230 status = ::readlink (p, buf, MAXPATHLEN); |
3710
|
231 |
|
232 if (status < 0) |
|
233 { |
|
234 using namespace std; |
|
235 msg = ::strerror (errno); |
|
236 } |
|
237 else |
|
238 { |
|
239 buf[status] = '\0'; |
3769
|
240 result = std::string (buf); |
3710
|
241 status = 0; |
|
242 } |
|
243 #else |
|
244 msg = NOT_SUPPORTED ("rename"); |
|
245 #endif |
|
246 |
|
247 return status; |
|
248 } |
|
249 |
2433
|
250 // We provide a replacement for rename(). |
|
251 |
1765
|
252 int |
3504
|
253 file_ops::rename (const std::string& from, const std::string& to) |
1765
|
254 { |
3504
|
255 std::string msg; |
2937
|
256 return rename (from, to, msg); |
1765
|
257 } |
|
258 |
2668
|
259 int |
3504
|
260 file_ops::rename (const std::string& from, const std::string& to, |
|
261 std::string& msg) |
2668
|
262 { |
2937
|
263 int status = -1; |
|
264 |
3504
|
265 msg = std::string (); |
2668
|
266 |
2937
|
267 #if defined (HAVE_RENAME) |
|
268 status = ::rename (from.c_str (), to.c_str ()); |
2668
|
269 |
|
270 if (status < 0) |
3504
|
271 { |
|
272 using namespace std; |
|
273 msg = ::strerror (errno); |
|
274 } |
2937
|
275 #else |
|
276 msg = NOT_SUPPORTED ("rename"); |
|
277 #endif |
2668
|
278 |
|
279 return status; |
|
280 } |
|
281 |
2433
|
282 // We provide a replacement for rmdir(). |
|
283 |
1765
|
284 int |
3504
|
285 file_ops::rmdir (const std::string& name) |
1765
|
286 { |
3504
|
287 std::string msg; |
2937
|
288 return rmdir (name, msg); |
1765
|
289 } |
|
290 |
2668
|
291 int |
3504
|
292 file_ops::rmdir (const std::string& name, std::string& msg) |
2668
|
293 { |
3504
|
294 msg = std::string (); |
2668
|
295 |
2937
|
296 int status = -1; |
|
297 |
|
298 #if defined (HAVE_RMDIR) |
|
299 status = ::rmdir (name.c_str ()); |
2668
|
300 |
|
301 if (status < 0) |
3504
|
302 { |
|
303 using namespace std; |
|
304 msg = ::strerror (errno); |
|
305 } |
2937
|
306 #else |
|
307 msg = NOT_SUPPORTED ("rmdir"); |
|
308 #endif |
2668
|
309 |
|
310 return status; |
|
311 } |
|
312 |
2433
|
313 // We provide a replacement for tempnam(). |
|
314 |
3504
|
315 std::string |
|
316 file_ops::tempnam (const std::string& dir, const std::string& pfx) |
1802
|
317 { |
3504
|
318 std::string msg; |
2937
|
319 return tempnam (dir, pfx, msg); |
|
320 } |
|
321 |
3504
|
322 std::string |
|
323 file_ops::tempnam (const std::string& dir, const std::string& pfx, |
|
324 std::string& msg) |
2937
|
325 { |
3504
|
326 msg = std::string (); |
2937
|
327 |
3504
|
328 std::string retval; |
2937
|
329 |
|
330 const char *pdir = dir.empty () ? 0 : dir.c_str (); |
1802
|
331 |
2937
|
332 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
|
333 |
|
334 char *tmp = ::tempnam (pdir, ppfx); |
1802
|
335 |
|
336 if (tmp) |
|
337 { |
|
338 retval = tmp; |
|
339 |
2937
|
340 ::free (tmp); |
1802
|
341 } |
|
342 else |
3504
|
343 { |
|
344 using namespace std; |
|
345 msg = ::strerror (errno); |
|
346 } |
1802
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
3040
|
351 // The following tilde-expansion code was stolen and adapted from |
|
352 // readline. |
2926
|
353 |
3040
|
354 // The default value of tilde_additional_prefixes. This is set to |
|
355 // whitespace preceding a tilde so that simple programs which do not |
|
356 // perform any word separation get desired behaviour. |
|
357 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 }; |
|
358 |
|
359 // The default value of tilde_additional_suffixes. This is set to |
|
360 // whitespace or newline so that simple programs which do not perform |
|
361 // any word separation get desired behaviour. |
|
362 static const char *default_suffixes[] = { " ", "\n", ":", 0 }; |
|
363 |
|
364 // If non-null, this contains the address of a function that the |
|
365 // application wants called before trying the standard tilde |
|
366 // expansions. The function is called with the text sans tilde, and |
|
367 // returns a malloc()'ed string which is the expansion, or a NULL |
|
368 // pointer if the expansion fails. |
|
369 file_ops::tilde_expansion_hook file_ops::tilde_expansion_preexpansion_hook = 0; |
|
370 |
|
371 // If non-null, this contains the address of a function to call if the |
|
372 // standard meaning for expanding a tilde fails. The function is |
|
373 // called with the text (sans tilde, as in "foo"), and returns a |
|
374 // malloc()'ed string which is the expansion, or a NULL pointer if |
|
375 // there is no expansion. |
|
376 file_ops::tilde_expansion_hook file_ops::tilde_expansion_failure_hook = 0; |
|
377 |
|
378 // When non-null, this is a NULL terminated array of strings which are |
|
379 // duplicates for a tilde prefix. Bash uses this to expand `=~' and |
|
380 // `:~'. |
|
381 string_vector file_ops::tilde_additional_prefixes = default_prefixes; |
|
382 |
|
383 // When non-null, this is a NULL terminated array of strings which |
|
384 // match the end of a username, instead of just "/". Bash sets this |
|
385 // to `:' and `=~'. |
|
386 string_vector file_ops::tilde_additional_suffixes = default_suffixes; |
|
387 |
|
388 // Find the start of a tilde expansion in S, and return the index |
|
389 // of the tilde which starts the expansion. Place the length of the |
|
390 // text which identified this tilde starter in LEN, excluding the |
|
391 // tilde itself. |
|
392 |
|
393 static size_t |
3504
|
394 tilde_find_prefix (const std::string& s, size_t& len) |
3040
|
395 { |
|
396 len = 0; |
|
397 |
|
398 size_t s_len = s.length (); |
|
399 |
|
400 if (s_len == 0 || s[0] == '~') |
|
401 return 0; |
|
402 |
|
403 string_vector prefixes = file_ops::tilde_additional_prefixes; |
|
404 |
|
405 if (! prefixes.empty ()) |
|
406 { |
|
407 for (size_t i = 0; i < s_len; i++) |
|
408 { |
|
409 for (int j = 0; j < prefixes.length (); j++) |
|
410 { |
|
411 size_t pfx_len = prefixes[j].length (); |
|
412 |
|
413 if (prefixes[j].compare (s.substr (i, pfx_len)) == 0) |
|
414 { |
|
415 len = pfx_len - 1; |
|
416 return i + len; |
|
417 } |
|
418 } |
|
419 } |
|
420 } |
|
421 |
|
422 return s_len; |
|
423 } |
|
424 |
|
425 // Find the end of a tilde expansion in S, and return the index |
|
426 // of the character which ends the tilde definition. |
|
427 |
|
428 static size_t |
3504
|
429 tilde_find_suffix (const std::string& s) |
3040
|
430 { |
|
431 size_t s_len = s.length (); |
|
432 |
|
433 string_vector suffixes = file_ops::tilde_additional_suffixes; |
|
434 |
|
435 size_t i = 0; |
|
436 |
|
437 for ( ; i < s_len; i++) |
|
438 { |
4101
|
439 if (s[i] == file_ops::dir_sep_char) |
3040
|
440 break; |
|
441 |
|
442 if (! suffixes.empty ()) |
|
443 { |
|
444 for (int j = 0; j < suffixes.length (); j++) |
|
445 { |
|
446 size_t sfx_len = suffixes[j].length (); |
|
447 |
|
448 if (suffixes[j].compare (s.substr (i, sfx_len)) == 0) |
|
449 return i; |
|
450 } |
|
451 } |
|
452 } |
|
453 |
|
454 return i; |
|
455 } |
|
456 |
|
457 // Take FNAME and return the tilde prefix we want expanded. |
|
458 |
3504
|
459 static std::string |
|
460 isolate_tilde_prefix (const std::string& fname) |
3040
|
461 { |
|
462 size_t f_len = fname.length (); |
|
463 |
|
464 size_t len = 1; |
|
465 |
4101
|
466 while (len < f_len && fname[len] != file_ops::dir_sep_char) |
3040
|
467 len++; |
|
468 |
|
469 return fname.substr (1, len); |
|
470 } |
|
471 |
|
472 // Do the work of tilde expansion on FILENAME. FILENAME starts with a |
|
473 // tilde. |
|
474 |
3504
|
475 static std::string |
|
476 tilde_expand_word (const std::string& filename) |
3040
|
477 { |
|
478 size_t f_len = filename.length (); |
|
479 |
|
480 if (f_len == 0 || filename[0] != '~') |
|
481 return filename; |
|
482 |
|
483 // A leading `~/' or a bare `~' is *always* translated to the value |
|
484 // of $HOME or the home directory of the current user, regardless of |
|
485 // any preexpansion hook. |
|
486 |
4101
|
487 if (f_len == 1 || filename[1] == file_ops::dir_sep_char) |
3040
|
488 return octave_env::get_home_directory () + filename.substr (1); |
|
489 |
3504
|
490 std::string username = isolate_tilde_prefix (filename); |
3040
|
491 |
|
492 size_t user_len = username.length (); |
|
493 |
3504
|
494 std::string dirname; |
3040
|
495 |
|
496 if (file_ops::tilde_expansion_preexpansion_hook) |
|
497 { |
3504
|
498 std::string expansion |
3040
|
499 = file_ops::tilde_expansion_preexpansion_hook (username); |
|
500 |
|
501 if (! expansion.empty ()) |
3074
|
502 return expansion + filename.substr (user_len+1); |
3040
|
503 } |
|
504 |
|
505 // No preexpansion hook, or the preexpansion hook failed. Look in the |
|
506 // password database. |
|
507 |
|
508 octave_passwd pw = octave_passwd::getpwnam (username); |
|
509 |
|
510 if (! pw) |
|
511 { |
|
512 // If the calling program has a special syntax for expanding tildes, |
|
513 // and we couldn't find a standard expansion, then let them try. |
|
514 |
|
515 if (file_ops::tilde_expansion_failure_hook) |
|
516 { |
3504
|
517 std::string expansion |
3040
|
518 = file_ops::tilde_expansion_failure_hook (username); |
|
519 |
|
520 if (! expansion.empty ()) |
3074
|
521 dirname = expansion + filename.substr (user_len+1); |
3040
|
522 } |
|
523 |
|
524 // If we don't have a failure hook, or if the failure hook did not |
|
525 // expand the tilde, return a copy of what we were passed. |
|
526 |
|
527 if (dirname.length () == 0) |
|
528 dirname = filename; |
|
529 } |
|
530 else |
3074
|
531 dirname = pw.dir () + filename.substr (user_len+1); |
3040
|
532 |
|
533 return dirname; |
|
534 } |
|
535 |
|
536 // If NAME has a leading ~ or ~user, Unix-style, expand it to the |
|
537 // user's home directory. If no ~, or no <pwd.h>, just return NAME. |
|
538 |
3504
|
539 std::string |
|
540 file_ops::tilde_expand (const std::string& name) |
2926
|
541 { |
3504
|
542 std::string result; |
2926
|
543 |
3040
|
544 size_t name_len = name.length (); |
2926
|
545 |
3040
|
546 // Scan through S expanding tildes as we come to them. |
2926
|
547 |
3040
|
548 size_t pos = 0; |
2926
|
549 |
3040
|
550 while (1) |
|
551 { |
|
552 if (pos > name_len) |
|
553 break; |
2926
|
554 |
3040
|
555 size_t len; |
|
556 |
|
557 // Make START point to the tilde which starts the expansion. |
2926
|
558 |
3040
|
559 size_t start = tilde_find_prefix (name.substr (pos), len); |
|
560 |
|
561 result.append (name.substr (pos, start)); |
2926
|
562 |
3040
|
563 // Advance STRING to the starting tilde. |
|
564 |
|
565 pos += start; |
2926
|
566 |
3040
|
567 // Make FINI be the index of one after the last character of the |
|
568 // username. |
2926
|
569 |
3040
|
570 size_t fini = tilde_find_suffix (name.substr (pos)); |
2926
|
571 |
3040
|
572 // If both START and FINI are zero, we are all done. |
2926
|
573 |
3040
|
574 if (! (start || fini)) |
|
575 break; |
2926
|
576 |
3040
|
577 // Expand the entire tilde word, and copy it into RESULT. |
2947
|
578 |
3504
|
579 std::string tilde_word = name.substr (pos, fini); |
3040
|
580 |
|
581 pos += fini; |
2926
|
582 |
3504
|
583 std::string expansion = tilde_expand_word (tilde_word); |
3040
|
584 |
|
585 result.append (expansion); |
2926
|
586 } |
|
587 |
3040
|
588 return result; |
2926
|
589 } |
|
590 |
|
591 // A vector version of the above. |
|
592 |
|
593 string_vector |
|
594 file_ops::tilde_expand (const string_vector& names) |
|
595 { |
|
596 string_vector retval; |
|
597 |
|
598 int n = names.length (); |
|
599 |
|
600 retval.resize (n); |
|
601 |
|
602 for (int i = 0; i < n; i++) |
|
603 retval[i] = file_ops::tilde_expand (names[i]); |
|
604 |
|
605 return retval; |
|
606 } |
1802
|
607 |
1765
|
608 int |
2926
|
609 file_ops::umask (mode_t mode) |
1765
|
610 { |
|
611 #if defined (HAVE_UMASK) |
2926
|
612 return ::umask (mode); |
1765
|
613 #else |
|
614 return 0; |
|
615 #endif |
|
616 } |
|
617 |
1773
|
618 int |
3504
|
619 file_ops::unlink (const std::string& name) |
1773
|
620 { |
3504
|
621 std::string msg; |
2937
|
622 return unlink (name, msg); |
1773
|
623 } |
|
624 |
2668
|
625 int |
3504
|
626 file_ops::unlink (const std::string& name, std::string& msg) |
2668
|
627 { |
3504
|
628 msg = std::string (); |
2668
|
629 |
2937
|
630 int status = -1; |
|
631 |
|
632 #if defined (HAVE_UNLINK) |
|
633 status = ::unlink (name.c_str ()); |
2668
|
634 |
|
635 if (status < 0) |
3504
|
636 { |
|
637 using namespace std; |
|
638 msg = ::strerror (errno); |
|
639 } |
2937
|
640 #else |
|
641 msg = NOT_SUPPORTED ("unlink"); |
|
642 #endif |
2668
|
643 |
|
644 return status; |
|
645 } |
|
646 |
4097
|
647 bool |
|
648 file_ops::is_dir_sep (char c) |
|
649 { |
4101
|
650 return dir_sep_chars.find (c) != NPOS; |
4097
|
651 } |
|
652 |
1765
|
653 /* |
|
654 ;;; Local Variables: *** |
|
655 ;;; mode: C++ *** |
|
656 ;;; End: *** |
|
657 */ |