tools/stat: break main into functions

This commit is contained in:
Danny Robson 2019-04-25 09:22:43 +10:00
parent 2d7eb25167
commit 4d538190f2
2 changed files with 64 additions and 53 deletions

@ -1 +1 @@
Subproject commit 174bd62a293d3e15dc4fdc3112dea21443fc1d90 Subproject commit 1ca47626aa5c033d9a40d7040657ba4ecc045155

View File

@ -12,12 +12,7 @@
#include <cstdlib> #include <cstdlib>
constexpr unsigned type_index (mode_t val) ///////////////////////////////////////////////////////////////////////////////
{
return (val & S_IFMT) >> cruft::ctz (unsigned {S_IFMT});
}
struct type_printer { struct type_printer {
type_printer (struct stat const &_data) type_printer (struct stat const &_data)
: data (_data) : data (_data)
@ -26,6 +21,8 @@ struct type_printer {
struct stat const &data; struct stat const &data;
}; };
//-----------------------------------------------------------------------------
std::ostream& operator<< (std::ostream &os, type_printer const &val) std::ostream& operator<< (std::ostream &os, type_printer const &val)
{ {
os << "[ "; os << "[ ";
@ -50,6 +47,7 @@ std::ostream& operator<< (std::ostream &os, type_printer const &val)
} }
///////////////////////////////////////////////////////////////////////////////
struct mode_printer { struct mode_printer {
mode_printer (struct stat const &_data) mode_printer (struct stat const &_data)
: data (_data) : data (_data)
@ -59,6 +57,7 @@ struct mode_printer {
}; };
//-----------------------------------------------------------------------------
std::ostream& operator<< (std::ostream &os, mode_printer const &val) std::ostream& operator<< (std::ostream &os, mode_printer const &val)
{ {
return S_ISREG (val.data.st_mode) ? os << "REGULAR" return S_ISREG (val.data.st_mode) ? os << "REGULAR"
@ -72,6 +71,7 @@ std::ostream& operator<< (std::ostream &os, mode_printer const &val)
} }
//-----------------------------------------------------------------------------
std::ostream& operator<< (std::ostream &os, struct stat const &val) std::ostream& operator<< (std::ostream &os, struct stat const &val)
{ {
return os << "{ dev: " << val.st_dev return os << "{ dev: " << val.st_dev
@ -86,24 +86,18 @@ std::ostream& operator<< (std::ostream &os, struct stat const &val)
} }
int main (int argc, char **argv) ///////////////////////////////////////////////////////////////////////////////
static void do_stat (char const *path)
{ {
if (argc < 2) {
std::cerr << argv[0] << " <target> ...\n";
return EXIT_FAILURE;
}
for (int i = 1; i < argc; ++i) {
std::cout << "{ stat: ";
char const *path = argv[i];
{
struct stat buffer; struct stat buffer;
cruft::posix::error::try_call (stat, path, &buffer); cruft::posix::error::try_call (stat, path, &buffer);
std::cout << buffer; std::cout << buffer;
} }
//-----------------------------------------------------------------------------
void do_xattr (char const *path)
{
std::cout << ", xattr: "; std::cout << ", xattr: ";
{ {
@ -143,6 +137,23 @@ int main (int argc, char **argv)
std::cout << "}"; std::cout << "}";
} }
}
///////////////////////////////////////////////////////////////////////////////
int main (int argc, char **argv)
{
if (argc < 2) {
std::cerr << argv[0] << " <target> ...\n";
return EXIT_FAILURE;
}
for (int i = 1; i < argc; ++i) {
std::cout << "{ stat: ";
char const *path = argv[i];
do_stat (path);
do_xattr (path);
std::cout << '\n'; std::cout << '\n';
} }