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>
19 //#include <sys/stat.h>
26 # define S_IRUSR S_IREAD
28 # define S_IRUSR 00400
34 # define S_IWUSR S_IWRITE
36 # define S_IWUSR 00200
42 # define S_IXUSR S_IEXEC
44 # define S_IXUSR 00100
48 #ifdef STAT_MACROS_BROKEN
61 #if !defined(S_ISBLK) && defined(S_IFBLK)
62 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
64 #if !defined(S_ISCHR) && defined(S_IFCHR)
65 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
67 #if !defined(S_ISDIR) && defined(S_IFDIR)
68 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
70 #if !defined(S_ISREG) && defined(S_IFREG)
71 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
73 #if !defined(S_ISFIFO) && defined(S_IFIFO)
74 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
79 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
80 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
82 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
83 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
84 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
86 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
87 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
90 // Since major is a function on SVR4, we can't use `ifndef major'.
91 // might want to put MAJOR_IN_MKDEV for SYSV
93 #include <sys/mkdev.h>
96 #ifdef MAJOR_IN_SYSMACROS
97 #include <sys/sysmacros.h>
105 #define major(dev) (((dev) >> 8) & 0xff)
106 #define minor(dev) ((dev) & 0xff)
107 #define makedev(maj, min) (((maj) << 8) | (min))
118 FileInfo::FileInfo(string const & path, bool link)
126 FileInfo::FileInfo(int fildes)
129 status = fstat(fildes, &buf);
130 if (status) err = errno;
134 void FileInfo::init()
141 void FileInfo::dostat(bool link)
144 status = ::lstat(fname.c_str(), &buf);
146 status = ::stat(fname.c_str(), &buf);
148 if (status) err = errno;
152 FileInfo & FileInfo::newFile(string const & path, bool link)
165 FileInfo & FileInfo::newFile(int fildes)
169 status = fstat(fildes, &buf);
170 if (status) err = errno;
175 // should not be in FileInfo
176 char const * FileInfo::typeIndicator() const
178 if (S_ISDIR(buf.st_mode)) return ("/");
180 if (S_ISLNK(buf.st_mode)) return ("@");
183 if (S_ISFIFO(buf.st_mode)) return ("|");
186 if (S_ISSOCK(buf.st_mode)) return ("=");
188 if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
194 mode_t FileInfo::getMode() const
200 // should not be in FileInfo
201 void FileInfo::modeString(char * szString) const
203 szString[0] = typeLetter();
204 flagRWX((buf.st_mode & 0700) << 0, &szString[1]);
205 flagRWX((buf.st_mode & 0070) << 3, &szString[4]);
206 flagRWX((buf.st_mode & 0007) << 6, &szString[7]);
212 // should not be in FileInfo
213 char FileInfo::typeLetter() const
216 if (S_ISBLK(buf.st_mode)) return 'b';
218 if (S_ISCHR(buf.st_mode)) return 'c';
219 if (S_ISDIR(buf.st_mode)) return 'd';
220 if (S_ISREG(buf.st_mode)) return '-';
222 if (S_ISFIFO(buf.st_mode)) return 'p';
225 if (S_ISLNK(buf.st_mode)) return 'l';
228 if (S_ISSOCK(buf.st_mode)) return 's';
231 if (S_ISMPC(buf.st_mode)) return 'm';
234 if (S_ISNWK(buf.st_mode)) return 'n';
240 // should not be in FileInfo
241 void FileInfo::flagRWX(mode_t i, char * szString) const
243 szString[0] = (i & S_IRUSR) ? 'r' : '-';
244 szString[1] = (i & S_IWUSR) ? 'w' : '-';
245 szString[2] = (i & S_IXUSR) ? 'x' : '-';
249 // should not be in FileInfo
250 void FileInfo::setSticky(char * szString) const
253 if (buf.st_mode & S_ISUID) {
254 if (szString[3] != 'x') szString[3] = 'S';
255 else szString[3] = 's';
259 if (buf.st_mode & S_ISGID) {
260 if (szString[6] != 'x') szString[6] = 'S';
261 else szString[6] = 's';
265 if (buf.st_mode & S_ISVTX) {
266 if (szString[9] != 'x') szString[9] = 'T';
267 else szString[9] = 't';
273 time_t FileInfo::getModificationTime() const
279 time_t FileInfo::getAccessTime() const
285 time_t FileInfo::getStatusChangeTime() const
291 nlink_t FileInfo::getNumberOfLinks() const
297 uid_t FileInfo::getUid() const
303 gid_t FileInfo::getGid() const
309 off_t FileInfo::getSize() const
315 int FileInfo::getError() const
321 bool FileInfo::isOK() const
323 // DEC cxx 6.0 chokes on this bizarre construct (compiler bug)
324 // return (status) ? false : true;
325 // So I replaced it with a simpler one (JMarc)
330 bool FileInfo::isLink() const
332 return S_ISLNK(buf.st_mode);
336 bool FileInfo::isRegular() const
338 return S_ISREG(buf.st_mode);
342 bool FileInfo::isDir() const
344 return S_ISDIR(buf.st_mode);
348 bool FileInfo::isChar() const
350 return S_ISCHR(buf.st_mode);
354 bool FileInfo::isBlock() const
356 return S_ISBLK(buf.st_mode);
360 bool FileInfo::isFifo() const
362 return S_ISFIFO(buf.st_mode);
366 bool FileInfo::isSocket() const
369 return S_ISSOCK(buf.st_mode);
376 // should not be in FileInfo
377 bool FileInfo::access(int p) const
379 // if we don't have a filename we fail
380 if (fname.empty()) return false;
382 if (::access(fname.c_str(), p) == 0)
385 // If we were really kind, we would also tell why
386 // the file access failed.