2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
15 #pragma implementation
18 #include <sys/types.h>
25 # define S_IRUSR S_IREAD
27 # define S_IRUSR 00400
33 # define S_IWUSR S_IWRITE
35 # define S_IWUSR 00200
41 # define S_IXUSR S_IEXEC
43 # define S_IXUSR 00100
47 #ifdef STAT_MACROS_BROKEN
60 #if !defined(S_ISBLK) && defined(S_IFBLK)
61 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
63 #if !defined(S_ISCHR) && defined(S_IFCHR)
64 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
66 #if !defined(S_ISDIR) && defined(S_IFDIR)
67 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
69 #if !defined(S_ISREG) && defined(S_IFREG)
70 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
72 #if !defined(S_ISFIFO) && defined(S_IFIFO)
73 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
75 #if !defined(S_ISLNK) && defined(S_IFLNK)
76 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
78 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
79 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
81 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
82 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
83 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
85 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
86 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
89 // Since major is a function on SVR4, we can't use `ifndef major'.
90 // might want to put MAJOR_IN_MKDEV for SYSV
92 #include <sys/mkdev.h>
95 #ifdef MAJOR_IN_SYSMACROS
96 #include <sys/sysmacros.h>
104 #define major(dev) (((dev) >> 8) & 0xff)
105 #define minor(dev) ((dev) & 0xff)
106 #define makedev(maj, min) (((maj) << 8) | (min))
117 FileInfo::FileInfo(string const & path, bool link)
125 FileInfo::FileInfo(int fildes)
128 status = fstat(fildes, &buf);
129 if (status) err = errno;
133 void FileInfo::init()
140 void FileInfo::dostat(bool link)
143 status = ::lstat(fname.c_str(), &buf);
145 status = ::stat(fname.c_str(), &buf);
147 if (status) err = errno;
151 FileInfo & FileInfo::newFile(string const & path, bool link)
164 FileInfo & FileInfo::newFile(int fildes)
168 status = fstat(fildes, &buf);
169 if (status) err = errno;
174 // should not be in FileInfo
175 char const * FileInfo::typeIndicator() const
177 if (S_ISDIR(buf.st_mode)) return ("/");
179 if (S_ISLNK(buf.st_mode)) return ("@");
182 if (S_ISFIFO(buf.st_mode)) return ("|");
185 if (S_ISSOCK(buf.st_mode)) return ("=");
187 if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
193 mode_t FileInfo::getMode() const
199 // should not be in FileInfo
200 void FileInfo::modeString(char * szString) const
202 szString[0] = typeLetter();
203 flagRWX((buf.st_mode & 0700) << 0, &szString[1]);
204 flagRWX((buf.st_mode & 0070) << 3, &szString[4]);
205 flagRWX((buf.st_mode & 0007) << 6, &szString[7]);
211 // should not be in FileInfo
212 char FileInfo::typeLetter() const
215 if (S_ISBLK(buf.st_mode)) return 'b';
217 if (S_ISCHR(buf.st_mode)) return 'c';
218 if (S_ISDIR(buf.st_mode)) return 'd';
219 if (S_ISREG(buf.st_mode)) return '-';
221 if (S_ISFIFO(buf.st_mode)) return 'p';
224 if (S_ISLNK(buf.st_mode)) return 'l';
227 if (S_ISSOCK(buf.st_mode)) return 's';
230 if (S_ISMPC(buf.st_mode)) return 'm';
233 if (S_ISNWK(buf.st_mode)) return 'n';
239 // should not be in FileInfo
240 void FileInfo::flagRWX(mode_t i, char * szString) const
242 szString[0] = (i & S_IRUSR) ? 'r' : '-';
243 szString[1] = (i & S_IWUSR) ? 'w' : '-';
244 szString[2] = (i & S_IXUSR) ? 'x' : '-';
248 // should not be in FileInfo
249 void FileInfo::setSticky(char * szString) const
252 if (buf.st_mode & S_ISUID) {
253 if (szString[3] != 'x') szString[3] = 'S';
254 else szString[3] = 's';
258 if (buf.st_mode & S_ISGID) {
259 if (szString[6] != 'x') szString[6] = 'S';
260 else szString[6] = 's';
264 if (buf.st_mode & S_ISVTX) {
265 if (szString[9] != 'x') szString[9] = 'T';
266 else szString[9] = 't';
272 time_t FileInfo::getModificationTime() const
278 time_t FileInfo::getAccessTime() const
284 time_t FileInfo::getStatusChangeTime() const
290 nlink_t FileInfo::getNumberOfLinks() const
296 uid_t FileInfo::getUid() const
302 gid_t FileInfo::getGid() const
308 off_t FileInfo::getSize() const
314 int FileInfo::getError() const
320 bool FileInfo::isOK() const
322 // DEC cxx 6.0 chokes on this bizarre construct (compiler bug)
323 // return (status) ? false : true;
324 // So I replaced it with a simpler one (JMarc)
329 bool FileInfo::isLink() const
331 return S_ISLNK(buf.st_mode);
335 bool FileInfo::isRegular() const
337 return S_ISREG(buf.st_mode);
341 bool FileInfo::isDir() const
343 return S_ISDIR(buf.st_mode);
347 bool FileInfo::isChar() const
349 return S_ISCHR(buf.st_mode);
353 bool FileInfo::isBlock() const
355 return S_ISBLK(buf.st_mode);
359 bool FileInfo::isFifo() const
361 return S_ISFIFO(buf.st_mode);
365 bool FileInfo::isSocket() const
368 return S_ISSOCK(buf.st_mode);
375 // should not be in FileInfo
376 bool FileInfo::access(int p) const
378 // if we don't have a filename we fail
379 if (fname.empty()) return false;
381 if (::access(fname.c_str(), p) == 0)
384 // If we were really kind, we would also tell why
385 // the file access failed.