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