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