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