Facebook
From J00R, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 73
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <time.h>
  6. #include <sys/stat.h>
  7. #include <grp.h>
  8. #include <pwd.h>
  9.  
  10. // Stale przechowujace format wyswietlanej daty oraz daty z godzina
  11. #define DATE_FORMAT "%d-%m-%Y"
  12. #define DATE_TIME_FORMAT "%d %B %Y roku o %X"
  13.  
  14. // Struktura przechowujaca wszystkie informacje na temat pliku, ktore ponzeniej
  15. typedef struct fileSystemNode {
  16.     char type;
  17.     char permission[16];
  18.     char owner[128];
  19.     char group[128];
  20.     long size;
  21.     int linkCount;
  22.     time_t lastUpdateTimestamp;
  23.     time_t lastAccessTimestamp;
  24.     time_t lastStateChangeTimestamp;
  25.     char name[512];
  26.     char filename[1024];
  27.     short isExecutable;
  28.     short isSymLink;
  29.     short isDirectory;
  30. } fsNode;
  31.  
  32. // Funkcja porownujaca napisy wykorzystywana przy sortowaniu
  33. int stringComparator(const fsNode*a, const fsNode*b) {
  34.     return strcmp(a->name, b->name);
  35. }
  36.  
  37. // Funkcja formatujaca date/czas do wyswietlenia, funkcja zwraca wartosc poprzez podany wskaznik
  38. // (zwracana wartosc podobnie jak w funkcji scanf)
  39. void formatTime(time_t t, char *output, char *format) {
  40.     struct tm lt;
  41.     localtime_r(&t, &lt);
  42.     strftime(output, 64, format, &lt);
  43. }
  44.  
  45. // Funkcja odczytujaca za pomoca funkcji stat informacje o pliku i wpisujaca je do struktury fsNode
  46. fsNode getFsNode(const char *path,const char *name) {
  47.     struct stat S;
  48.     char filename[512];
  49.     sprintf(filename, "%s/%s", path, name);
  50.     stat(filename, &S);
  51.     char perm[16];
  52.     sprintf(perm, "%c%c%c%c%c%c%c%c%c",
  53.             ((S.st_mode & S_IRUSR) ? 'r' : '-'),
  54.             ((S.st_mode & S_IWUSR) ? 'w' : '-'),
  55.             ((S.st_mode & S_IXUSR) ? 'x' : '-'),
  56.             ((S.st_mode & S_IRGRP) ? 'r' : '-'),
  57.             ((S.st_mode & S_IWGRP) ? 'w' : '-'),
  58.             ((S.st_mode & S_IXGRP) ? 'x' : '-'),
  59.             ((S.st_mode & S_IROTH) ? 'r' : '-'),
  60.             ((S.st_mode & S_IWOTH) ? 'w' : '-'),
  61.             ((S.st_mode & S_IXOTH) ? 'x' : '-'));
  62.  
  63.     struct group *gr = getgrgid(S.st_gid);
  64.     struct passwd *user = getpwuid(S.st_uid);
  65.  
  66.     fsNode fileNode;
  67.     strcpy(fileNode.name, name);
  68.     strcpy(fileNode.filename, filename);                                                                                                                                             strcpy(fileNode.owner, user->pw_name);
  69.     strcpy(fileNode.group, gr->gr_name);
  70.     strcpy(fileNode.permission, perm);
  71.     fileNode.type = ((S_ISDIR(S.st_mode)) ? 'd' : '-');
  72.     fileNode.size = S.st_size;
  73.     fileNode.linkCount = S.st_nlink;
  74.     fileNode.lastUpdateTimestamp = S.st_mtime;
  75.     fileNode.lastAccessTimestamp = S.st_atime;
  76.     fileNode.lastStateChangeTimestamp = S.st_ctime;
  77.     fileNode.isExecutable = (S.st_mode & S_IXUSR) || (S.st_mode & S_IXGRP) || (S.st_mode & S_IXOTH);
  78.     fileNode.isSymLink = (S_ISLNK(S.st_mode)) ? 1 : 0;
  79.     fileNode.isDirectory = (S_ISDIR(S.st_mode)) ? 1 : 0;
  80.     return fileNode;
  81. }
  82.  
  83. // Funkcja drukujaca listing zawartosci katalogu - TRYB 1
  84. void printDir(char *path) {
  85.     DIR *mydir;
  86.     struct dirent *myfile;
  87.     mydir = opendir(path);
  88.     int i = 0;
  89.     fsNode nodes[128];
  90.  
  91.     while ((myfile = readdir(mydir)) != NULL) {
  92.         nodes[i] = getFsNode(path, myfile->d_name);
  93.         i++;
  94.     }
  95.  
  96.         qsort( nodes, i, sizeof(fsNode),( int( * )( const void *, const void * ) ) stringComparator);
  97.  
  98.  
  99.     int j = 0;
  100.     char formattedTime[64];
  101.     while (j < i) {
  102.         formatTime(nodes[j].lastUpdateTimestamp, formattedTime, DATE_FORMAT);
  103.         printf("%c%s %3d %10s %10s %8ld %s %s\n",
  104.                nodes[j].type,
  105.                nodes[j].permission,
  106.                nodes[j].linkCount,
  107.                nodes[j].owner,
  108.                nodes[j].group,
  109.                nodes[j].size,
  110.                formattedTime,
  111.                nodes[j].name);
  112.         j++;
  113.     }
  114.  
  115.     closedir(mydir);
  116. }
  117.  
  118. // Funkcja drukujaca szczegoly dla zadanego pliku - TRYB 2
  119. void printDetails(const char *path,const char *name) {
  120.     fsNode fileNode = getFsNode(path, name);
  121.  
  122.     printf("Informacje o pliku:\nTyp pliku: ");
  123.     if (fileNode.isSymLink) {
  124.         printf("link symboliczny\n");
  125.     } else if (fileNode.isDirectory) {
  126.         printf("katalog\n");
  127.     } else {
  128.         printf("zwykly plik\n");
  129.     }
  130.     printf("Sciezka:     %s\n", fileNode.filename);
  131.     printf("Rozmiar:     %ld bajtow\n", fileNode.size);
  132.     printf("Uprawnienia: %s\n", fileNode.permission);
  133.  
  134.     char formattedTime[64];
  135.  
  136.     formatTime(fileNode.lastAccessTimestamp, formattedTime, DATE_TIME_FORMAT);
  137.     printf("Ostatnio uzywany:        %s\n", formattedTime);
  138.  
  139.     formatTime(fileNode.lastUpdateTimestamp, formattedTime, DATE_TIME_FORMAT);
  140.     printf("Ostatnio modyfikowany:   %s\n", formattedTime);
  141.  
  142.     formatTime(fileNode.lastStateChangeTimestamp, formattedTime, DATE_TIME_FORMAT);
  143.     printf("Ostatnio zmieniany stan: %s\n", formattedTime);
  144.  
  145.     if (!fileNode.isExecutable) {
  146.         printf("Poczatek zawartosci: \n");
  147.         FILE *file = fopen(fileNode.filename, "r");
  148.         char contents[512];
  149.         int i = 2;
  150.         while (fgets(contents, sizeof(contents), file) && i-- > 0) {
  151.             printf("%s", contents);
  152.         }
  153.     }
  154. }
  155.  
  156. // Przekazanie parametrow wejsciowych do odpowiednich funkcji
  157. int main(int argc, const char *argv[]) {
  158.     if (argc == 1) {
  159.         printDir(".");
  160.     } else if (argc == 2) {
  161.         printDetails(".", argv[1]);
  162.     } else {
  163.         perror("Unsupported operation");
  164.     }
  165.     return 0;
  166. }