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