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