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