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 * ====================================================== */
14 #include <sys/types.h>
21 # define S_IRUSR S_IREAD
23 # define S_IRUSR 00400
29 # define S_IWUSR S_IWRITE
31 # define S_IWUSR 00200
37 # define S_IXUSR S_IEXEC
39 # define S_IXUSR 00100
43 #ifdef STAT_MACROS_BROKEN
56 #if !defined(S_ISBLK) && defined(S_IFBLK)
57 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
59 #if !defined(S_ISCHR) && defined(S_IFCHR)
60 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
62 #if !defined(S_ISDIR) && defined(S_IFDIR)
63 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
65 #if !defined(S_ISREG) && defined(S_IFREG)
66 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
68 #if !defined(S_ISFIFO) && defined(S_IFIFO)
69 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
71 #if !defined(S_ISLNK) && defined(S_IFLNK)
72 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
74 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
75 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
77 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
78 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
79 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
81 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
82 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
85 // Since major is a function on SVR4, we can't use `ifndef major'.
86 // might want to put MAJOR_IN_MKDEV for SYSV
88 #include <sys/mkdev.h>
91 #ifdef MAJOR_IN_SYSMACROS
92 #include <sys/sysmacros.h>
100 #define major(dev) (((dev) >> 8) & 0xff)
101 #define minor(dev) ((dev) & 0xff)
102 #define makedev(maj, min) (((maj) << 8) | (min))
113 FileInfo::FileInfo(string const & path, bool link)
121 FileInfo::FileInfo(int fildes)
124 status = fstat(fildes, &buf);
125 if (status) err = errno;
129 void FileInfo::init()
136 void FileInfo::dostat(bool link)
139 status = lstat(fname.c_str(), &buf);
141 status = stat(fname.c_str(), &buf);
143 if (status) err = errno;
147 FileInfo & FileInfo::newFile(string const & path, bool link)
160 FileInfo & FileInfo::newFile(int fildes)
164 status = fstat(fildes, &buf);
165 if (status) err = errno;
170 // should not be in FileInfo
171 char const * FileInfo::typeIndicator() const
173 if (S_ISDIR(buf.st_mode)) return ("/");
175 if (S_ISLNK(buf.st_mode)) return ("@");
178 if (S_ISFIFO(buf.st_mode)) return ("|");
181 if (S_ISSOCK(buf.st_mode)) return ("=");
183 if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
189 mode_t FileInfo::getMode() const
195 // should not be in FileInfo
196 void FileInfo::modeString(char * szString) const
198 szString[0] = typeLetter();
199 flagRWX((buf.st_mode & 0700) << 0, &szString[1]);
200 flagRWX((buf.st_mode & 0070) << 3, &szString[4]);
201 flagRWX((buf.st_mode & 0007) << 6, &szString[7]);
207 // should not be in FileInfo
208 char FileInfo::typeLetter() const
211 if (S_ISBLK(buf.st_mode)) return 'b';
213 if (S_ISCHR(buf.st_mode)) return 'c';
214 if (S_ISDIR(buf.st_mode)) return 'd';
215 if (S_ISREG(buf.st_mode)) return '-';
217 if (S_ISFIFO(buf.st_mode)) return 'p';
220 if (S_ISLNK(buf.st_mode)) return 'l';
223 if (S_ISSOCK(buf.st_mode)) return 's';
226 if (S_ISMPC(buf.st_mode)) return 'm';
229 if (S_ISNWK(buf.st_mode)) return 'n';
235 // should not be in FileInfo
236 void FileInfo::flagRWX(unsigned short i, char * szString) const
238 szString[0] = (i & S_IRUSR) ? 'r' : '-';
239 szString[1] = (i & S_IWUSR) ? 'w' : '-';
240 szString[2] = (i & S_IXUSR) ? 'x' : '-';
244 // should not be in FileInfo
245 void FileInfo::setSticky(char * szString) const
248 if (buf.st_mode & S_ISUID) {
249 if (szString[3] != 'x') szString[3] = 'S';
250 else szString[3] = 's';
254 if (buf.st_mode & S_ISGID) {
255 if (szString[6] != 'x') szString[6] = 'S';
256 else szString[6] = 's';
260 if (buf.st_mode & S_ISVTX) {
261 if (szString[9] != 'x') szString[9] = 'T';
262 else szString[9] = 't';
268 time_t FileInfo::getModificationTime() const
274 time_t FileInfo::getAccessTime() const
280 time_t FileInfo::getStatusChangeTime() const
286 nlink_t FileInfo::getNumberOfLinks() const
292 uid_t FileInfo::getUid() const
298 gid_t FileInfo::getGid() const
304 off_t FileInfo::getSize() const
310 int FileInfo::getError() const
316 bool FileInfo::isOK() const
318 // DEC cxx 6.0 chokes on this bizarre construct (compiler bug)
319 // return (status) ? false : true;
320 // So I replaced it with a simpler one (JMarc)
325 bool FileInfo::isLink() const
327 return S_ISLNK(buf.st_mode);
331 bool FileInfo::isRegular() const
333 return S_ISREG(buf.st_mode);
337 bool FileInfo::isDir() const
339 return S_ISDIR(buf.st_mode);
343 bool FileInfo::isChar() const
345 return S_ISCHR(buf.st_mode);
349 bool FileInfo::isBlock() const
351 return S_ISBLK(buf.st_mode);
355 bool FileInfo::isFifo() const
357 return S_ISFIFO(buf.st_mode);
361 bool FileInfo::isSocket() const
364 return S_ISSOCK(buf.st_mode);
371 // should not be in FileInfo
372 bool FileInfo::access(int p)
374 // if we don't have a filename we fail
375 if (fname.empty()) return false;
377 if (::access(fname.c_str(), p) == 0)
380 // If we were really kind, we would also tell why
381 // the file access failed.