1765
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1765
|
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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cerrno> |
1802
|
28 #include <cstdio> |
|
29 #include <cstdlib> |
1765
|
30 #include <cstring> |
|
31 |
2443
|
32 #ifdef HAVE_SYS_TYPES_H |
1765
|
33 #include <sys/types.h> |
2443
|
34 #endif |
2926
|
35 |
|
36 #ifdef HAVE_UNISTD_H |
1765
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
|
40 #include "file-ops.h" |
2926
|
41 #include "oct-env.h" |
2934
|
42 #include "oct-passwd.h" |
1775
|
43 #include "statdefs.h" |
2926
|
44 #include "str-vec.h" |
1765
|
45 |
2937
|
46 #define NOT_SUPPORTED(nm) \ |
|
47 nm ## ": not supported on this system" |
|
48 |
2433
|
49 // We provide a replacement for mkdir(). |
|
50 |
1765
|
51 int |
2926
|
52 file_ops::mkdir (const string& name, mode_t mode) |
1765
|
53 { |
2937
|
54 string msg; |
|
55 return mkdir (name, mode, msg); |
1765
|
56 } |
|
57 |
2668
|
58 int |
2926
|
59 file_ops::mkdir (const string& name, mode_t mode, string& msg) |
2668
|
60 { |
|
61 msg = string (); |
|
62 |
2937
|
63 int status = -1; |
|
64 |
|
65 #if defined (HAVE_MKDIR) |
|
66 status = ::mkdir (name.c_str (), mode); |
2668
|
67 |
|
68 if (status < 0) |
2926
|
69 msg = ::strerror (errno); |
2937
|
70 #else |
|
71 msg = NOT_SUPPORTED ("mkdir"); |
|
72 #endif |
2668
|
73 |
|
74 return status; |
|
75 } |
|
76 |
2433
|
77 // I don't know how to emulate this on systems that don't provide it. |
|
78 |
1765
|
79 int |
2926
|
80 file_ops::mkfifo (const string& name, mode_t mode) |
1765
|
81 { |
2937
|
82 string msg; |
|
83 return mkfifo (name, mode, msg); |
1765
|
84 } |
|
85 |
2668
|
86 int |
2926
|
87 file_ops::mkfifo (const string& name, mode_t mode, string& msg) |
2668
|
88 { |
|
89 msg = string (); |
|
90 |
2937
|
91 int status = -1; |
|
92 |
2668
|
93 #if defined (HAVE_MKFIFO) |
2937
|
94 status = ::mkfifo (name.c_str (), mode); |
2668
|
95 |
|
96 if (status < 0) |
2926
|
97 msg = ::strerror (errno); |
2937
|
98 #else |
|
99 msg = NOT_SUPPORTED ("mkfifo"); |
|
100 #endif |
2668
|
101 |
|
102 return status; |
|
103 } |
|
104 |
2433
|
105 // We provide a replacement for rename(). |
|
106 |
1765
|
107 int |
2926
|
108 file_ops::rename (const string& from, const string& to) |
1765
|
109 { |
2937
|
110 string msg; |
|
111 return rename (from, to, msg); |
1765
|
112 } |
|
113 |
2668
|
114 int |
2926
|
115 file_ops::rename (const string& from, const string& to, string& msg) |
2668
|
116 { |
2937
|
117 int status = -1; |
|
118 |
2668
|
119 msg = string (); |
|
120 |
2937
|
121 #if defined (HAVE_RENAME) |
|
122 status = ::rename (from.c_str (), to.c_str ()); |
2668
|
123 |
|
124 if (status < 0) |
2926
|
125 msg = ::strerror (errno); |
2937
|
126 #else |
|
127 msg = NOT_SUPPORTED ("rename"); |
|
128 #endif |
2668
|
129 |
|
130 return status; |
|
131 } |
|
132 |
2433
|
133 // We provide a replacement for rmdir(). |
|
134 |
1765
|
135 int |
2926
|
136 file_ops::rmdir (const string& name) |
1765
|
137 { |
2937
|
138 string msg; |
|
139 return rmdir (name, msg); |
1765
|
140 } |
|
141 |
2668
|
142 int |
2926
|
143 file_ops::rmdir (const string& name, string& msg) |
2668
|
144 { |
|
145 msg = string (); |
|
146 |
2937
|
147 int status = -1; |
|
148 |
|
149 #if defined (HAVE_RMDIR) |
|
150 status = ::rmdir (name.c_str ()); |
2668
|
151 |
|
152 if (status < 0) |
2926
|
153 msg = ::strerror (errno); |
2937
|
154 #else |
|
155 msg = NOT_SUPPORTED ("rmdir"); |
|
156 #endif |
2668
|
157 |
|
158 return status; |
|
159 } |
|
160 |
2433
|
161 // We provide a replacement for tempnam(). |
|
162 |
1802
|
163 string |
2937
|
164 file_ops::tempnam (const string& dir, const string& pfx) |
1802
|
165 { |
2937
|
166 string msg; |
|
167 return tempnam (dir, pfx, msg); |
|
168 } |
|
169 |
|
170 string |
|
171 file_ops::tempnam (const string& dir, const string& pfx, string& msg) |
|
172 { |
|
173 msg = string (); |
|
174 |
1802
|
175 string retval; |
2937
|
176 |
|
177 const char *pdir = dir.empty () ? 0 : dir.c_str (); |
1802
|
178 |
2937
|
179 const char *ppfx = pfx.empty () ? 0 : pfx.c_str (); |
|
180 |
|
181 char *tmp = ::tempnam (pdir, ppfx); |
1802
|
182 |
|
183 if (tmp) |
|
184 { |
|
185 retval = tmp; |
|
186 |
2937
|
187 ::free (tmp); |
1802
|
188 } |
|
189 else |
2937
|
190 msg = ::strerror (errno); |
1802
|
191 |
|
192 return retval; |
|
193 } |
|
194 |
2926
|
195 // If NAME has a leading ~ or ~user, Unix-style, expand it to the |
|
196 // user's home directory. If no ~, or no <pwd.h>, just return NAME. |
|
197 |
|
198 // Mostly stolen from kpathsea. Readline also has a more complicated |
|
199 // tilde-expand function, but we can probalby get by with something a |
|
200 // bit simpler. |
|
201 |
|
202 // XXX FIXME XXX |
|
203 #define DIR_SEP_CHAR '/' |
|
204 |
|
205 string |
|
206 file_ops::tilde_expand (const string& name) |
|
207 { |
|
208 string expansion = name; |
|
209 |
|
210 // If no leading tilde, do nothing. |
|
211 |
|
212 size_t beg = name.find_first_not_of (" \t"); |
|
213 |
|
214 if (beg != NPOS && name[beg] == '~') |
|
215 { |
|
216 // If `~' or `~/', use $HOME if it exists, or `.' if it doesn't. |
|
217 |
|
218 // If `~user' or `~user/', look up user in the passwd database. |
|
219 |
|
220 size_t len = name.length (); |
|
221 |
|
222 if (beg == len-1 || name[beg+1] == DIR_SEP_CHAR) |
|
223 { |
|
224 string home = octave_env::get_home_directory (); |
|
225 |
|
226 if (home.empty ()) |
|
227 home = "."; |
|
228 |
|
229 expansion = name.substr (0, beg) + home; |
|
230 |
|
231 if (beg < len) |
|
232 expansion.append (name.substr (beg+1)); |
|
233 } |
|
234 else |
|
235 { |
|
236 size_t end = name.find (DIR_SEP_CHAR, beg); |
|
237 |
|
238 size_t len = end; |
|
239 |
|
240 if (len != NPOS) |
|
241 len -= beg + 1; |
|
242 |
|
243 string user = name.substr (beg+1, len); |
|
244 |
2934
|
245 octave_passwd pw = octave_passwd::getpwnam (user); |
2926
|
246 |
|
247 // If no such user, just use `.'. |
|
248 |
2937
|
249 string home = pw ? string (".") : pw.dir (); |
2926
|
250 |
|
251 expansion = string (" ", beg) + home; |
|
252 |
|
253 if (end != NPOS) |
|
254 expansion.append (name.substr (end)); |
|
255 } |
|
256 } |
|
257 |
|
258 return expansion; |
|
259 } |
|
260 |
|
261 // A vector version of the above. |
|
262 |
|
263 string_vector |
|
264 file_ops::tilde_expand (const string_vector& names) |
|
265 { |
|
266 string_vector retval; |
|
267 |
|
268 int n = names.length (); |
|
269 |
|
270 retval.resize (n); |
|
271 |
|
272 for (int i = 0; i < n; i++) |
|
273 retval[i] = file_ops::tilde_expand (names[i]); |
|
274 |
|
275 return retval; |
|
276 } |
1802
|
277 |
1765
|
278 int |
2926
|
279 file_ops::umask (mode_t mode) |
1765
|
280 { |
|
281 #if defined (HAVE_UMASK) |
2926
|
282 return ::umask (mode); |
1765
|
283 #else |
|
284 return 0; |
|
285 #endif |
|
286 } |
|
287 |
1773
|
288 int |
2926
|
289 file_ops::unlink (const string& name) |
1773
|
290 { |
2937
|
291 string msg; |
|
292 return unlink (name, msg); |
1773
|
293 } |
|
294 |
2668
|
295 int |
2937
|
296 file_ops::unlink (const string& name, string& msg) |
2668
|
297 { |
2937
|
298 msg = string (); |
2668
|
299 |
2937
|
300 int status = -1; |
|
301 |
|
302 #if defined (HAVE_UNLINK) |
|
303 status = ::unlink (name.c_str ()); |
2668
|
304 |
|
305 if (status < 0) |
2937
|
306 msg = ::strerror (errno); |
|
307 #else |
|
308 msg = NOT_SUPPORTED ("unlink"); |
|
309 #endif |
2668
|
310 |
|
311 return status; |
|
312 } |
|
313 |
1765
|
314 /* |
|
315 ;;; Local Variables: *** |
|
316 ;;; mode: C++ *** |
|
317 ;;; End: *** |
|
318 */ |