1765
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 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 |
|
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 |
|
32 #ifdef HAVE_UNISTD_H |
2443
|
33 #ifdef HAVE_SYS_TYPES_H |
1765
|
34 #include <sys/types.h> |
2443
|
35 #endif |
1765
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
2433
|
39 #include "error.h" |
1765
|
40 #include "file-ops.h" |
1802
|
41 #include "lo-error.h" |
1775
|
42 #include "statdefs.h" |
|
43 |
|
44 // These must come after <sys/types.h> and <sys/stat.h>. |
|
45 |
2492
|
46 #include <safe-lstat.h> |
|
47 #include <safe-stat.h> |
1765
|
48 |
|
49 // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid |
|
50 // for initialized objects. If called for an object that is not |
|
51 // initialized, they should throw an exception. |
|
52 |
|
53 bool |
|
54 file_stat::is_blk (void) const |
|
55 { |
2466
|
56 #ifdef S_ISBLK |
1765
|
57 return S_ISBLK (fs_mode); |
2466
|
58 #else |
|
59 return false; |
|
60 #endif |
1765
|
61 } |
|
62 |
|
63 bool |
|
64 file_stat::is_chr (void) const |
|
65 { |
2466
|
66 #ifdef S_ISCHR |
1765
|
67 return S_ISCHR (fs_mode); |
2466
|
68 #else |
|
69 return false; |
|
70 #endif |
1765
|
71 } |
|
72 |
|
73 bool |
|
74 file_stat::is_dir (void) const |
|
75 { |
2466
|
76 #ifdef S_ISDIR |
1765
|
77 return S_ISDIR (fs_mode); |
2466
|
78 #else |
|
79 return false; |
|
80 #endif |
1765
|
81 } |
|
82 |
|
83 bool |
|
84 file_stat::is_fifo (void) const |
|
85 { |
2466
|
86 #ifdef S_ISFIFO |
1765
|
87 return S_ISFIFO (fs_mode); |
2466
|
88 #else |
|
89 return false; |
|
90 #endif |
1765
|
91 } |
|
92 |
|
93 bool |
|
94 file_stat::is_lnk (void) const |
|
95 { |
2466
|
96 #ifdef S_ISLNK |
1765
|
97 return S_ISLNK (fs_mode); |
2466
|
98 #else |
|
99 return false; |
|
100 #endif |
1765
|
101 } |
|
102 |
|
103 bool |
|
104 file_stat::is_reg (void) const |
|
105 { |
2466
|
106 #ifdef S_ISREG |
1765
|
107 return S_ISREG (fs_mode); |
2466
|
108 #else |
|
109 return false; |
|
110 #endif |
1765
|
111 } |
|
112 |
|
113 bool |
|
114 file_stat::is_sock (void) const |
|
115 { |
2466
|
116 #ifdef S_ISSOCK |
1765
|
117 return S_ISSOCK (fs_mode); |
2466
|
118 #else |
|
119 return false; |
|
120 #endif |
1765
|
121 } |
|
122 |
|
123 extern "C" void mode_string (); |
|
124 |
|
125 string |
|
126 file_stat::mode_as_string (void) const |
|
127 { |
|
128 char buf[11]; |
|
129 |
|
130 mode_string (fs_mode, buf); |
|
131 |
|
132 buf[10] = '\0'; |
|
133 |
2431
|
134 return string (buf); |
1765
|
135 } |
|
136 |
|
137 // Private stuff: |
|
138 |
|
139 void |
|
140 file_stat::update_internal (bool force) |
|
141 { |
|
142 if (! initialized || force) |
|
143 { |
|
144 initialized = false; |
|
145 fail = false; |
|
146 |
|
147 const char *cname = file_name.c_str (); |
|
148 |
|
149 struct stat buf; |
|
150 |
|
151 int status = follow_links |
1776
|
152 ? SAFE_STAT (cname, &buf) : SAFE_LSTAT (cname, &buf); |
1765
|
153 |
|
154 if (status < 0) |
|
155 { |
|
156 fail = true; |
|
157 errmsg = strerror (errno); |
|
158 } |
|
159 else |
|
160 { |
|
161 fs_mode = buf.st_mode; |
|
162 fs_ino = buf.st_ino; |
|
163 fs_dev = buf.st_dev; |
|
164 fs_nlink = buf.st_nlink; |
|
165 fs_uid = buf.st_uid; |
|
166 fs_gid = buf.st_gid; |
|
167 fs_size = buf.st_size; |
|
168 fs_atime = buf.st_atime; |
|
169 fs_mtime = buf.st_mtime; |
|
170 fs_ctime = buf.st_ctime; |
|
171 |
|
172 #if defined (HAVE_ST_RDEV) |
|
173 fs_rdev = buf.st_rdev; |
|
174 #endif |
|
175 |
|
176 #if defined (HAVE_ST_BLKSIZE) |
|
177 fs_blksize = buf.st_blksize; |
|
178 #endif |
|
179 |
|
180 #if defined (HAVE_ST_BLOCKS) |
|
181 fs_blocks = buf.st_blocks; |
|
182 #endif |
|
183 } |
|
184 |
|
185 initialized = true; |
|
186 } |
|
187 } |
|
188 |
1779
|
189 void |
|
190 file_stat::copy (const file_stat& fs) |
|
191 { |
|
192 file_name = fs.file_name; |
|
193 follow_links = fs.follow_links; |
|
194 initialized = fs.initialized; |
|
195 fail = fs.fail; |
|
196 errmsg = fs.errmsg; |
|
197 fs_mode = fs.fs_mode; |
|
198 fs_ino = fs.fs_ino; |
|
199 fs_dev = fs.fs_dev; |
|
200 fs_nlink = fs.fs_nlink; |
|
201 fs_uid = fs.fs_uid; |
|
202 fs_gid = fs.fs_gid; |
|
203 fs_size = fs.fs_size; |
|
204 fs_atime = fs.fs_atime; |
|
205 fs_mtime = fs.fs_mtime; |
|
206 fs_ctime = fs.fs_ctime; |
|
207 |
|
208 #if defined (HAVE_ST_RDEV) |
|
209 fs_rdev = fs.fs_rdev; |
|
210 #endif |
|
211 |
|
212 #if defined (HAVE_ST_BLKSIZE) |
|
213 fs_blksize = fs.fs_blksize; |
|
214 #endif |
|
215 |
|
216 #if defined (HAVE_ST_BLOCKS) |
|
217 fs_blocks = fs.fs_blocks; |
|
218 #endif |
|
219 } |
|
220 |
1765
|
221 // Functions for octave. |
|
222 |
|
223 // Has FILE been modified since TIME? Returns 1 for yes, 0 for no, |
|
224 // and -1 for any error. |
|
225 int |
|
226 is_newer (const string& file, time_t time) |
|
227 { |
|
228 file_stat fs (file); |
|
229 |
|
230 return fs ? fs.is_newer (time) : -1; |
|
231 } |
|
232 |
2433
|
233 // We provide a replacement for mkdir(). |
|
234 |
1765
|
235 int |
1772
|
236 oct_mkdir (const string& name, mode_t mode) |
1765
|
237 { |
|
238 return mkdir (name.c_str (), mode); |
|
239 } |
|
240 |
2668
|
241 int |
|
242 oct_mkdir (const string& name, mode_t mode, string& msg) |
|
243 { |
|
244 msg = string (); |
|
245 |
|
246 int status = mkdir (name.c_str (), mode); |
|
247 |
|
248 if (status < 0) |
|
249 msg = strerror (errno); |
|
250 |
|
251 return status; |
|
252 } |
|
253 |
2433
|
254 // I don't know how to emulate this on systems that don't provide it. |
|
255 |
1765
|
256 int |
1773
|
257 oct_mkfifo (const string& name, mode_t mode) |
1765
|
258 { |
2433
|
259 #if defined (HAVE_MKFIFO) |
1773
|
260 return mkfifo (name.c_str (), mode); |
2433
|
261 #else |
|
262 ::error ("mkfifo: not implemented on this system"); |
|
263 return -1; |
|
264 #endif |
1765
|
265 } |
|
266 |
2668
|
267 int |
|
268 oct_mkfifo (const string& name, mode_t mode, string& msg) |
|
269 { |
|
270 msg = string (); |
|
271 |
|
272 #if defined (HAVE_MKFIFO) |
|
273 int status = mkfifo (name.c_str (), mode); |
|
274 |
|
275 if (status < 0) |
|
276 msg = strerror (errno); |
|
277 |
|
278 return status; |
|
279 #else |
|
280 ::error ("mkfifo: not implemented on this system"); |
|
281 return -1; |
|
282 #endif |
|
283 } |
|
284 |
2433
|
285 // We provide a replacement for rename(). |
|
286 |
1765
|
287 int |
1772
|
288 oct_rename (const string& from, const string& to) |
1765
|
289 { |
|
290 return rename (from.c_str (), to.c_str ()); |
|
291 } |
|
292 |
2668
|
293 int |
|
294 oct_rename (const string& from, const string& to, string& msg) |
|
295 { |
|
296 msg = string (); |
|
297 |
|
298 int status = rename (from.c_str (), to.c_str ()); |
|
299 |
|
300 if (status < 0) |
|
301 msg = strerror (errno); |
|
302 |
|
303 return status; |
|
304 } |
|
305 |
2433
|
306 // We provide a replacement for rmdir(). |
|
307 |
1765
|
308 int |
1773
|
309 oct_rmdir (const string& name) |
1765
|
310 { |
1773
|
311 return rmdir (name.c_str ()); |
1765
|
312 } |
|
313 |
2668
|
314 int |
|
315 oct_rmdir (const string& name, string& msg) |
|
316 { |
|
317 msg = string (); |
|
318 |
|
319 int status = rmdir (name.c_str ()); |
|
320 |
|
321 if (status < 0) |
|
322 msg = strerror (errno); |
|
323 |
|
324 return status; |
|
325 } |
|
326 |
2433
|
327 // We provide a replacement for tempnam(). |
|
328 |
1802
|
329 string |
|
330 oct_tempnam (void) |
|
331 { |
|
332 string retval; |
|
333 |
|
334 char *tmp = tempnam (0, "oct-"); |
|
335 |
|
336 if (tmp) |
|
337 { |
|
338 retval = tmp; |
|
339 |
|
340 free (tmp); |
|
341 } |
|
342 else |
|
343 (*current_liboctave_error_handler) ("can't open temporary file!"); |
|
344 |
|
345 return retval; |
|
346 } |
|
347 |
|
348 |
1765
|
349 int |
1772
|
350 oct_umask (mode_t mode) |
1765
|
351 { |
|
352 #if defined (HAVE_UMASK) |
|
353 return umask (mode); |
|
354 #else |
|
355 return 0; |
|
356 #endif |
|
357 } |
|
358 |
1773
|
359 int |
|
360 oct_unlink (const string& name) |
|
361 { |
|
362 return unlink (name.c_str ()); |
|
363 } |
|
364 |
2668
|
365 int |
|
366 oct_unlink (const string& name, string& errmsg) |
|
367 { |
|
368 errmsg = string (); |
|
369 |
|
370 int status = unlink (name.c_str ()); |
|
371 |
|
372 if (status < 0) |
|
373 errmsg = strerror (errno); |
|
374 |
|
375 return status; |
|
376 } |
|
377 |
1765
|
378 /* |
|
379 ;;; Local Variables: *** |
|
380 ;;; mode: C++ *** |
|
381 ;;; End: *** |
|
382 */ |