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