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