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