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