523
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
523
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
523
|
20 |
|
21 */ |
|
22 |
661
|
23 /* |
|
24 |
|
25 The functions listed below were adapted from a similar functions |
|
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
27 Free Software Foundation, Inc. |
|
28 |
|
29 polite_directory_format absolute_pathname |
1804
|
30 base_pathname |
661
|
31 make_absolute pathname_backup |
|
32 change_to_directory get_working_directory |
|
33 |
|
34 */ |
|
35 |
523
|
36 #ifdef HAVE_CONFIG_H |
1192
|
37 #include <config.h> |
523
|
38 #endif |
|
39 |
1341
|
40 #include <cerrno> |
|
41 #include <cstdio> |
|
42 #include <cstddef> |
|
43 #include <cstdlib> |
|
44 #include <cstring> |
|
45 |
1728
|
46 #include <string> |
|
47 |
523
|
48 #include <strstream.h> |
|
49 |
1832
|
50 #ifdef HAVE_UNISTD_H |
2442
|
51 #ifdef HAVE_SYS_TYPES_H |
1832
|
52 #include <sys/types.h> |
2442
|
53 #endif |
1832
|
54 #include <unistd.h> |
|
55 #endif |
|
56 |
2495
|
57 #include "oct-glob.h" |
1755
|
58 #include "str-vec.h" |
|
59 |
1355
|
60 #include "defun.h" |
1781
|
61 #include "dir-ops.h" |
1355
|
62 #include "dirfns.h" |
|
63 #include "error.h" |
1766
|
64 #include "file-ops.h" |
1402
|
65 #include "gripes.h" |
1389
|
66 #include "help.h" |
1750
|
67 #include "oct-obj.h" |
1355
|
68 #include "pager.h" |
|
69 #include "pathlen.h" |
|
70 #include "procstream.h" |
1750
|
71 #include "pt-plot.h" |
1355
|
72 #include "sysdep.h" |
1750
|
73 #include "toplev.h" |
1449
|
74 #include "unwind-prot.h" |
523
|
75 #include "utils.h" |
1742
|
76 #include "variables.h" |
523
|
77 |
2196
|
78 // The current working directory. |
2204
|
79 string Vcurrent_directory; |
2196
|
80 |
523
|
81 // Non-zero means follow symbolic links that point to directories just |
|
82 // as if they are real directories. |
|
83 static int follow_symbolic_links = 1; |
|
84 |
|
85 // Non-zero means that pwd always give verbatim directory, regardless |
|
86 // of symbolic link following. |
|
87 static int verbatim_pwd = 1; |
|
88 |
661
|
89 // Remove the last N directories from PATH. Do not PATH blank. |
|
90 // PATH must contain enough space for MAXPATHLEN characters. |
|
91 |
1755
|
92 static void |
|
93 pathname_backup (string& path, int n) |
523
|
94 { |
1755
|
95 if (path.empty ()) |
523
|
96 return; |
|
97 |
1755
|
98 size_t i = path.length () - 1; |
523
|
99 |
|
100 while (n--) |
|
101 { |
1755
|
102 while (path[i] == '/' && i > 0) |
|
103 i--; |
523
|
104 |
1755
|
105 while (path[i] != '/' && i > 0) |
|
106 i--; |
523
|
107 |
1755
|
108 i++; |
523
|
109 } |
1755
|
110 |
|
111 path.resize (i); |
523
|
112 } |
|
113 |
661
|
114 // Return a pretty pathname. If the first part of the pathname is the |
|
115 // same as $HOME, then replace that with `~'. |
|
116 |
1755
|
117 string |
|
118 polite_directory_format (const string& name) |
523
|
119 { |
1755
|
120 string retval; |
523
|
121 |
2204
|
122 size_t len = Vhome_directory.length (); |
1755
|
123 |
2204
|
124 if (len > 1 && Vhome_directory.compare (name, 0, len) == 0 |
1755
|
125 && (name.length () == len || name[len] == '/')) |
523
|
126 { |
1755
|
127 retval = "~"; |
|
128 retval.append (name.substr (len)); |
523
|
129 } |
|
130 else |
1755
|
131 retval = name; |
|
132 |
|
133 return retval; |
523
|
134 } |
|
135 |
661
|
136 // Return 1 if STRING contains an absolute pathname, else 0. |
|
137 |
1803
|
138 static int |
1755
|
139 absolute_pathname (const string& s) |
523
|
140 { |
1755
|
141 if (s.empty ()) |
523
|
142 return 0; |
|
143 |
1755
|
144 if (s[0] == '/') |
523
|
145 return 1; |
|
146 |
1755
|
147 if (s[0] == '.') |
523
|
148 { |
1755
|
149 if (s[1] == '\0' || s[1] == '/') |
523
|
150 return 1; |
|
151 |
1755
|
152 if (s[1] == '.') |
|
153 if (s[2] == '\0' || s[2] == '/') |
523
|
154 return 1; |
|
155 } |
1755
|
156 |
523
|
157 return 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 |
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 { |
1785
|
183 if (dot_path.empty () || s[0] == '/' || s.empty ()) |
1755
|
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) |
2204
|
249 Vcurrent_directory = ""; |
523
|
250 |
2204
|
251 if (Vcurrent_directory.empty ()) |
523
|
252 { |
2204
|
253 Vcurrent_directory = octave_getcwd (); |
523
|
254 |
2204
|
255 if (Vcurrent_directory.empty ()) |
1755
|
256 warning ("%s: can't find current directory!", for_whom.c_str ()); |
523
|
257 } |
|
258 |
2204
|
259 return Vcurrent_directory; |
523
|
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 { |
2204
|
272 if (Vcurrent_directory.empty ()) |
523
|
273 get_working_directory ("cd_links"); |
|
274 |
2204
|
275 if (Vcurrent_directory.empty ()) |
1755
|
276 tmp = newdir; |
523
|
277 else |
2204
|
278 tmp = make_absolute (newdir, Vcurrent_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 { |
2204
|
294 Vcurrent_directory = tmp; |
1755
|
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 |
1957
|
315 DEFUN_TEXT (cd, args, , |
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 { |
2086
|
322 octave_value_list retval; |
523
|
323 |
1755
|
324 int argc = args.length () + 1; |
|
325 |
1965
|
326 string_vector argv = args.make_argv ("cd"); |
1755
|
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 { |
2204
|
343 if (Vhome_directory.empty () |
|
344 || ! octave_change_to_directory (Vhome_directory)) |
523
|
345 { |
|
346 return retval; |
|
347 } |
|
348 } |
|
349 |
1755
|
350 string directory = get_working_directory ("cd"); |
2366
|
351 bind_builtin_variable ("PWD", directory, 1); |
523
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
611
|
356 DEFALIAS (chdir, cd); |
|
357 |
661
|
358 // Get a directory listing. |
|
359 |
1965
|
360 static void |
|
361 cleanup_iprocstream (void *p) |
|
362 { |
|
363 delete (iprocstream *) p; |
|
364 } |
|
365 |
1957
|
366 DEFUN_TEXT (ls, args, , |
523
|
367 "ls [options]\n\ |
|
368 \n\ |
|
369 print a directory listing") |
|
370 { |
2086
|
371 octave_value_list retval; |
523
|
372 |
1755
|
373 int argc = args.length () + 1; |
|
374 |
1965
|
375 string_vector argv = args.make_argv ("ls"); |
1755
|
376 |
|
377 if (error_state) |
|
378 return retval; |
523
|
379 |
|
380 ostrstream ls_buf; |
|
381 |
|
382 ls_buf << "ls -C "; |
|
383 for (int i = 1; i < argc; i++) |
1750
|
384 ls_buf << oct_tilde_expand (argv[i]) << " "; |
523
|
385 |
|
386 ls_buf << ends; |
1449
|
387 char *ls_command = ls_buf.str (); |
523
|
388 |
1449
|
389 iprocstream *cmd = new iprocstream (ls_command); |
523
|
390 |
1469
|
391 delete [] ls_command; |
|
392 |
1449
|
393 add_unwind_protect (cleanup_iprocstream, cmd); |
523
|
394 |
1449
|
395 if (cmd && *cmd) |
|
396 { |
|
397 int ch; |
|
398 while ((ch = cmd->get ()) != EOF) |
2095
|
399 octave_stdout << (char) ch; |
1449
|
400 } |
|
401 else |
|
402 error ("couldn't start process for ls!"); |
523
|
403 |
1449
|
404 run_unwind_protect (); |
523
|
405 |
|
406 return retval; |
|
407 } |
|
408 |
549
|
409 DEFALIAS (dir, ls); |
|
410 |
1957
|
411 DEFUN (pwd, , nargout, |
523
|
412 "pwd (): print current working directory") |
|
413 { |
2086
|
414 octave_value_list retval; |
1755
|
415 string directory; |
523
|
416 |
|
417 if (verbatim_pwd) |
|
418 { |
1755
|
419 directory = octave_getcwd (); |
523
|
420 |
1755
|
421 if (directory.empty ()) |
|
422 warning ("pwd: can't find working directory!"); |
523
|
423 } |
|
424 else |
1755
|
425 directory = get_working_directory ("pwd"); |
523
|
426 |
1755
|
427 if (! directory.empty ()) |
523
|
428 { |
1592
|
429 if (nargout == 0) |
2095
|
430 octave_stdout << directory << "\n"; |
1592
|
431 else |
|
432 retval = directory; |
523
|
433 } |
1592
|
434 |
523
|
435 return retval; |
|
436 } |
|
437 |
1957
|
438 DEFUN (readdir, args, , |
1389
|
439 "readdir (NAME)\n\ |
|
440 \n\ |
|
441 Return an array of strings containing the list of all files in the |
1401
|
442 named directory. If sucessful, returns 0; otherwise an error message |
|
443 is printed.") |
1389
|
444 { |
2086
|
445 octave_value_list retval; |
1389
|
446 |
1401
|
447 if (args.length () == 1) |
1389
|
448 { |
1755
|
449 string dirname = args(0).string_value (); |
1389
|
450 |
1401
|
451 if (error_state) |
1781
|
452 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
453 else |
|
454 { |
1781
|
455 dir_entry dir (oct_tilde_expand (dirname)); |
1389
|
456 |
1401
|
457 if (dir) |
1389
|
458 { |
1781
|
459 string_vector dirlist = dir.read (); |
|
460 retval(0) = dirlist.qsort (); |
1401
|
461 } |
|
462 else |
|
463 { |
1781
|
464 string msg = dir.error (); |
|
465 error ("%s", msg.c_str ()); |
1401
|
466 } |
1389
|
467 } |
|
468 } |
|
469 else |
|
470 print_usage ("readdir"); |
|
471 |
1401
|
472 return retval; |
|
473 } |
|
474 |
|
475 // XXX FIXME XXX -- should probably also allow second arg to specify |
|
476 // mode. |
|
477 |
1957
|
478 DEFUN (mkdir, args, , |
1401
|
479 "mkdir (NAME)\n\ |
|
480 \n\ |
|
481 Create the directory named by NAME. If successful, returns 0;\n\ |
|
482 otherwise prints an error message.") |
|
483 { |
2086
|
484 octave_value_list retval; |
1401
|
485 |
|
486 int status = 0; |
|
487 |
|
488 if (args.length () == 1) |
|
489 { |
1755
|
490 string dirname = args(0).string_value (); |
1401
|
491 |
|
492 if (error_state) |
1402
|
493 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
494 else |
1401
|
495 { |
1773
|
496 int mkdir_retval = oct_mkdir (oct_tilde_expand (dirname), 0777); |
1489
|
497 |
|
498 if (mkdir_retval < 0) |
|
499 { |
|
500 status = -1; |
|
501 error ("%s", strerror (errno)); |
|
502 } |
1401
|
503 } |
|
504 } |
|
505 else |
|
506 print_usage ("mkdir"); |
|
507 |
|
508 if (status == 0) |
1389
|
509 retval (0) = (double) status; |
1401
|
510 |
|
511 return retval; |
|
512 } |
|
513 |
1957
|
514 DEFUN (rmdir, args, , |
1401
|
515 "rmdir (NAME)\n\ |
|
516 \n\ |
|
517 Remove the directory named by NAME. If successful, returns 0;\n\ |
|
518 otherwise prints an error message.") |
|
519 { |
2086
|
520 octave_value_list retval; |
1401
|
521 |
|
522 int status = 0; |
|
523 |
|
524 if (args.length () == 1) |
|
525 { |
1755
|
526 string dirname = args(0).string_value (); |
1401
|
527 |
|
528 if (error_state) |
1402
|
529 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
530 else |
1401
|
531 { |
1773
|
532 int rmdir_retval = oct_rmdir (oct_tilde_expand (dirname)); |
1489
|
533 |
|
534 if (rmdir_retval < 0) |
|
535 { |
|
536 status = -1; |
|
537 error ("%s", strerror (errno)); |
|
538 } |
1401
|
539 } |
|
540 } |
1389
|
541 else |
1401
|
542 print_usage ("rmdir"); |
|
543 |
|
544 if (status == 0) |
|
545 retval (0) = (double) status; |
|
546 |
|
547 return retval; |
|
548 } |
|
549 |
1957
|
550 DEFUN (rename, args, , |
1401
|
551 "rename (FROM, TO)\n\ |
|
552 \n\ |
|
553 Rename a file. If successful, returns 0;\n\ |
|
554 otherwise prints an error message and returns -1.") |
|
555 { |
2086
|
556 octave_value_list retval; |
1401
|
557 |
|
558 int status = 0; |
|
559 |
|
560 if (args.length () == 2) |
|
561 { |
1755
|
562 string from = args(0).string_value (); |
1728
|
563 |
1401
|
564 if (error_state) |
1402
|
565 gripe_wrong_type_arg ("rename", args(0)); |
|
566 else |
1401
|
567 { |
1755
|
568 string to = args(1).string_value (); |
1728
|
569 |
1402
|
570 if (error_state) |
|
571 gripe_wrong_type_arg ("rename", args(1)); |
1773
|
572 else if (oct_rename (from, to) < 0) |
1402
|
573 { |
|
574 status = -1; |
|
575 error ("%s", strerror (errno)); |
|
576 } |
1401
|
577 } |
|
578 } |
|
579 else |
|
580 print_usage ("rename"); |
|
581 |
|
582 if (status == 0) |
|
583 retval (0) = (double) status; |
1389
|
584 |
|
585 return retval; |
|
586 } |
|
587 |
2495
|
588 DEFUN (glob, args, , |
|
589 "glob (PATTERN)\n\ |
|
590 \n\ |
2496
|
591 Given an array of strings in PATTERN, return the list of file names\n\ |
|
592 that any of them, or an empty string if no patterns match. Tilde\n\ |
|
593 expansion is performed on each of the patterns before looking for\n\ |
2495
|
594 matching file names.") |
|
595 { |
|
596 octave_value retval; |
|
597 |
|
598 if (args.length () == 1) |
|
599 { |
|
600 string_vector pat = args(0).all_strings (); |
|
601 |
|
602 if (error_state) |
|
603 gripe_wrong_type_arg ("glob", args(0)); |
|
604 else |
|
605 { |
|
606 glob_match pattern (oct_tilde_expand (pat)); |
|
607 |
|
608 string_vector list = pattern.glob (); |
|
609 |
|
610 if (list.empty ()) |
|
611 retval = ""; |
|
612 else |
|
613 retval = list; |
|
614 } |
|
615 } |
|
616 else |
|
617 print_usage ("glob"); |
|
618 |
|
619 return retval; |
|
620 } |
|
621 |
2496
|
622 DEFUN (fnmatch, args, , |
|
623 "fnmatch (PATTERN, STRING)\n\ |
|
624 \n\ |
|
625 Return 1 or zero for each element of STRING that matches any of the\n\ |
|
626 elements of the string array PATTERN, using the rules of filename\n\ |
|
627 pattern matching.") |
|
628 { |
|
629 octave_value retval; |
|
630 |
|
631 if (args.length () == 2) |
|
632 { |
|
633 string_vector pat = args(0).all_strings (); |
|
634 string_vector str = args(1).all_strings (); |
|
635 |
|
636 if (error_state) |
|
637 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
638 else |
|
639 { |
|
640 glob_match pattern (oct_tilde_expand (pat)); |
|
641 |
|
642 Array<bool> tmp = pattern.match (str); |
|
643 |
|
644 int n = tmp.length (); |
|
645 |
|
646 ColumnVector result (n); |
|
647 |
|
648 for (int i = 0; i < n; i++) |
|
649 result(i) = tmp(i); |
|
650 |
|
651 retval = octave_value (result, true); |
|
652 } |
|
653 } |
|
654 else |
|
655 print_usage ("fnmatch"); |
|
656 |
|
657 return retval; |
|
658 } |
|
659 |
2196
|
660 static int |
|
661 pwd (void) |
|
662 { |
|
663 int status = 0; |
|
664 |
|
665 string s = builtin_string_variable ("PWD"); |
|
666 |
|
667 if (s.empty ()) |
|
668 { |
|
669 gripe_invalid_value_specified ("PWD"); |
|
670 status = -1; |
|
671 } |
|
672 else |
|
673 Vcurrent_directory = s; |
|
674 |
|
675 return status; |
|
676 } |
|
677 |
|
678 void |
|
679 symbols_of_dirfns (void) |
|
680 { |
|
681 DEFCONST (PWD, get_working_directory ("initialize_globals"), 0, pwd, |
|
682 "current working directory"); |
|
683 } |
|
684 |
523
|
685 /* |
|
686 ;;; Local Variables: *** |
|
687 ;;; mode: C++ *** |
|
688 ;;; End: *** |
|
689 */ |