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 |
1804
|
31 base_pathname |
661
|
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" |
1781
|
54 #include "dir-ops.h" |
1355
|
55 #include "dirfns.h" |
|
56 #include "error.h" |
1766
|
57 #include "file-ops.h" |
1402
|
58 #include "gripes.h" |
1389
|
59 #include "help.h" |
1750
|
60 #include "oct-obj.h" |
1355
|
61 #include "pager.h" |
|
62 #include "pathlen.h" |
|
63 #include "procstream.h" |
1750
|
64 #include "pt-plot.h" |
1355
|
65 #include "sysdep.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 |
1803
|
128 static 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 the `basename' of the pathname in STRING (the stuff after |
|
151 // the last '/'). If STRING is not a full pathname, simply return it. |
|
152 |
1755
|
153 string |
|
154 base_pathname (const string& s) |
523
|
155 { |
1755
|
156 if (! absolute_pathname (s)) |
|
157 return s; |
523
|
158 |
1755
|
159 size_t pos = s.rfind ('/'); |
523
|
160 |
1755
|
161 if (pos == NPOS) |
|
162 return s; |
523
|
163 else |
1755
|
164 return s.substr (pos+1); |
523
|
165 } |
|
166 |
661
|
167 // Turn STRING (a pathname) into an absolute pathname, assuming that |
1755
|
168 // DOT_PATH contains the symbolic location of '.'. |
661
|
169 |
1755
|
170 string |
|
171 make_absolute (const string& s, const string& dot_path) |
523
|
172 { |
1785
|
173 if (dot_path.empty () || s[0] == '/' || s.empty ()) |
1755
|
174 return s; |
|
175 |
|
176 string current_path = dot_path; |
523
|
177 |
1755
|
178 if (current_path.empty ()) |
|
179 current_path = "./"; |
523
|
180 |
1755
|
181 size_t pos = current_path.length () - 1; |
523
|
182 |
1755
|
183 if (current_path[pos] != '/') |
|
184 current_path.append ("/"); |
523
|
185 |
1755
|
186 size_t i = 0; |
|
187 size_t slen = s.length (); |
523
|
188 |
1755
|
189 while (i < slen) |
523
|
190 { |
1755
|
191 if (s[i] == '.') |
523
|
192 { |
1755
|
193 if (i + 1 == slen) |
|
194 return current_path; |
523
|
195 |
1755
|
196 if (s[i+1] == '/') |
523
|
197 { |
1755
|
198 i += 2; |
523
|
199 continue; |
|
200 } |
|
201 |
1755
|
202 if (s[i+1] == '.' && (i + 2 == slen || s[i+2] == '/')) |
523
|
203 { |
1755
|
204 i += 2; |
523
|
205 |
1755
|
206 if (i != slen) |
|
207 i++; |
523
|
208 |
|
209 pathname_backup (current_path, 1); |
1755
|
210 |
523
|
211 continue; |
|
212 } |
|
213 } |
|
214 |
1755
|
215 size_t tmp = s.find ('/', i); |
523
|
216 |
1755
|
217 if (tmp == NPOS) |
|
218 { |
|
219 current_path.append (s, i, tmp-i); |
|
220 break; |
|
221 } |
|
222 else |
|
223 { |
|
224 current_path.append (s, i, tmp-i+1); |
|
225 i = tmp + 1; |
|
226 } |
|
227 } |
523
|
228 |
1755
|
229 return current_path; |
523
|
230 } |
|
231 |
661
|
232 // Return a consed string which is the current working directory. |
|
233 // FOR_WHOM is the name of the caller for error printing. |
|
234 |
1755
|
235 string |
|
236 get_working_directory (const string& for_whom) |
523
|
237 { |
|
238 if (! follow_symbolic_links) |
1755
|
239 the_current_working_directory = ""; |
523
|
240 |
1755
|
241 if (the_current_working_directory.empty ()) |
523
|
242 { |
1755
|
243 the_current_working_directory = octave_getcwd (); |
523
|
244 |
1755
|
245 if (the_current_working_directory.empty ()) |
|
246 warning ("%s: can't find current directory!", for_whom.c_str ()); |
523
|
247 } |
|
248 |
|
249 return the_current_working_directory; |
|
250 } |
|
251 |
661
|
252 // Do the work of changing to the directory NEWDIR. Handle symbolic |
|
253 // link following, etc. |
|
254 |
523
|
255 static int |
1755
|
256 change_to_directory (const string& newdir) |
523
|
257 { |
1755
|
258 string tmp; |
523
|
259 |
|
260 if (follow_symbolic_links) |
|
261 { |
1755
|
262 if (the_current_working_directory.empty ()) |
523
|
263 get_working_directory ("cd_links"); |
|
264 |
1755
|
265 if (the_current_working_directory.empty ()) |
|
266 tmp = newdir; |
523
|
267 else |
1755
|
268 tmp = make_absolute (newdir, the_current_working_directory); |
523
|
269 |
1358
|
270 // Get rid of trailing `/'. |
|
271 |
1755
|
272 size_t len = tmp.length (); |
523
|
273 |
1755
|
274 if (len > 1) |
523
|
275 { |
1755
|
276 if (tmp[--len] == '/') |
|
277 tmp.resize (len); |
523
|
278 } |
|
279 |
1755
|
280 if (octave_chdir (tmp) < 0) |
|
281 return 0; |
|
282 else |
|
283 { |
|
284 the_current_working_directory = tmp; |
|
285 return 1; |
|
286 } |
523
|
287 } |
|
288 else |
1755
|
289 return (octave_chdir (newdir) < 0) ? 0 : 1; |
523
|
290 } |
|
291 |
1328
|
292 static int |
1755
|
293 octave_change_to_directory (const string& newdir) |
1328
|
294 { |
|
295 int cd_ok = change_to_directory (newdir); |
|
296 |
|
297 if (cd_ok) |
|
298 do_external_plotter_cd (newdir); |
|
299 else |
1755
|
300 error ("%s: %s", newdir.c_str (), strerror (errno)); |
1328
|
301 |
|
302 return cd_ok; |
|
303 } |
|
304 |
1488
|
305 DEFUN_TEXT ("cd", Fcd, Scd, 10, |
523
|
306 "cd [dir]\n\ |
|
307 \n\ |
|
308 change current working directory\n\ |
|
309 if no arguments are given, the current directory is changed to the\n\ |
|
310 users home directory") |
|
311 { |
|
312 Octave_object retval; |
|
313 |
1755
|
314 int argc = args.length () + 1; |
|
315 |
|
316 string_vector argv = make_argv (args, "cd"); |
|
317 |
|
318 if (error_state) |
|
319 return retval; |
523
|
320 |
|
321 if (argc > 1) |
|
322 { |
1750
|
323 string dirname = oct_tilde_expand (argv[1]); |
523
|
324 |
1750
|
325 if (dirname.length () > 0 |
1755
|
326 && ! octave_change_to_directory (dirname)) |
523
|
327 { |
|
328 return retval; |
|
329 } |
|
330 } |
|
331 else |
|
332 { |
1755
|
333 if (home_directory.empty () |
|
334 || ! octave_change_to_directory (home_directory)) |
523
|
335 { |
|
336 return retval; |
|
337 } |
|
338 } |
|
339 |
1755
|
340 string directory = get_working_directory ("cd"); |
523
|
341 tree_constant *dir = new tree_constant (directory); |
|
342 bind_builtin_variable ("PWD", dir, 1); |
|
343 |
|
344 return retval; |
|
345 } |
|
346 |
611
|
347 DEFALIAS (chdir, cd); |
|
348 |
661
|
349 // Get a directory listing. |
|
350 |
1488
|
351 DEFUN_TEXT ("ls", Fls, Sls, 10, |
523
|
352 "ls [options]\n\ |
|
353 \n\ |
|
354 print a directory listing") |
|
355 { |
|
356 Octave_object retval; |
|
357 |
1755
|
358 int argc = args.length () + 1; |
|
359 |
|
360 string_vector argv = make_argv (args, "ls"); |
|
361 |
|
362 if (error_state) |
|
363 return retval; |
523
|
364 |
|
365 ostrstream ls_buf; |
|
366 |
|
367 ls_buf << "ls -C "; |
|
368 for (int i = 1; i < argc; i++) |
1750
|
369 ls_buf << oct_tilde_expand (argv[i]) << " "; |
523
|
370 |
|
371 ls_buf << ends; |
1449
|
372 char *ls_command = ls_buf.str (); |
523
|
373 |
1449
|
374 iprocstream *cmd = new iprocstream (ls_command); |
523
|
375 |
1469
|
376 delete [] ls_command; |
|
377 |
1449
|
378 add_unwind_protect (cleanup_iprocstream, cmd); |
523
|
379 |
1449
|
380 if (cmd && *cmd) |
|
381 { |
|
382 int ch; |
|
383 ostrstream output_buf; |
|
384 while ((ch = cmd->get ()) != EOF) |
|
385 output_buf << (char) ch; |
|
386 output_buf << ends; |
523
|
387 |
1449
|
388 maybe_page_output (output_buf); |
|
389 } |
|
390 else |
|
391 error ("couldn't start process for ls!"); |
523
|
392 |
1449
|
393 run_unwind_protect (); |
523
|
394 |
|
395 return retval; |
|
396 } |
|
397 |
549
|
398 DEFALIAS (dir, ls); |
|
399 |
1592
|
400 DEFUN ("pwd", Fpwd, Spwd, 01, |
523
|
401 "pwd (): print current working directory") |
|
402 { |
|
403 Octave_object retval; |
1755
|
404 string directory; |
523
|
405 |
|
406 if (verbatim_pwd) |
|
407 { |
1755
|
408 directory = octave_getcwd (); |
523
|
409 |
1755
|
410 if (directory.empty ()) |
|
411 warning ("pwd: can't find working directory!"); |
523
|
412 } |
|
413 else |
1755
|
414 directory = get_working_directory ("pwd"); |
523
|
415 |
1755
|
416 if (! directory.empty ()) |
523
|
417 { |
1592
|
418 if (nargout == 0) |
|
419 { |
|
420 ostrstream output_buf; |
|
421 output_buf << directory << "\n" << ends; |
|
422 maybe_page_output (output_buf); |
|
423 } |
|
424 else |
|
425 retval = directory; |
523
|
426 } |
1592
|
427 |
523
|
428 return retval; |
|
429 } |
|
430 |
1488
|
431 DEFUN ("readdir", Freaddir, Sreaddir, 10, |
1389
|
432 "readdir (NAME)\n\ |
|
433 \n\ |
|
434 Return an array of strings containing the list of all files in the |
1401
|
435 named directory. If sucessful, returns 0; otherwise an error message |
|
436 is printed.") |
1389
|
437 { |
|
438 Octave_object retval; |
|
439 |
1401
|
440 if (args.length () == 1) |
1389
|
441 { |
1755
|
442 string dirname = args(0).string_value (); |
1389
|
443 |
1401
|
444 if (error_state) |
1781
|
445 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
446 else |
|
447 { |
1781
|
448 dir_entry dir (oct_tilde_expand (dirname)); |
1389
|
449 |
1401
|
450 if (dir) |
1389
|
451 { |
1781
|
452 string_vector dirlist = dir.read (); |
|
453 retval(0) = dirlist.qsort (); |
1401
|
454 } |
|
455 else |
|
456 { |
1781
|
457 string msg = dir.error (); |
|
458 error ("%s", msg.c_str ()); |
1401
|
459 } |
1389
|
460 } |
|
461 } |
|
462 else |
|
463 print_usage ("readdir"); |
|
464 |
1401
|
465 return retval; |
|
466 } |
|
467 |
|
468 // XXX FIXME XXX -- should probably also allow second arg to specify |
|
469 // mode. |
|
470 |
1488
|
471 DEFUN ("mkdir", Fmkdir, Smkdir, 10, |
1401
|
472 "mkdir (NAME)\n\ |
|
473 \n\ |
|
474 Create the directory named by NAME. If successful, returns 0;\n\ |
|
475 otherwise prints an error message.") |
|
476 { |
|
477 Octave_object retval; |
|
478 |
|
479 int status = 0; |
|
480 |
|
481 if (args.length () == 1) |
|
482 { |
1755
|
483 string dirname = args(0).string_value (); |
1401
|
484 |
|
485 if (error_state) |
1402
|
486 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
487 else |
1401
|
488 { |
1773
|
489 int mkdir_retval = oct_mkdir (oct_tilde_expand (dirname), 0777); |
1489
|
490 |
|
491 if (mkdir_retval < 0) |
|
492 { |
|
493 status = -1; |
|
494 error ("%s", strerror (errno)); |
|
495 } |
1401
|
496 } |
|
497 } |
|
498 else |
|
499 print_usage ("mkdir"); |
|
500 |
|
501 if (status == 0) |
1389
|
502 retval (0) = (double) status; |
1401
|
503 |
|
504 return retval; |
|
505 } |
|
506 |
1488
|
507 DEFUN ("rmdir", Frmdir, Srmdir, 10, |
1401
|
508 "rmdir (NAME)\n\ |
|
509 \n\ |
|
510 Remove the directory named by NAME. If successful, returns 0;\n\ |
|
511 otherwise prints an error message.") |
|
512 { |
|
513 Octave_object retval; |
|
514 |
|
515 int status = 0; |
|
516 |
|
517 if (args.length () == 1) |
|
518 { |
1755
|
519 string dirname = args(0).string_value (); |
1401
|
520 |
|
521 if (error_state) |
1402
|
522 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
523 else |
1401
|
524 { |
1773
|
525 int rmdir_retval = oct_rmdir (oct_tilde_expand (dirname)); |
1489
|
526 |
|
527 if (rmdir_retval < 0) |
|
528 { |
|
529 status = -1; |
|
530 error ("%s", strerror (errno)); |
|
531 } |
1401
|
532 } |
|
533 } |
1389
|
534 else |
1401
|
535 print_usage ("rmdir"); |
|
536 |
|
537 if (status == 0) |
|
538 retval (0) = (double) status; |
|
539 |
|
540 return retval; |
|
541 } |
|
542 |
1488
|
543 DEFUN ("rename", Frename, Srename, 10, |
1401
|
544 "rename (FROM, TO)\n\ |
|
545 \n\ |
|
546 Rename a file. If successful, returns 0;\n\ |
|
547 otherwise prints an error message and returns -1.") |
|
548 { |
|
549 Octave_object retval; |
|
550 |
|
551 int status = 0; |
|
552 |
|
553 if (args.length () == 2) |
|
554 { |
1755
|
555 string from = args(0).string_value (); |
1728
|
556 |
1401
|
557 if (error_state) |
1402
|
558 gripe_wrong_type_arg ("rename", args(0)); |
|
559 else |
1401
|
560 { |
1755
|
561 string to = args(1).string_value (); |
1728
|
562 |
1402
|
563 if (error_state) |
|
564 gripe_wrong_type_arg ("rename", args(1)); |
1773
|
565 else if (oct_rename (from, to) < 0) |
1402
|
566 { |
|
567 status = -1; |
|
568 error ("%s", strerror (errno)); |
|
569 } |
1401
|
570 } |
|
571 } |
|
572 else |
|
573 print_usage ("rename"); |
|
574 |
|
575 if (status == 0) |
|
576 retval (0) = (double) status; |
1389
|
577 |
|
578 return retval; |
|
579 } |
|
580 |
523
|
581 /* |
|
582 ;;; Local Variables: *** |
|
583 ;;; mode: C++ *** |
|
584 ;;; page-delimiter: "^/\\*" *** |
|
585 ;;; End: *** |
|
586 */ |