2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2926
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
2926
|
29 #include <string> |
|
30 |
|
31 #ifdef HAVE_UNISTD_H |
|
32 #ifdef HAVE_SYS_TYPES_H |
|
33 #include <sys/types.h> |
|
34 #endif |
|
35 #include <unistd.h> |
|
36 #endif |
|
37 |
5872
|
38 #include "file-ops.h" |
3069
|
39 #include "lo-error.h" |
2926
|
40 #include "pathlen.h" |
6123
|
41 #include "lo-sysdep.h" |
2926
|
42 |
3504
|
43 std::string |
2926
|
44 octave_getcwd (void) |
|
45 { |
3504
|
46 std::string retval; |
3069
|
47 |
2926
|
48 char buf[MAXPATHLEN]; |
|
49 |
3069
|
50 char *tmp = 0; |
|
51 |
2926
|
52 #if defined (__EMX__) |
3069
|
53 tmp = _getcwd2 (buf, MAXPATHLEN); |
3112
|
54 #elif defined (HAVE_GETCWD) |
|
55 tmp = getcwd (buf, MAXPATHLEN); |
3069
|
56 #elif defined (HAVE_GETWD) |
|
57 tmp = getwd (buf); |
2926
|
58 #endif |
|
59 |
|
60 if (tmp) |
|
61 retval = tmp; |
3069
|
62 else |
|
63 (*current_liboctave_error_handler) ("unable to find current directory"); |
2926
|
64 |
|
65 return retval; |
|
66 } |
|
67 |
|
68 int |
5872
|
69 octave_chdir (const std::string& path_arg) |
2926
|
70 { |
5872
|
71 std::string path = file_ops::tilde_expand (path_arg); |
|
72 |
2926
|
73 #if defined (__EMX__) |
|
74 int retval = -1; |
|
75 |
|
76 char *tmp_path = strsave (path.c_str ()); |
|
77 |
|
78 if (path.length () == 2 && path[1] == ':') |
|
79 { |
|
80 char *upper_case_dir_name = strupr (tmp_path); |
|
81 _chdrive (upper_case_dir_name[0]); |
|
82 if (_getdrive () == upper_case_dir_name[0]) |
|
83 retval = _chdir2 ("/"); |
|
84 } |
|
85 else |
|
86 retval = _chdir2 (tmp_path); |
|
87 |
|
88 delete [] tmp_path; |
|
89 |
|
90 return retval; |
|
91 #else |
|
92 return chdir (path.c_str ()); |
|
93 #endif |
|
94 } |
|
95 |
6123
|
96 #if defined (_MSC_VER) |
6093
|
97 |
|
98 // FIXME -- it would probably be better to adapt the versions of |
|
99 // opendir, readdir, and closedir from Emacs as they appear to be more |
|
100 // complete implementations (do the functions below work for network |
|
101 // paths, for example)? We can probably get along without rewinddir. |
|
102 |
|
103 #include <windows.h> |
|
104 |
6123
|
105 struct __DIR |
6093
|
106 { |
|
107 HANDLE hnd; |
|
108 WIN32_FIND_DATA fd; |
|
109 int dirty; |
|
110 struct direct d; |
6123
|
111 const char *current; |
|
112 }; |
6093
|
113 |
|
114 DIR * |
|
115 opendir (const char *name) |
|
116 { |
|
117 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); |
|
118 static char buffer[MAX_PATH]; |
|
119 |
|
120 strncpy (buffer, name, MAX_PATH); |
|
121 strncat (buffer, "\\*", MAX_PATH); |
|
122 d->current = buffer; |
|
123 d->hnd = FindFirstFile (buffer, &(d->fd)); |
|
124 if (d->hnd == INVALID_HANDLE_VALUE) |
|
125 return 0; |
|
126 d->dirty = 1; |
|
127 return d; |
|
128 } |
|
129 |
|
130 void |
6123
|
131 rewinddir (DIR *d) |
6093
|
132 { |
|
133 if (d->hnd != INVALID_HANDLE_VALUE) |
|
134 FindClose (d->hnd); |
|
135 d->hnd = FindFirstFile (d->current, &(d->fd)); |
|
136 d->dirty = 1; |
|
137 } |
|
138 |
|
139 void |
|
140 closedir (DIR *d) |
|
141 { |
|
142 if (d->hnd != INVALID_HANDLE_VALUE) |
|
143 FindClose (d->hnd); |
|
144 free (d); |
|
145 } |
|
146 |
|
147 struct direct * |
|
148 readdir (DIR *d) |
|
149 { |
|
150 if (! d->dirty) |
|
151 { |
|
152 if (! FindNextFile(d->hnd, &(d->fd))) |
|
153 return 0; |
|
154 } |
|
155 d->d.d_name = d->fd.cFileName; |
|
156 d->dirty = 0; |
|
157 return &(d->d); |
|
158 } |
|
159 |
|
160 #endif |
|
161 |
2926
|
162 /* |
|
163 ;;; Local Variables: *** |
|
164 ;;; mode: C++ *** |
|
165 ;;; End: *** |
|
166 */ |