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