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