Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. #include <sys/types.h> /* Typy zmiennych używane w systemie */
  2. #include <sys/stat.h> /* Funkcja stat pozwalająca pobrać informacje o pliku */
  3. #include <stdio.h> /* standard input/output */
  4. #include <unistd.h> /* standardowe funkcje unixa */
  5. #include <dirent.h> /* funkcje i definicje pozwalające używać struktury direntry */
  6. #include <string.h> /* funkcje pozwalające manipulować łańcuchami */
  7. #include <stdlib.h> /* biblioteka standardowa C */
  8. #include <time.h> /* funkcje pozwalające manipulować czasem */
  9. #include <pwd.h> /* struktura passwd i funkcje do manipulacji nią */
  10. #include <grp.h> /* struktura group i funkcje do manipulacji nią */
  11. #include <fcntl.h> /* kontrolne funkcje systemowe */
  12.  
  13. void ls(char *name)
  14. {
  15. DIR *dir;
  16. struct dirent *dp;
  17. struct stat mystat;
  18. struct tm lt;
  19. struct passwd *pwd;
  20. struct group *grp;
  21.  
  22.  
  23. char buf[1024];
  24. dir = opendir(name);
  25. while((dp = readdir(dir)) != NULL)
  26. {
  27. if(dp->d_name[0] != '.') /* Bez ukrytych plikow */
  28. {
  29. sprintf(buf, "%s/%s", name, dp->d_name);
  30. stat(buf, &mystat);
  31.  
  32. /* Typ i prawa */
  33. printf( (S_ISDIR(mystat.st_mode)) ? "d" : "-");
  34. printf( (mystat.st_mode & S_IRUSR) ? "r" : "-");
  35. printf( (mystat.st_mode & S_IWUSR) ? "w" : "-");
  36. printf( (mystat.st_mode & S_IXUSR) ? "x" : "-");
  37. printf( (mystat.st_mode & S_IRGRP) ? "r" : "-");
  38. printf( (mystat.st_mode & S_IWGRP) ? "w" : "-");
  39. printf( (mystat.st_mode & S_IXGRP) ? "x" : "-");
  40. printf( (mystat.st_mode & S_IROTH) ? "r" : "-");
  41. printf( (mystat.st_mode & S_IWOTH) ? "w" : "-");
  42. printf( (mystat.st_mode & S_IXOTH) ? "x " : "- ");
  43.  
  44. /* Wypisywanie ilości dowiązań */
  45. printf("\t %d",mystat.st_nlink);
  46.  
  47. /* Uzytkownik */
  48. pwd = getpwuid(mystat.st_uid);
  49. printf("\t %s", pwd->pw_name);
  50.  
  51. /* Grupa */
  52. grp=getgrgid(mystat.st_gid);
  53. printf("\t %s", grp->gr_name);
  54.  
  55. /* Rozmiar */
  56. printf("\t %lld",mystat.st_size);
  57.  
  58.  
  59. time_t t = mystat.st_mtime;
  60. localtime_r(&t, &lt);
  61. char timebuf[80];
  62.  
  63. strftime(timebuf, sizeof(timebuf), "%d %b %H:%M", &lt);
  64.  
  65. /* Czas dostępu */
  66. printf("\t %s", timebuf);
  67.  
  68. /* Nazwa pliku */
  69. printf("\t %s\n", dp->d_name);
  70. }
  71. }
  72. closedir(dir);
  73. }
  74. char* human_readable(double size, char* buf)
  75. {
  76. const char* units[] = { "B", "K", "M", "G", "T" };
  77. int i = 0;
  78.  
  79. while (size > 1024)
  80. {
  81. size /= 1024;
  82. i++;
  83. }
  84.  
  85. sprintf(buf, "%.*f%s", i, size, units[i]);
  86. return buf;
  87. }
  88.  
  89. void ls_with_h(char *name)
  90. {
  91. DIR *dir;
  92. struct dirent *dp;
  93. struct stat mystat;
  94. struct tm lt;
  95. struct passwd *pwd;
  96. struct group *grp;
  97.  
  98.  
  99. char buf[1024];
  100. dir = opendir(name);
  101. while((dp = readdir(dir)) != NULL)
  102. {
  103. if(dp->d_name[0] != '.') /* Bez ukrytych plikow */
  104. {
  105. sprintf(buf, "%s/%s", name, dp->d_name);
  106. stat(buf, &mystat);
  107.  
  108. /* Typ i prawa */
  109. printf( (S_ISDIR(mystat.st_mode)) ? "d" : "-");
  110. printf( (mystat.st_mode & S_IRUSR) ? "r" : "-");
  111. printf( (mystat.st_mode & S_IWUSR) ? "w" : "-");
  112. printf( (mystat.st_mode & S_IXUSR) ? "x" : "-");
  113. printf( (mystat.st_mode & S_IRGRP) ? "r" : "-");
  114. printf( (mystat.st_mode & S_IWGRP) ? "w" : "-");
  115. printf( (mystat.st_mode & S_IXGRP) ? "x" : "-");
  116. printf( (mystat.st_mode & S_IROTH) ? "r" : "-");
  117. printf( (mystat.st_mode & S_IWOTH) ? "w" : "-");
  118. printf( (mystat.st_mode & S_IXOTH) ? "x " : "- ");
  119.  
  120. /* Wypisywanie ilości dowiązań */
  121. printf("\t %d",mystat.st_nlink);
  122.  
  123. /* Uzytkownik */
  124. pwd = getpwuid(mystat.st_uid);
  125. printf("\t %s", pwd->pw_name);
  126.  
  127. /* Grupa */
  128. grp=getgrgid(mystat.st_gid);
  129. printf("\t %s", grp->gr_name);
  130.  
  131. /* Rozmiar z uzyciem funkcji human_readable*/
  132. printf(" %s ", human_readable(mystat.st_size, buf));
  133.  
  134.  
  135. time_t t = mystat.st_mtime;
  136. localtime_r(&t, &lt);
  137. char timebuf[80];
  138.  
  139. strftime(timebuf, sizeof(timebuf), "%d %b %H:%M", &lt);
  140.  
  141. /* Czas dostępu */
  142. printf("\t %s", timebuf);
  143.  
  144. /* Nazwa pliku */
  145. printf("\t %s\n", dp->d_name);
  146. }
  147. }
  148. closedir(dir);
  149. }
  150.  
  151.  
  152. void help()
  153. {
  154. printf("[Just running the program | Display list of files]\n");
  155. printf("[--help | Show help]\n");
  156. printf("[--version | Display version of ls]\n");
  157. printf("[-h | Print sizes in human readable format]\n");
  158. printf("[-R | Display ls -l for Directory and all directories in it]\n\n");
  159. }
  160.  
  161. void version()
  162. {
  163. printf("Wersja 0.0.1\n\n");
  164. }
  165.  
  166.  
  167.  
  168.  
  169. int main(int argc, char* argv[])
  170. {
  171. int i;
  172.  
  173. if(argc<2)
  174. {
  175. ls(".");
  176. }
  177.  
  178. else
  179. {
  180. for (i=1; i<argc; i++)
  181. {
  182. if (strcmp(argv[i],"--help")==0)
  183. {
  184. help();
  185. }
  186.  
  187. else if (strcmp(argv[i],"--version")==0)
  188. {
  189. version();
  190. }
  191.  
  192. else if(strcmp(argv[i],"-h")==0)
  193. {
  194. printf("%s:\n", argv[i]);
  195. ls_with_h(argv[i]);
  196. printf("\n");
  197. }
  198.  
  199. else
  200. {
  201. printf("%s:\n", argv[i]);
  202. ls(argv[i]);
  203. printf("\n");
  204. }
  205. }
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement