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