523
|
1 // dirfns.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
523
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
523
|
21 |
|
22 */ |
|
23 |
661
|
24 /* |
|
25 |
|
26 The functions listed below were adapted from a similar functions |
|
27 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
28 Free Software Foundation, Inc. |
|
29 |
|
30 polite_directory_format absolute_pathname |
|
31 absolute_program base_pathname |
|
32 make_absolute pathname_backup |
|
33 change_to_directory get_working_directory |
|
34 |
|
35 */ |
|
36 |
523
|
37 #ifdef HAVE_CONFIG_H |
1192
|
38 #include <config.h> |
523
|
39 #endif |
|
40 |
1341
|
41 #include <cerrno> |
|
42 #include <cstdio> |
|
43 #include <cstddef> |
|
44 #include <cstdlib> |
|
45 #include <cstring> |
|
46 |
1728
|
47 #include <string> |
|
48 |
523
|
49 #include <strstream.h> |
|
50 |
1755
|
51 #include "str-vec.h" |
|
52 |
1355
|
53 #include "defun.h" |
|
54 #include "dirfns.h" |
|
55 #include "error.h" |
1402
|
56 #include "gripes.h" |
1389
|
57 #include "help.h" |
1750
|
58 #include "oct-obj.h" |
1355
|
59 #include "pager.h" |
|
60 #include "pathlen.h" |
|
61 #include "procstream.h" |
1750
|
62 #include "pt-plot.h" |
523
|
63 #include "statdefs.h" |
1355
|
64 #include "sysdep.h" |
|
65 #include "sysdir.h" |
1750
|
66 #include "toplev.h" |
1449
|
67 #include "unwind-prot.h" |
523
|
68 #include "utils.h" |
1742
|
69 #include "variables.h" |
523
|
70 |
|
71 // Non-zero means follow symbolic links that point to directories just |
|
72 // as if they are real directories. |
|
73 static int follow_symbolic_links = 1; |
|
74 |
|
75 // Non-zero means that pwd always give verbatim directory, regardless |
|
76 // of symbolic link following. |
|
77 static int verbatim_pwd = 1; |
|
78 |
661
|
79 // Remove the last N directories from PATH. Do not PATH blank. |
|
80 // PATH must contain enough space for MAXPATHLEN characters. |
|
81 |
1755
|
82 static void |
|
83 pathname_backup (string& path, int n) |
523
|
84 { |
1755
|
85 if (path.empty ()) |
523
|
86 return; |
|
87 |
1755
|
88 size_t i = path.length () - 1; |
523
|
89 |
|
90 while (n--) |
|
91 { |
1755
|
92 while (path[i] == '/' && i > 0) |
|
93 i--; |
523
|
94 |
1755
|
95 while (path[i] != '/' && i > 0) |
|
96 i--; |
523
|
97 |
1755
|
98 i++; |
523
|
99 } |
1755
|
100 |
|
101 path.resize (i); |
523
|
102 } |
|
103 |
661
|
104 // Return a pretty pathname. If the first part of the pathname is the |
|
105 // same as $HOME, then replace that with `~'. |
|
106 |
1755
|
107 string |
|
108 polite_directory_format (const string& name) |
523
|
109 { |
1755
|
110 string retval; |
523
|
111 |
1755
|
112 size_t len = home_directory.length (); |
|
113 |
|
114 if (len > 1 && home_directory.compare (name, 0, len) == 0 |
|
115 && (name.length () == len || name[len] == '/')) |
523
|
116 { |
1755
|
117 retval = "~"; |
|
118 retval.append (name.substr (len)); |
523
|
119 } |
|
120 else |
1755
|
121 retval = name; |
|
122 |
|
123 return retval; |
523
|
124 } |
|
125 |
661
|
126 // Return 1 if STRING contains an absolute pathname, else 0. |
|
127 |
523
|
128 int |
1755
|
129 absolute_pathname (const string& s) |
523
|
130 { |
1755
|
131 if (s.empty ()) |
523
|
132 return 0; |
|
133 |
1755
|
134 if (s[0] == '/') |
523
|
135 return 1; |
|
136 |
1755
|
137 if (s[0] == '.') |
523
|
138 { |
1755
|
139 if (s[1] == '\0' || s[1] == '/') |
523
|
140 return 1; |
|
141 |
1755
|
142 if (s[1] == '.') |
|
143 if (s[2] == '\0' || s[2] == '/') |
523
|
144 return 1; |
|
145 } |
1755
|
146 |
523
|
147 return 0; |
|
148 } |
|
149 |
661
|
150 // Return 1 if STRING is an absolute program name; it is absolute if |
|
151 // it contains any slashes. This is used to decide whether or not to |
|
152 // look up through $PATH. |
|
153 |
523
|
154 int |
1755
|
155 absolute_program (const string& s) |
523
|
156 { |
1755
|
157 return (s.find ('/') != NPOS); |
523
|
158 } |
|
159 |
661
|
160 // Return the `basename' of the pathname in STRING (the stuff after |
|
161 // the last '/'). If STRING is not a full pathname, simply return it. |
|
162 |
1755
|
163 string |
|
164 base_pathname (const string& s) |
523
|
165 { |
1755
|
166 if (! absolute_pathname (s)) |
|
167 return s; |
523
|
168 |
1755
|
169 size_t pos = s.rfind ('/'); |
523
|
170 |
1755
|
171 if (pos == NPOS) |
|
172 return s; |
523
|
173 else |
1755
|
174 return s.substr (pos+1); |
523
|
175 } |
|
176 |
661
|
177 // Turn STRING (a pathname) into an absolute pathname, assuming that |
1755
|
178 // DOT_PATH contains the symbolic location of '.'. |
661
|
179 |
1755
|
180 string |
|
181 make_absolute (const string& s, const string& dot_path) |
523
|
182 { |
1755
|
183 if (dot_path.empty () || s[0] == '/') |
|
184 return s; |
|
185 |
|
186 string current_path = dot_path; |
523
|
187 |
1755
|
188 if (current_path.empty ()) |
|
189 current_path = "./"; |
523
|
190 |
1755
|
191 size_t pos = current_path.length () - 1; |
523
|
192 |
1755
|
193 if (current_path[pos] != '/') |
|
194 current_path.append ("/"); |
523
|
195 |
1755
|
196 size_t i = 0; |
|
197 size_t slen = s.length (); |
523
|
198 |
1755
|
199 while (i < slen) |
523
|
200 { |
1755
|
201 if (s[i] == '.') |
523
|
202 { |
1755
|
203 if (i + 1 == slen) |
|
204 return current_path; |
523
|
205 |
1755
|
206 if (s[i+1] == '/') |
523
|
207 { |
1755
|
208 i += 2; |
523
|
209 continue; |
|
210 } |
|
211 |
1755
|
212 if (s[i+1] == '.' && (i + 2 == slen || s[i+2] == '/')) |
523
|
213 { |
1755
|
214 i += 2; |
523
|
215 |
1755
|
216 if (i != slen) |
|
217 i++; |
523
|
218 |
|
219 pathname_backup (current_path, 1); |
1755
|
220 |
523
|
221 continue; |
|
222 } |
|
223 } |
|
224 |
1755
|
225 size_t tmp = s.find ('/', i); |
523
|
226 |
1755
|
227 if (tmp == NPOS) |
|
228 { |
|
229 current_path.append (s, i, tmp-i); |
|
230 break; |
|
231 } |
|
232 else |
|
233 { |
|
234 current_path.append (s, i, tmp-i+1); |
|
235 i = tmp + 1; |
|
236 } |
|
237 } |
523
|
238 |
1755
|
239 return current_path; |
523
|
240 } |
|
241 |
661
|
242 // Has file `A' been modified after time `T'? |
|
243 // |
|
244 // case: |
|
245 // |
|
246 // a newer than t returns 1 |
|
247 // a older than t returns 0 |
|
248 // stat on a fails returns -1 |
|
249 |
523
|
250 int |
1755
|
251 is_newer (const string& fa, time_t t) |
523
|
252 { |
|
253 struct stat fa_sb; |
|
254 register int fa_stat; |
|
255 register int status = 0; |
|
256 |
1755
|
257 fa_stat = stat (fa.c_str (), &fa_sb); |
523
|
258 if (fa_stat != 0) |
|
259 status = -1; |
|
260 |
|
261 if (status != 0) |
|
262 return status; |
|
263 |
|
264 return (fa_sb.st_mtime > t); |
|
265 } |
|
266 |
661
|
267 // Return a consed string which is the current working directory. |
|
268 // FOR_WHOM is the name of the caller for error printing. |
|
269 |
1755
|
270 string |
|
271 get_working_directory (const string& for_whom) |
523
|
272 { |
|
273 if (! follow_symbolic_links) |
1755
|
274 the_current_working_directory = ""; |
523
|
275 |
1755
|
276 if (the_current_working_directory.empty ()) |
523
|
277 { |
1755
|
278 the_current_working_directory = octave_getcwd (); |
523
|
279 |
1755
|
280 if (the_current_working_directory.empty ()) |
|
281 warning ("%s: can't find current directory!", for_whom.c_str ()); |
523
|
282 } |
|
283 |
|
284 return the_current_working_directory; |
|
285 } |
|
286 |
661
|
287 // Do the work of changing to the directory NEWDIR. Handle symbolic |
|
288 // link following, etc. |
|
289 |
523
|
290 static int |
1755
|
291 change_to_directory (const string& newdir) |
523
|
292 { |
1755
|
293 string tmp; |
523
|
294 |
|
295 if (follow_symbolic_links) |
|
296 { |
1755
|
297 if (the_current_working_directory.empty ()) |
523
|
298 get_working_directory ("cd_links"); |
|
299 |
1755
|
300 if (the_current_working_directory.empty ()) |
|
301 tmp = newdir; |
523
|
302 else |
1755
|
303 tmp = make_absolute (newdir, the_current_working_directory); |
523
|
304 |
1358
|
305 // Get rid of trailing `/'. |
|
306 |
1755
|
307 size_t len = tmp.length (); |
523
|
308 |
1755
|
309 if (len > 1) |
523
|
310 { |
1755
|
311 if (tmp[--len] == '/') |
|
312 tmp.resize (len); |
523
|
313 } |
|
314 |
1755
|
315 if (octave_chdir (tmp) < 0) |
|
316 return 0; |
|
317 else |
|
318 { |
|
319 the_current_working_directory = tmp; |
|
320 return 1; |
|
321 } |
523
|
322 } |
|
323 else |
1755
|
324 return (octave_chdir (newdir) < 0) ? 0 : 1; |
523
|
325 } |
|
326 |
1328
|
327 static int |
1755
|
328 octave_change_to_directory (const string& newdir) |
1328
|
329 { |
|
330 int cd_ok = change_to_directory (newdir); |
|
331 |
|
332 if (cd_ok) |
|
333 do_external_plotter_cd (newdir); |
|
334 else |
1755
|
335 error ("%s: %s", newdir.c_str (), strerror (errno)); |
1328
|
336 |
|
337 return cd_ok; |
|
338 } |
|
339 |
1488
|
340 DEFUN_TEXT ("cd", Fcd, Scd, 10, |
523
|
341 "cd [dir]\n\ |
|
342 \n\ |
|
343 change current working directory\n\ |
|
344 if no arguments are given, the current directory is changed to the\n\ |
|
345 users home directory") |
|
346 { |
|
347 Octave_object retval; |
|
348 |
1755
|
349 int argc = args.length () + 1; |
|
350 |
|
351 string_vector argv = make_argv (args, "cd"); |
|
352 |
|
353 if (error_state) |
|
354 return retval; |
523
|
355 |
|
356 if (argc > 1) |
|
357 { |
1750
|
358 string dirname = oct_tilde_expand (argv[1]); |
523
|
359 |
1750
|
360 if (dirname.length () > 0 |
1755
|
361 && ! octave_change_to_directory (dirname)) |
523
|
362 { |
|
363 return retval; |
|
364 } |
|
365 } |
|
366 else |
|
367 { |
1755
|
368 if (home_directory.empty () |
|
369 || ! octave_change_to_directory (home_directory)) |
523
|
370 { |
|
371 return retval; |
|
372 } |
|
373 } |
|
374 |
1755
|
375 string directory = get_working_directory ("cd"); |
523
|
376 tree_constant *dir = new tree_constant (directory); |
|
377 bind_builtin_variable ("PWD", dir, 1); |
|
378 |
|
379 return retval; |
|
380 } |
|
381 |
611
|
382 DEFALIAS (chdir, cd); |
|
383 |
661
|
384 // Get a directory listing. |
|
385 |
1488
|
386 DEFUN_TEXT ("ls", Fls, Sls, 10, |
523
|
387 "ls [options]\n\ |
|
388 \n\ |
|
389 print a directory listing") |
|
390 { |
|
391 Octave_object retval; |
|
392 |
1755
|
393 int argc = args.length () + 1; |
|
394 |
|
395 string_vector argv = make_argv (args, "ls"); |
|
396 |
|
397 if (error_state) |
|
398 return retval; |
523
|
399 |
|
400 ostrstream ls_buf; |
|
401 |
|
402 ls_buf << "ls -C "; |
|
403 for (int i = 1; i < argc; i++) |
1750
|
404 ls_buf << oct_tilde_expand (argv[i]) << " "; |
523
|
405 |
|
406 ls_buf << ends; |
1449
|
407 char *ls_command = ls_buf.str (); |
523
|
408 |
1449
|
409 iprocstream *cmd = new iprocstream (ls_command); |
523
|
410 |
1469
|
411 delete [] ls_command; |
|
412 |
1449
|
413 add_unwind_protect (cleanup_iprocstream, cmd); |
523
|
414 |
1449
|
415 if (cmd && *cmd) |
|
416 { |
|
417 int ch; |
|
418 ostrstream output_buf; |
|
419 while ((ch = cmd->get ()) != EOF) |
|
420 output_buf << (char) ch; |
|
421 output_buf << ends; |
523
|
422 |
1449
|
423 maybe_page_output (output_buf); |
|
424 } |
|
425 else |
|
426 error ("couldn't start process for ls!"); |
523
|
427 |
1449
|
428 run_unwind_protect (); |
523
|
429 |
|
430 return retval; |
|
431 } |
|
432 |
549
|
433 DEFALIAS (dir, ls); |
|
434 |
1592
|
435 DEFUN ("pwd", Fpwd, Spwd, 01, |
523
|
436 "pwd (): print current working directory") |
|
437 { |
|
438 Octave_object retval; |
1755
|
439 string directory; |
523
|
440 |
|
441 if (verbatim_pwd) |
|
442 { |
1755
|
443 directory = octave_getcwd (); |
523
|
444 |
1755
|
445 if (directory.empty ()) |
|
446 warning ("pwd: can't find working directory!"); |
523
|
447 } |
|
448 else |
1755
|
449 directory = get_working_directory ("pwd"); |
523
|
450 |
1755
|
451 if (! directory.empty ()) |
523
|
452 { |
1592
|
453 if (nargout == 0) |
|
454 { |
|
455 ostrstream output_buf; |
|
456 output_buf << directory << "\n" << ends; |
|
457 maybe_page_output (output_buf); |
|
458 } |
|
459 else |
|
460 retval = directory; |
523
|
461 } |
1592
|
462 |
523
|
463 return retval; |
|
464 } |
|
465 |
1488
|
466 DEFUN ("readdir", Freaddir, Sreaddir, 10, |
1389
|
467 "readdir (NAME)\n\ |
|
468 \n\ |
|
469 Return an array of strings containing the list of all files in the |
1401
|
470 named directory. If sucessful, returns 0; otherwise an error message |
|
471 is printed.") |
1389
|
472 { |
|
473 Octave_object retval; |
1572
|
474 charMatrix dirlist; |
1389
|
475 int status = 0; |
|
476 |
1401
|
477 if (args.length () == 1) |
1389
|
478 { |
1755
|
479 string dirname = args(0).string_value (); |
1389
|
480 |
1401
|
481 if (error_state) |
1389
|
482 { |
1401
|
483 status = -1; |
1402
|
484 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
485 } |
|
486 else |
|
487 { |
1750
|
488 string tmp = oct_tilde_expand (dirname); |
1489
|
489 |
1750
|
490 DIR *dir = opendir (tmp.c_str ()); |
1389
|
491 |
1401
|
492 if (dir) |
1389
|
493 { |
1401
|
494 int count = 0; |
1572
|
495 int max_len = 0; |
|
496 |
|
497 struct dirent *dir_entry; |
|
498 |
|
499 while ((dir_entry = readdir (dir))) |
|
500 { |
|
501 count++; |
|
502 int len = strlen (dir_entry->d_name); |
|
503 if (len > max_len) |
|
504 max_len = len; |
|
505 } |
1401
|
506 |
|
507 rewinddir (dir); |
|
508 |
1572
|
509 dirlist.resize (count, max_len, 0); |
1389
|
510 |
1401
|
511 while ((dir_entry = readdir (dir))) |
|
512 { |
|
513 if (--count < 0) |
|
514 break; |
|
515 |
1572
|
516 dirlist.insert (dir_entry->d_name, count, 0); |
1401
|
517 } |
1389
|
518 |
|
519 #if defined (CLOSEDIR_VOID) |
1401
|
520 closedir (dir); |
1389
|
521 #else |
1401
|
522 if (closedir (dir) < 0) |
|
523 { |
|
524 status = -1; |
|
525 error ("%s", strerror (errno)); |
|
526 } |
1389
|
527 #endif |
|
528 |
1401
|
529 if (count != 0) |
|
530 { |
|
531 status = -1; |
|
532 error ("readdir: failed reading directory"); |
|
533 } |
|
534 } |
|
535 else |
|
536 { |
|
537 status = -1; |
|
538 error ("%s", strerror (errno)); |
|
539 } |
1389
|
540 } |
|
541 } |
|
542 else |
|
543 print_usage ("readdir"); |
|
544 |
1401
|
545 if (status == 0) |
1572
|
546 retval(0) = tree_constant (dirlist, 1); |
1401
|
547 |
|
548 return retval; |
|
549 } |
|
550 |
|
551 // XXX FIXME XXX -- should probably also allow second arg to specify |
|
552 // mode. |
|
553 |
1488
|
554 DEFUN ("mkdir", Fmkdir, Smkdir, 10, |
1401
|
555 "mkdir (NAME)\n\ |
|
556 \n\ |
|
557 Create the directory named by NAME. If successful, returns 0;\n\ |
|
558 otherwise prints an error message.") |
|
559 { |
|
560 Octave_object retval; |
|
561 |
|
562 int status = 0; |
|
563 |
|
564 if (args.length () == 1) |
|
565 { |
1755
|
566 string dirname = args(0).string_value (); |
1401
|
567 |
|
568 if (error_state) |
1402
|
569 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
570 else |
1401
|
571 { |
1750
|
572 string tmp = oct_tilde_expand (dirname); |
1489
|
573 |
1750
|
574 int mkdir_retval = mkdir (tmp.c_str (), 0777); |
1489
|
575 |
|
576 if (mkdir_retval < 0) |
|
577 { |
|
578 status = -1; |
|
579 error ("%s", strerror (errno)); |
|
580 } |
1401
|
581 } |
|
582 } |
|
583 else |
|
584 print_usage ("mkdir"); |
|
585 |
|
586 if (status == 0) |
1389
|
587 retval (0) = (double) status; |
1401
|
588 |
|
589 return retval; |
|
590 } |
|
591 |
1488
|
592 DEFUN ("rmdir", Frmdir, Srmdir, 10, |
1401
|
593 "rmdir (NAME)\n\ |
|
594 \n\ |
|
595 Remove the directory named by NAME. If successful, returns 0;\n\ |
|
596 otherwise prints an error message.") |
|
597 { |
|
598 Octave_object retval; |
|
599 |
|
600 int status = 0; |
|
601 |
|
602 if (args.length () == 1) |
|
603 { |
1755
|
604 string dirname = args(0).string_value (); |
1401
|
605 |
|
606 if (error_state) |
1402
|
607 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
608 else |
1401
|
609 { |
1750
|
610 string tmp = oct_tilde_expand (dirname); |
1489
|
611 |
1750
|
612 int rmdir_retval = rmdir (tmp.c_str ()); |
1489
|
613 |
|
614 if (rmdir_retval < 0) |
|
615 { |
|
616 status = -1; |
|
617 error ("%s", strerror (errno)); |
|
618 } |
1401
|
619 } |
|
620 } |
1389
|
621 else |
1401
|
622 print_usage ("rmdir"); |
|
623 |
|
624 if (status == 0) |
|
625 retval (0) = (double) status; |
|
626 |
|
627 return retval; |
|
628 } |
|
629 |
1488
|
630 DEFUN ("rename", Frename, Srename, 10, |
1401
|
631 "rename (FROM, TO)\n\ |
|
632 \n\ |
|
633 Rename a file. If successful, returns 0;\n\ |
|
634 otherwise prints an error message and returns -1.") |
|
635 { |
|
636 Octave_object retval; |
|
637 |
|
638 int status = 0; |
|
639 |
|
640 if (args.length () == 2) |
|
641 { |
1755
|
642 string from = args(0).string_value (); |
1728
|
643 |
1401
|
644 if (error_state) |
1402
|
645 gripe_wrong_type_arg ("rename", args(0)); |
|
646 else |
1401
|
647 { |
1755
|
648 string to = args(1).string_value (); |
1728
|
649 |
1402
|
650 if (error_state) |
|
651 gripe_wrong_type_arg ("rename", args(1)); |
1755
|
652 else if (rename (from.c_str (), to.c_str ()) < 0) |
1402
|
653 { |
|
654 status = -1; |
|
655 error ("%s", strerror (errno)); |
|
656 } |
1401
|
657 } |
|
658 } |
|
659 else |
|
660 print_usage ("rename"); |
|
661 |
|
662 if (status == 0) |
|
663 retval (0) = (double) status; |
1389
|
664 |
|
665 return retval; |
|
666 } |
|
667 |
523
|
668 /* |
|
669 ;;; Local Variables: *** |
|
670 ;;; mode: C++ *** |
|
671 ;;; page-delimiter: "^/\\*" *** |
|
672 ;;; End: *** |
|
673 */ |