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