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" |
|
41 |
3504
|
42 std::string |
2926
|
43 octave_getcwd (void) |
|
44 { |
3504
|
45 std::string retval; |
3069
|
46 |
2926
|
47 char buf[MAXPATHLEN]; |
|
48 |
3069
|
49 char *tmp = 0; |
|
50 |
2926
|
51 #if defined (__EMX__) |
3069
|
52 tmp = _getcwd2 (buf, MAXPATHLEN); |
3112
|
53 #elif defined (HAVE_GETCWD) |
|
54 tmp = getcwd (buf, MAXPATHLEN); |
3069
|
55 #elif defined (HAVE_GETWD) |
|
56 tmp = getwd (buf); |
2926
|
57 #endif |
|
58 |
|
59 if (tmp) |
|
60 retval = tmp; |
3069
|
61 else |
|
62 (*current_liboctave_error_handler) ("unable to find current directory"); |
2926
|
63 |
|
64 return retval; |
|
65 } |
|
66 |
|
67 int |
5872
|
68 octave_chdir (const std::string& path_arg) |
2926
|
69 { |
5872
|
70 std::string path = file_ops::tilde_expand (path_arg); |
|
71 |
2926
|
72 #if defined (__EMX__) |
|
73 int retval = -1; |
|
74 |
|
75 char *tmp_path = strsave (path.c_str ()); |
|
76 |
|
77 if (path.length () == 2 && path[1] == ':') |
|
78 { |
|
79 char *upper_case_dir_name = strupr (tmp_path); |
|
80 _chdrive (upper_case_dir_name[0]); |
|
81 if (_getdrive () == upper_case_dir_name[0]) |
|
82 retval = _chdir2 ("/"); |
|
83 } |
|
84 else |
|
85 retval = _chdir2 (tmp_path); |
|
86 |
|
87 delete [] tmp_path; |
|
88 |
|
89 return retval; |
|
90 #else |
|
91 return chdir (path.c_str ()); |
|
92 #endif |
|
93 } |
|
94 |
6093
|
95 #ifdef _MSC_VER |
|
96 |
|
97 // FIXME -- it would probably be better to adapt the versions of |
|
98 // opendir, readdir, and closedir from Emacs as they appear to be more |
|
99 // complete implementations (do the functions below work for network |
|
100 // paths, for example)? We can probably get along without rewinddir. |
|
101 |
|
102 #include <windows.h> |
|
103 |
|
104 struct direct |
|
105 { |
|
106 char *d_name; |
|
107 }; |
|
108 |
|
109 typedef struct |
|
110 { |
|
111 HANDLE hnd; |
|
112 WIN32_FIND_DATA fd; |
|
113 int dirty; |
|
114 struct direct d; |
|
115 const char* current; |
|
116 } DIR; |
|
117 |
|
118 DIR * |
|
119 opendir (const char *name) |
|
120 { |
|
121 DIR *d = static_cast<DIR *> (malloc (sizeof (DIR))); |
|
122 static char buffer[MAX_PATH]; |
|
123 |
|
124 strncpy (buffer, name, MAX_PATH); |
|
125 strncat (buffer, "\\*", MAX_PATH); |
|
126 d->current = buffer; |
|
127 d->hnd = FindFirstFile (buffer, &(d->fd)); |
|
128 if (d->hnd == INVALID_HANDLE_VALUE) |
|
129 return 0; |
|
130 d->dirty = 1; |
|
131 return d; |
|
132 } |
|
133 |
|
134 void |
|
135 rewinddir (DIR* d) |
|
136 { |
|
137 if (d->hnd != INVALID_HANDLE_VALUE) |
|
138 FindClose (d->hnd); |
|
139 d->hnd = FindFirstFile (d->current, &(d->fd)); |
|
140 d->dirty = 1; |
|
141 } |
|
142 |
|
143 void |
|
144 closedir (DIR *d) |
|
145 { |
|
146 if (d->hnd != INVALID_HANDLE_VALUE) |
|
147 FindClose (d->hnd); |
|
148 free (d); |
|
149 } |
|
150 |
|
151 struct direct * |
|
152 readdir (DIR *d) |
|
153 { |
|
154 if (! d->dirty) |
|
155 { |
|
156 if (! FindNextFile(d->hnd, &(d->fd))) |
|
157 return 0; |
|
158 } |
|
159 d->d.d_name = d->fd.cFileName; |
|
160 d->dirty = 0; |
|
161 return &(d->d); |
|
162 } |
|
163 |
|
164 #endif |
|
165 |
2926
|
166 /* |
|
167 ;;; Local Variables: *** |
|
168 ;;; mode: C++ *** |
|
169 ;;; End: *** |
|
170 */ |