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 |
523
|
47 #include <sys/types.h> |
|
48 #ifdef HAVE_UNISTD_H |
|
49 #include <unistd.h> |
|
50 #endif |
|
51 #include <strstream.h> |
|
52 #include <sys/param.h> |
|
53 |
|
54 // This mess suggested by the autoconf manual. |
|
55 // unistd.h defines _POSIX_VERSION on POSIX.1 systems. |
958
|
56 #if defined (HAVE_DIRENT_H) || defined (_POSIX_VERSION) |
523
|
57 #include <dirent.h> |
|
58 #define NLENGTH(dirent) (strlen((dirent)->d_name)) |
958
|
59 #else |
523
|
60 #define dirent direct |
|
61 #define NLENGTH(dirent) ((dirent)->d_namlen) |
958
|
62 #if defined (HAVE_SYS_NDIR_H) |
523
|
63 #include <sys/ndir.h> |
958
|
64 #endif |
|
65 #if defined (HAVE_SYS_DIR_H) |
523
|
66 #include <sys/dir.h> |
958
|
67 #endif |
|
68 #if defined (HAVE_NDIR_H) |
523
|
69 #include <ndir.h> |
958
|
70 #endif |
|
71 #endif |
523
|
72 |
|
73 #include "statdefs.h" |
|
74 #include "procstream.h" |
|
75 #include "tree-const.h" |
1328
|
76 #include "tree-plot.h" |
523
|
77 #include "oct-obj.h" |
1111
|
78 #include "sysdep.h" |
523
|
79 #include "octave.h" |
|
80 #include "dirfns.h" |
|
81 #include "pager.h" |
|
82 #include "error.h" |
|
83 #include "utils.h" |
|
84 #include "defun.h" |
|
85 |
|
86 extern "C" |
|
87 { |
|
88 #include <readline/tilde.h> |
|
89 extern char *strerror (int); |
|
90 } |
|
91 |
|
92 #ifndef MAXPATHLEN |
|
93 #define MAXPATHLEN 1024 |
|
94 #endif |
|
95 |
|
96 // Temp storage for a path. |
|
97 static char tdir[MAXPATHLEN]; |
|
98 |
|
99 // Non-zero means follow symbolic links that point to directories just |
|
100 // as if they are real directories. |
|
101 static int follow_symbolic_links = 1; |
|
102 |
|
103 // Non-zero means that pwd always give verbatim directory, regardless |
|
104 // of symbolic link following. |
|
105 static int verbatim_pwd = 1; |
|
106 |
661
|
107 // Remove the last N directories from PATH. Do not PATH blank. |
|
108 // PATH must contain enough space for MAXPATHLEN characters. |
|
109 |
523
|
110 void |
|
111 pathname_backup (char *path, int n) |
|
112 { |
|
113 register char *p; |
|
114 |
|
115 if (! *path) |
|
116 return; |
|
117 |
|
118 p = path + (strlen (path) - 1); |
|
119 |
|
120 while (n--) |
|
121 { |
|
122 while (*p == '/' && p != path) |
|
123 p--; |
|
124 |
|
125 while (*p != '/' && p != path) |
|
126 p--; |
|
127 |
|
128 *++p = '\0'; |
|
129 } |
|
130 } |
|
131 |
661
|
132 // Return a pretty pathname. If the first part of the pathname is the |
|
133 // same as $HOME, then replace that with `~'. |
|
134 |
523
|
135 char * |
|
136 polite_directory_format (char *name) |
|
137 { |
|
138 int l = home_directory ? strlen (home_directory) : 0; |
|
139 |
|
140 if (l > 1 && strncmp (home_directory, name, l) == 0 |
|
141 && (! name[l] || name[l] == '/')) |
|
142 { |
|
143 strcpy (tdir + 1, name + l); |
|
144 tdir[0] = '~'; |
|
145 return (tdir); |
|
146 } |
|
147 else |
|
148 return name; |
|
149 } |
|
150 |
661
|
151 // Return 1 if STRING contains an absolute pathname, else 0. |
|
152 |
523
|
153 int |
|
154 absolute_pathname (const char *string) |
|
155 { |
|
156 if (! string || ! *string) |
|
157 return 0; |
|
158 |
|
159 if (*string == '/') |
|
160 return 1; |
|
161 |
|
162 if (*string++ == '.') |
|
163 { |
|
164 if ((! *string) || *string == '/') |
|
165 return 1; |
|
166 |
|
167 if (*string++ == '.') |
|
168 if (! *string || *string == '/') |
|
169 return 1; |
|
170 } |
|
171 return 0; |
|
172 } |
|
173 |
661
|
174 // Return 1 if STRING is an absolute program name; it is absolute if |
|
175 // it contains any slashes. This is used to decide whether or not to |
|
176 // look up through $PATH. |
|
177 |
523
|
178 int |
|
179 absolute_program (const char *string) |
|
180 { |
|
181 return (strchr (string, '/') != 0); |
|
182 } |
|
183 |
661
|
184 // Return the `basename' of the pathname in STRING (the stuff after |
|
185 // the last '/'). If STRING is not a full pathname, simply return it. |
|
186 |
523
|
187 char * |
|
188 base_pathname (char *string) |
|
189 { |
|
190 char *p = strrchr (string, '/'); |
|
191 |
|
192 if (! absolute_pathname (string)) |
|
193 return (string); |
|
194 |
|
195 if (p) |
|
196 return (++p); |
|
197 else |
|
198 return (string); |
|
199 } |
|
200 |
661
|
201 // Turn STRING (a pathname) into an absolute pathname, assuming that |
|
202 // DOT_PATH contains the symbolic location of '.'. This always |
|
203 // returns a new string, even if STRING was an absolute pathname to |
|
204 // begin with. |
|
205 |
523
|
206 char * |
|
207 make_absolute (const char *string, const char *dot_path) |
|
208 { |
|
209 static char current_path[MAXPATHLEN]; |
|
210 register char *cp; |
|
211 |
|
212 if (! dot_path || *string == '/') |
|
213 return strsave (string); |
|
214 |
|
215 strcpy (current_path, dot_path); |
|
216 |
|
217 if (! current_path[0]) |
|
218 strcpy (current_path, "./"); |
|
219 |
|
220 cp = current_path + (strlen (current_path) - 1); |
|
221 |
|
222 if (*cp++ != '/') |
|
223 *cp++ = '/'; |
|
224 |
|
225 *cp = '\0'; |
|
226 |
|
227 while (*string) |
|
228 { |
|
229 if (*string == '.') |
|
230 { |
|
231 if (! string[1]) |
|
232 return strsave (current_path); |
|
233 |
|
234 if (string[1] == '/') |
|
235 { |
|
236 string += 2; |
|
237 continue; |
|
238 } |
|
239 |
|
240 if (string[1] == '.' && (string[2] == '/' || ! string[2])) |
|
241 { |
|
242 string += 2; |
|
243 |
|
244 if (*string) |
|
245 string++; |
|
246 |
|
247 pathname_backup (current_path, 1); |
|
248 cp = current_path + strlen (current_path); |
|
249 continue; |
|
250 } |
|
251 } |
|
252 |
|
253 while (*string && *string != '/') |
|
254 *cp++ = *string++; |
|
255 |
|
256 if (*string) |
|
257 *cp++ = *string++; |
|
258 |
|
259 *cp = '\0'; |
|
260 } |
|
261 return strsave (current_path); |
|
262 } |
|
263 |
661
|
264 // Has file `A' been modified after time `T'? |
|
265 // |
|
266 // case: |
|
267 // |
|
268 // a newer than t returns 1 |
|
269 // a older than t returns 0 |
|
270 // stat on a fails returns -1 |
|
271 |
523
|
272 int |
|
273 is_newer (const char *fa, time_t t) |
|
274 { |
|
275 struct stat fa_sb; |
|
276 register int fa_stat; |
|
277 register int status = 0; |
|
278 |
|
279 fa_stat = stat (fa, &fa_sb); |
|
280 if (fa_stat != 0) |
|
281 status = -1; |
|
282 |
|
283 if (status != 0) |
|
284 return status; |
|
285 |
|
286 return (fa_sb.st_mtime > t); |
|
287 } |
|
288 |
661
|
289 // Return a consed string which is the current working directory. |
|
290 // FOR_WHOM is the name of the caller for error printing. |
|
291 |
523
|
292 char * |
|
293 get_working_directory (const char *for_whom) |
|
294 { |
|
295 if (! follow_symbolic_links) |
|
296 { |
|
297 if (the_current_working_directory) |
|
298 delete [] the_current_working_directory; |
|
299 |
|
300 the_current_working_directory = 0; |
|
301 } |
|
302 |
|
303 if (! the_current_working_directory) |
|
304 { |
|
305 char *directory; |
|
306 |
|
307 the_current_working_directory = new char [MAXPATHLEN]; |
1111
|
308 directory = octave_getcwd (the_current_working_directory, MAXPATHLEN); |
523
|
309 if (! directory) |
|
310 { |
|
311 message (for_whom, the_current_working_directory); |
|
312 delete [] the_current_working_directory; |
|
313 the_current_working_directory = 0; |
|
314 return 0; |
|
315 } |
|
316 } |
|
317 |
|
318 return the_current_working_directory; |
|
319 } |
|
320 |
661
|
321 // Do the work of changing to the directory NEWDIR. Handle symbolic |
|
322 // link following, etc. |
|
323 |
523
|
324 static int |
|
325 change_to_directory (const char *newdir) |
|
326 { |
|
327 char *t; |
|
328 |
|
329 if (follow_symbolic_links) |
|
330 { |
|
331 if (! the_current_working_directory) |
|
332 get_working_directory ("cd_links"); |
|
333 |
|
334 if (the_current_working_directory) |
|
335 t = make_absolute (newdir, the_current_working_directory); |
|
336 else |
|
337 t = strsave (newdir); |
|
338 |
661
|
339 // Get rid of trailing `/'. |
523
|
340 { |
|
341 register int len_t = strlen (t); |
|
342 if (len_t > 1) |
|
343 { |
|
344 --len_t; |
|
345 if (t[len_t] == '/') |
|
346 t[len_t] = '\0'; |
|
347 } |
|
348 } |
|
349 |
1111
|
350 if (octave_chdir (t) < 0) |
523
|
351 { |
|
352 delete [] t; |
|
353 return 0; |
|
354 } |
|
355 |
|
356 if (the_current_working_directory) |
|
357 strcpy (the_current_working_directory, t); |
|
358 |
|
359 delete [] t; |
|
360 return 1; |
|
361 } |
|
362 else |
|
363 { |
1111
|
364 if (octave_chdir (newdir) < 0) |
523
|
365 return 0; |
|
366 else |
|
367 return 1; |
|
368 } |
|
369 } |
|
370 |
1328
|
371 static int |
|
372 octave_change_to_directory (const char *newdir) |
|
373 { |
|
374 int cd_ok = change_to_directory (newdir); |
|
375 |
|
376 if (cd_ok) |
|
377 do_external_plotter_cd (newdir); |
|
378 else |
|
379 error ("%s: %s", newdir, strerror (errno)); |
|
380 |
|
381 return cd_ok; |
|
382 } |
|
383 |
523
|
384 DEFUN_TEXT ("cd", Fcd, Scd, 2, 1, |
|
385 "cd [dir]\n\ |
|
386 \n\ |
|
387 change current working directory\n\ |
|
388 if no arguments are given, the current directory is changed to the\n\ |
|
389 users home directory") |
|
390 { |
|
391 Octave_object retval; |
|
392 |
|
393 DEFINE_ARGV("cd"); |
|
394 |
|
395 if (argc > 1) |
|
396 { |
|
397 static char *dirname = 0; |
|
398 |
|
399 if (dirname) |
|
400 free (dirname); |
|
401 |
|
402 dirname = tilde_expand (argv[1]); |
|
403 |
1328
|
404 if (dirname && ! octave_change_to_directory (dirname)) |
523
|
405 { |
|
406 DELETE_ARGV; |
|
407 return retval; |
|
408 } |
|
409 } |
|
410 else |
|
411 { |
1328
|
412 if (! home_directory || ! octave_change_to_directory (home_directory)) |
523
|
413 { |
|
414 DELETE_ARGV; |
|
415 return retval; |
|
416 } |
|
417 } |
|
418 |
|
419 char *directory = get_working_directory ("cd"); |
|
420 tree_constant *dir = new tree_constant (directory); |
|
421 bind_builtin_variable ("PWD", dir, 1); |
|
422 |
|
423 DELETE_ARGV; |
|
424 |
|
425 return retval; |
|
426 } |
|
427 |
611
|
428 DEFALIAS (chdir, cd); |
|
429 |
661
|
430 // Get a directory listing. |
|
431 |
523
|
432 DEFUN_TEXT ("ls", Fls, Sls, -1, 1, |
|
433 "ls [options]\n\ |
|
434 \n\ |
|
435 print a directory listing") |
|
436 { |
|
437 Octave_object retval; |
|
438 |
|
439 DEFINE_ARGV("ls"); |
|
440 |
|
441 ostrstream ls_buf; |
|
442 |
|
443 ls_buf << "ls -C "; |
|
444 for (int i = 1; i < argc; i++) |
1158
|
445 { |
|
446 char *tmp = tilde_expand (argv[i]); |
|
447 ls_buf << tmp << " "; |
|
448 free (tmp); |
|
449 } |
523
|
450 |
|
451 ls_buf << ends; |
|
452 |
|
453 char *ls_command = ls_buf.str (); |
|
454 |
|
455 iprocstream cmd (ls_command); |
|
456 |
|
457 char ch; |
|
458 ostrstream output_buf; |
|
459 while (cmd.get (ch)) |
|
460 output_buf.put (ch); |
|
461 |
|
462 output_buf << ends; |
|
463 |
|
464 maybe_page_output (output_buf); |
|
465 |
|
466 delete [] ls_command; |
|
467 |
|
468 DELETE_ARGV; |
|
469 |
|
470 return retval; |
|
471 } |
|
472 |
549
|
473 DEFALIAS (dir, ls); |
|
474 |
523
|
475 DEFUN ("pwd", Fpwd, Spwd, 1, 0, |
|
476 "pwd (): print current working directory") |
|
477 { |
|
478 Octave_object retval; |
|
479 char *directory; |
|
480 |
|
481 if (verbatim_pwd) |
|
482 { |
|
483 char *buffer = new char [MAXPATHLEN]; |
1111
|
484 directory = octave_getcwd (buffer, MAXPATHLEN); |
523
|
485 |
|
486 if (!directory) |
|
487 { |
|
488 warning ("pwd: can't find working directory!"); |
|
489 delete buffer; |
|
490 } |
|
491 } |
|
492 else |
|
493 { |
|
494 directory = get_working_directory ("pwd"); |
|
495 } |
|
496 |
|
497 if (directory) |
|
498 { |
|
499 char *s = strconcat (directory, "\n"); |
|
500 retval = s; |
|
501 delete [] s; |
|
502 } |
|
503 return retval; |
|
504 } |
|
505 |
|
506 /* |
|
507 ;;; Local Variables: *** |
|
508 ;;; mode: C++ *** |
|
509 ;;; page-delimiter: "^/\\*" *** |
|
510 ;;; End: *** |
|
511 */ |