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