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