Skip to content

Instantly share code, notes, and snippets.

@kolodziej
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolodziej/9375179 to your computer and use it in GitHub Desktop.
Save kolodziej/9375179 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <cstring>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <ctime>
using namespace std;
void printStat(const char *, struct stat *);
char * permissions(mode_t);
char * type(mode_t);
int main(int argc, char ** argv)
{
if (argc > 1)
{
struct stat buf;
for (int i = 1; i < argc; ++i)
{
lstat(argv[i], &buf);
printStat(argv[i], &buf);
}
}
return 0;
}
char * permissions(mode_t mode)
{
const char * modes[] = { "---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx" };
char * dest = new char[10];
dest[9] = 0;
strcpy(&dest[0], modes[(mode >> 6) & 7]);
strcpy(&dest[3], modes[(mode >> 3) & 7]);
strcpy(&dest[6], modes[mode & 7]);
return dest;
}
char * type(mode_t mode)
{
char * buffer = new char[20];
if (S_ISREG(mode))
{
strcpy(buffer, "zwykly plik");
return buffer;
}
if (S_ISDIR(mode))
{
strcpy(buffer, "katalog");
return buffer;
}
if (S_ISCHR(mode))
{
strcpy(buffer, "urzadzenie znakowe");
return buffer;
}
if (S_ISBLK(mode))
{
strcpy(buffer, "urzadzenie blokowe");
return buffer;
}
if (S_ISFIFO(mode))
{
strcpy(buffer, "potok");
return buffer;
}
if (S_ISLNK(mode))
{
strcpy(buffer, "dowiazanie symboliczne");
return buffer;
}
if (S_ISSOCK(mode))
{
strcpy(buffer, "gniazdo");
return buffer;
}
}
void printStat(const char * fname, struct stat *data)
{
struct passwd *pw = getpwuid(data->st_uid);
struct group *gr = getgrgid(data->st_gid);
cout << "_____________________________________________________________" << endl;
cout << "Nazwa pliku: " << fname << "\tRozmiar pliku: " << data->st_size << "\n";
cout << "Rozmiar bloku: " << data->st_blksize << "\tLiczba zajmowanych blokow: " << data->st_blocks << "\n";
cout << "Urzadzenie: " << hex << data->st_dev << "h/" << dec << data->st_dev << "d\ti-wezel: " << data->st_ino << "\n";
cout << "Liczba twardych dowiazan: " << data->st_nlink << "\t\t" << type(data->st_mode) << "\n";
cout << "Uprawnienia: (" << oct << data->st_mode << ") " << permissions(data->st_mode) << "\n";
cout << dec << "Wlasciciel: " << data->st_uid << " (" << pw->pw_name << ")\tGrupa: " << data->st_gid << " (" << gr->gr_name << ")\n";
char timebuffer[80];
struct tm *ptm;
ptm = localtime(&(data->st_atime));
strftime(timebuffer, 80, "%c", ptm);
cout << "Ostatni dostep: " << timebuffer << "\n";
ptm = localtime(&(data->st_mtime));
strftime(timebuffer, 80, "%c", ptm);
cout << "Data modyfikacji: " << timebuffer << "\n";
ptm = localtime(&(data->st_ctime));
strftime(timebuffer, 80, "%c", ptm);
cout << "Data zmiany atrybutow: " << timebuffer << "\n";
cout << "____________________________________________________________" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment