1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 //#include <sys/types.h>
18 //#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
180 if (S_ISDIR(buf.st_mode)) return ("/");
182 if (S_ISLNK(buf.st_mode)) return ("@");
185 if (S_ISFIFO(buf.st_mode)) return ("|");
188 if (S_ISSOCK(buf.st_mode)) return ("=");
190 if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
196 mode_t FileInfo::getMode() const
204 // should not be in FileInfo
205 void FileInfo::modeString(char * szString) const
207 szString[0] = typeLetter();
208 flagRWX((buf.st_mode & 0700) << 0, &szString[1]);
209 flagRWX((buf.st_mode & 0070) << 3, &szString[4]);
210 flagRWX((buf.st_mode & 0007) << 6, &szString[7]);
216 // should not be in FileInfo
217 char FileInfo::typeLetter() const
222 if (S_ISBLK(buf.st_mode)) return 'b';
224 if (S_ISCHR(buf.st_mode)) return 'c';
225 if (S_ISDIR(buf.st_mode)) return 'd';
226 if (S_ISREG(buf.st_mode)) return '-';
228 if (S_ISFIFO(buf.st_mode)) return 'p';
231 if (S_ISLNK(buf.st_mode)) return 'l';
234 if (S_ISSOCK(buf.st_mode)) return 's';
237 if (S_ISMPC(buf.st_mode)) return 'm';
240 if (S_ISNWK(buf.st_mode)) return 'n';
246 // should not be in FileInfo
247 void FileInfo::flagRWX(mode_t i, char * szString) const
249 szString[0] = (i & S_IRUSR) ? 'r' : '-';
250 szString[1] = (i & S_IWUSR) ? 'w' : '-';
251 szString[2] = (i & S_IXUSR) ? 'x' : '-';
255 // should not be in FileInfo
256 void FileInfo::setSticky(char * szString) const
261 if (buf.st_mode & S_ISUID) {
262 if (szString[3] != 'x') szString[3] = 'S';
263 else szString[3] = 's';
267 if (buf.st_mode & S_ISGID) {
268 if (szString[6] != 'x') szString[6] = 'S';
269 else szString[6] = 's';
273 if (buf.st_mode & S_ISVTX) {
274 if (szString[9] != 'x') szString[9] = 'T';
275 else szString[9] = 't';
281 time_t FileInfo::getModificationTime() const
288 time_t FileInfo::getAccessTime() const
295 time_t FileInfo::getStatusChangeTime() const
302 nlink_t FileInfo::getNumberOfLinks() const
309 uid_t FileInfo::getUid() const
316 gid_t FileInfo::getGid() const
323 off_t FileInfo::getSize() const
330 int FileInfo::getError() const
336 bool FileInfo::isOK() const
338 // DEC cxx 6.0 chokes on this bizarre construct (compiler bug)
339 // return (status) ? false : true;
340 // So I replaced it with a simpler one (JMarc)
345 bool FileInfo::isLink() const
348 return S_ISLNK(buf.st_mode);
352 bool FileInfo::isRegular() const
355 return S_ISREG(buf.st_mode);
359 bool FileInfo::isDir() const
362 return S_ISDIR(buf.st_mode);
366 bool FileInfo::isChar() const
369 return S_ISCHR(buf.st_mode);
373 bool FileInfo::isBlock() const
376 return S_ISBLK(buf.st_mode);
380 bool FileInfo::isFifo() const
383 return S_ISFIFO(buf.st_mode);
387 bool FileInfo::isSocket() const
391 return S_ISSOCK(buf.st_mode);
398 // should not be in FileInfo
399 bool FileInfo::access(int p) const
401 // if we don't have a filename we fail
402 if (fname.empty()) return false;
404 if (::access(fname.c_str(), p) == 0)
407 // If we were really kind, we would also tell why
408 // the file access failed.