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