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