Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created August 19, 2010 13:15
Show Gist options
  • Save betawaffle/537841 to your computer and use it in GitHub Desktop.
Save betawaffle/537841 to your computer and use it in GitHub Desktop.
File::Debug()
#define IsReg(s) S_ISREG(s.st_mode)
#define IsDir(s) S_ISDIR(s.st_mode)
#define IsLnk(s) S_ISLNK(s.st_mode)
#define IsBlk(s) S_ISBLK(s.st_mode)
#define IsChr(s) S_ISCHR(s.st_mode)
#define IsFifo(s) S_ISFIFO(s.st_mode)
#define IsSock(s) S_ISSOCK(s.st_mode)
#if _POSIX_MESSAGE_PASSING >= 0
#define IsMsgQ(s) S_TYPEISMQ(s)
#else
#define IsMsgQ(s) (0)
#endif/*_POSIX_MESSAGE_PASSING >= 0*/
#if _POSIX_SEMAPHORES >= 0
#define IsSem(s) S_TYPEISSEM(s)
#else
#define IsSem(s) (0)
#endif/*_POSIX_SEMAPHORES >= 0*/
#if _POSIX_SHARED_MEMORY_OBJECTS >= 0
#define IsSmo(s) S_TYPEISSHM(s)
#else
#define IsSmo(s) (0)
#endif/*_POSIX_SHARED_MEMORY_OBJECTS >= 0*/
#if _POSIX_TYPED_MEMORY_OBJECTS >= 0
#define IsTmo(s) S_TYPEISTMO(s)
#else
#define IsTmo(s) (0)
#endif/*_POSIX_TYPED_MEMORY_OBJECTS >= 0*/
#define FileType(s) (\
IsReg(s) ? "Regular" : \
IsDir(s) ? "Directory" : \
IsLnk(s) ? "Symbolic Link" : \
IsBlk(s) ? "Block Special" : \
IsChr(s) ? "Character Special" : \
IsFifo(s) ? "FIFO Special" : \
IsSock(s) ? "Socket" : \
IsMsgQ(s) ? "Message Queue" : \
IsSem(s) ? "Semaphore" : \
IsSmo(s) ? "Shared Memory Object" : \
IsTmo(s) ? "Typed Memory Object" : "Unknown")
#define FilePerm(s, l) (\
s.st_mode & S_IR##l ? " Read" : "", \
s.st_mode & S_IW##l ? " Write" : "", \
s.st_mode & S_IX##l ? " Execute" : "", \
s.st_mode & S_IRWX##s ? "" : " None")
#define FileModeSOX(s) (\
s.st_mode & (S_ISUID | S_ISGID) ? "User & Group" : \
s.st_mode & (S_ISUID) ? "User" : \
s.st_mode & (S_ISGID) ? "Group" : "None")
#define FileModeVTX(s) (\
s.st_mode & S_ISVTX ? "Restricted" : "Unrestricted")
void
File::Debug(void)
{
struct stat s;
if (fstat(this->mDescriptor, &s) < 0) {
switch (errno) {
case EBADF: throw InvalidDescriptor();
case EIO: throw IOError();
case EOVERFLOW: throw Overflow();
}
throw errno;
}
printf("File Descriptor: %i\n", this->mDescriptor);
/*printf(" File on Device ID: %08X\n", s.st_dev);*/
/*printf(" File Serial Number: %X\n", s.st_ino);*/
printf(" File Owner: %i%s\n",
s.st_uid,
s.st_mode & S_ISUID ? " (Set-on-Execute)" : "");
printf(" File Group: %i%s\n",
s.st_gid,
s.st_mode & S_ISUID ? " (Set-on-Execute)" : "");
printf(" File Type: %s\n", FileType(s));
#if _XOPEN_VERSION == 600 && 0
if (IsReg(s) || IsSmo(s) || IsTmo(s)) {
printf(" Size: %i bytes\n", (int) s.st_size);
} else if (IsDir(s)) {
printf(" Deletion: %s", FileModeVTX(s));
} else if (IsLnk(s)) {
printf(" Path Length: %i bytes\n", (int) s.st_size);
} else if (IsBlk(s) || IsChr(s)) {
printf(" Device ID: %i\n", s.st_rdev);
}
#endif/*_XOPEN_VERSION == 600*/
printf(" File Permissions\n");
printf(" Owner:%s%s%s%s\n",
s.st_mode & S_IRUSR ? " Read" : "",
s.st_mode & S_IWUSR ? " Write" : "",
s.st_mode & S_IXUSR ? " Execute" : "",
s.st_mode & S_IRWXU ? "" : " None");
printf(" Group:%s%s%s%s\n",
s.st_mode & S_IRGRP ? " Read" : "",
s.st_mode & S_IWGRP ? " Write" : "",
s.st_mode & S_IXGRP ? " Execute" : "",
s.st_mode & S_IRWXG ? "" : " None");
printf(" Other:%s%s%s%s\n",
s.st_mode & S_IROTH ? " Read" : "",
s.st_mode & S_IWOTH ? " Write" : "",
s.st_mode & S_IXOTH ? " Execute" : "",
s.st_mode & S_IRWXO ? "" : " None");
/*printf(" Times\n");*/
/*printf(" Last Access: %i\n", s.st_atime);*/
/*printf(" Last Data Modification: %i\n", s.st_mtime);*/
/*printf(" Last Status Change: %i\n", s.st_ctime);*/
printf(" Hard Links: %i\n", s.st_nlink);
#if _XOPEN_VERSION == 600
/*printf(" Blocks\n");*/
/*printf(" Count: %i\n", (int) s.st_blocks);*/
/*printf(" Size: %i\n", s.st_blksize);*/
#endif/*_XOPEN_VERSION == 600*/
}
#undef FileModeVTX
#undef FileModeSOX
#undef FilePerm
#undef FileType
#undef IsTmo
#undef IsSmo
#undef IsSem
#undef IsMsgQ
#undef IsSock
#undef IsFifo
#undef IsChr
#undef IsBlk
#undef IsLnk
#undef IsDir
#undef IsReg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment