Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <dirent.h> // за struct dirent, readdir(dp)
  6. #include <sys/stat.h> // за fstat
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <sys/ioctl.h> // за winsize struct
  10. #include <pwd.h>
  11. #include <grp.h>
  12. #include <time.h>
  13.  
  14. #ifdef HAVE_ST_BIRTHTIME
  15. #define birthtime(x) x.st_birthtime
  16. #else
  17. #define birthtime(x) x.st_ctime
  18. #endif
  19.  
  20. int main(int argc, char* argv[])
  21. {
  22. char *current_dir = NULL;
  23. DIR *dp = NULL;
  24. struct dirent *dptr = NULL; // dirent - структура, която връща информация за въведената директория
  25. unsigned int count = 0;
  26. long *ptr = NULL;
  27. struct winsize w; // w = screen_size
  28. int num_files = 0; //Променлива, която държи броя файлове в текущата директория
  29. char arguments;
  30.  
  31. //ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); //to get the number of rows and column visible on terminal
  32. current_dir = getenv("PWD"); //взимаме текущата директория чрез getenv("PWD") ако върне нула значи има грешка
  33.  
  34. if(current_dir == NULL)
  35. {
  36. printf("\n ERROR : Could not get the working directory\n");
  37. return -1;
  38. }
  39.  
  40. if (strcmp(argv[1], "ls") == 0 && argc == 2)
  41. {
  42. dp = opendir((const char*)current_dir); // open the directory
  43.  
  44. while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
  45. {
  46. if(dptr -> d_name[0] != '.') //ne broi failovete koito zapochvat s '.'
  47. {
  48. num_files++;
  49. }
  50. }
  51.  
  52. closedir(dp); //zatvarqme q
  53.  
  54. dp = NULL;
  55. dptr = NULL;
  56.  
  57. if(!num_files) //proverqvame dali ima failove v direktoriqta
  58. {
  59. return 0;
  60. }
  61. else
  62. {
  63. ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
  64.  
  65. if(ptr == NULL)
  66. {
  67. printf("\n Memory allocation failed\n");
  68. return -1;
  69. }
  70. else
  71. {
  72. memset(ptr, 0, num_files*8); // inicializirame pametta s 0li
  73. //ptr - pointer to the block of memory to fill
  74. // 0 = value to be set.
  75. // 8 = num - number of bytes to be set to the value
  76. // num_files - an insigned int type
  77. }
  78. }
  79.  
  80.  
  81. dp = opendir((const char*)current_dir);
  82. if(dp == NULL)
  83. {
  84. printf("\n ERROR : Could not open the working directory\n");
  85. free(ptr);
  86. return -1;
  87. }
  88.  
  89. // Start iterating the directory and read all its contents
  90. // inside an array allocated above.
  91. unsigned int j = 0;
  92. for(count = 0; NULL != (dptr = readdir(dp)); count++) // NULL != (dptr = readdir(dp)) ако е различно от NULL значи няма грешка
  93. {
  94. if(dptr->d_name[0] != '.')
  95. {
  96. ptr[j] = (long)dptr->d_name;
  97. j++;
  98. }
  99. }
  100.  
  101. // Start displaying on console.
  102. for(count = 0; count< num_files; count++)
  103. {
  104. // Check if the file/folder is executable.
  105. if(!access((const char*)ptr[count],X_OK))
  106. {
  107. int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
  108. struct stat st;
  109.  
  110. fd = open((char*)ptr[count], O_RDONLY, 0);
  111. if(-1 == fd)
  112. {
  113. printf("\n Opening file/Directory failed\n");
  114. free(ptr);
  115. return -1;
  116. }
  117.  
  118. fstat(fd, &st);
  119. if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
  120. {
  121. // If folder
  122. printf("d %s\n",(char*)ptr[count]);
  123. }
  124. else
  125. {
  126. // If executable file
  127. printf("%s\n",(char*)ptr[count]);
  128. }
  129. close(fd);
  130. }
  131. else
  132. {
  133. // If normal file, print by the default way(black color)
  134. printf("- %s\n",(char*)ptr[count]);
  135. }
  136. }
  137. printf("\n");
  138. }
  139.  
  140. else if (strcmp(argv[1], "-a") == 0)
  141. {
  142. dp = opendir((const char*)current_dir); // opne the directory
  143.  
  144. while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
  145. {
  146. num_files++;
  147. }
  148.  
  149. closedir(dp); // close the directory
  150.  
  151. dp = NULL;
  152. dptr = NULL;
  153.  
  154. if(!num_files) //proverqvame dali ima failove v direktoriqta
  155. {
  156. return 0;
  157. }
  158. else
  159. {
  160. ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
  161.  
  162. if(ptr == NULL)
  163. {
  164. printf("\n Memory allocation failed\n");
  165. return -1;
  166. }
  167. else
  168. {
  169. memset(ptr, 0, num_files*8); // Initialize the memory by zeros
  170. }
  171. }
  172.  
  173. dp = opendir((const char*)current_dir);
  174.  
  175. if(dp == NULL)
  176. {
  177. printf("\n ERROR : Could not open the working directory\n");
  178. free(ptr);
  179. return -1;
  180. }
  181.  
  182. //Chetem sudurjanieto na direktoriqta
  183. unsigned int j = 0;
  184. for(count = 0;(dptr = readdir(dp)) != NULL; count++) // (dptr = readdir(dp)) != NULL ако е различно от NULL значи няма грешка
  185. {
  186. ptr[j] = (long)dptr -> d_name;
  187. j++;
  188. }
  189.  
  190. for(count = 0; count< num_files; count++)
  191. {
  192. if(!access((const char*)ptr[count],X_OK)) // Proverqva dali failut/papkata e executable.
  193. {
  194. int fd = -1; // fd - file descriptor = indetifikator, koito izpolzvame za daden fail
  195. struct stat st;
  196.  
  197. fd = open((char*)ptr[count], O_RDONLY, 0);
  198. if(fd == -1)
  199. {
  200. printf("\n Opening file/Directory failed\n");
  201. free(ptr);
  202. return -1;
  203. }
  204.  
  205. fstat(fd, &st);
  206. if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
  207. {
  208. // If folder
  209. printf("d %s\n",(char*)ptr[count]);
  210. }
  211. else
  212. {
  213. // If executable file
  214. printf("%s\n",(char*)ptr[count]);
  215. }
  216. close(fd);
  217. }
  218. else
  219. {
  220. // If normal file, print by the default way(black color)
  221. printf("- %s\n",(char*)ptr[count]);
  222. }
  223. }
  224. printf("\n");
  225. }
  226. else if (strcmp(argv[1], "-l") == 0)
  227. {
  228. dp = opendir((const char*)current_dir); // opne the directory
  229.  
  230. while((dptr = readdir(dp)) != NULL) //zapochvame da chetem sudurjanieto na direktoriqta
  231. {
  232. if(dptr -> d_name[0] != '.') //ne broi failovete koito zapochvat s '.'
  233. {
  234. num_files++;
  235. }
  236. }
  237.  
  238. closedir(dp); //zatvarqme q
  239.  
  240. dp = NULL;
  241. dptr = NULL;
  242.  
  243. if(!num_files) //proverqvame dali ima failove v direktoriqta
  244. {
  245. return 0;
  246. }
  247. else
  248. {
  249. ptr = malloc(num_files*8); //zadelqme pamet za adresine na imenata na sudurjanieto na direktoriqta
  250.  
  251. if(ptr == NULL)
  252. {
  253. printf("\n Memory allocation failed\n");
  254. return -1;
  255. }
  256. else
  257. {
  258. memset(ptr, 0, num_files*8); // inicializirame pametta s 0li
  259. //ptr - pointer to the block of memory to fill
  260. // 0 = value to be set.
  261. // 8 = num - number of bytes to be set to the value
  262. // num_files - an insigned int type
  263. }
  264. }
  265.  
  266.  
  267. dp = opendir((const char*)current_dir);
  268. if(dp == NULL)
  269. {
  270. printf("\n ERROR : Could not open the working directory\n");
  271. free(ptr);
  272. return -1;
  273. }
  274.  
  275. // Start iterating the directory and read all its contents
  276. // inside an array allocated above.
  277. unsigned int j = 0;
  278. for(count = 0; NULL != (dptr = readdir(dp)); count++) // NULL != (dptr = readdir(dp)) ако е различно от NULL значи няма грешка
  279. {
  280. if(dptr->d_name[0] != '.')
  281. {
  282. ptr[j] = (long)dptr->d_name;
  283. j++;
  284. }
  285. }
  286.  
  287. // Start displaying on console.
  288. for(count = 0; count< num_files; count++)
  289. {
  290.  
  291. // Check if the file/folder is executable.
  292. if(!access((const char*)ptr[count],X_OK))
  293. {
  294. int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
  295. struct stat st;
  296.  
  297. fd = open((char*)ptr[count], O_RDONLY, 0);
  298. if(-1 == fd)
  299. {
  300. printf("\n Opening file/Directory failed\n");
  301. free(ptr);
  302. return -1;
  303. }
  304.  
  305. fstat(fd, &st);
  306. if(S_ISDIR(st.st_mode)) // S_ISDIR - проверява дали това е файл или директория
  307. {
  308. // If folder
  309.  
  310.  
  311. printf( (S_ISDIR(st.st_mode)) ? "d" : "-");
  312. printf( (st.st_mode & S_IRUSR) ? "r" : "-");
  313. printf( (st.st_mode & S_IWUSR) ? "w" : "-");
  314. printf( (st.st_mode & S_IXUSR) ? "x" : "-");
  315. printf( (st.st_mode & S_IRGRP) ? "r" : "-");
  316. printf( (st.st_mode & S_IWGRP) ? "w" : "-");
  317. printf( (st.st_mode & S_IXGRP) ? "x" : "-");
  318. printf( (st.st_mode & S_IROTH) ? "r" : "-");
  319. printf( (st.st_mode & S_IWOTH) ? "w" : "-");
  320. printf( (st.st_mode & S_IXOTH) ? "x" : "-"); //permissions
  321.  
  322. printf(" %ld",st.st_nlink); //number of links
  323.  
  324. uid_t uid = st.st_uid;
  325. struct passwd *pass = getpwuid(uid);
  326. printf(" %s",pass -> pw_name); //user
  327.  
  328. gid_t gid = st.st_gid;
  329. struct group *grp = getgrgid(gid);
  330. printf(" %s", grp -> gr_name); //group
  331.  
  332. printf(" %ld",st.st_size); //size
  333.  
  334. struct timeval tv;
  335. char str[100];
  336. struct tm *tm;
  337.  
  338. tm = localtime( (time_t*)&st.st_mtim );
  339.  
  340. strftime(str, sizeof(str), "%b %d %H:%M", tm);
  341. printf(" %s", str); //date
  342.  
  343. printf(" %s\n",(char*)ptr[count]); //file name
  344. }
  345. else // If executable file
  346. {
  347. //if()
  348. printf( (S_ISDIR(st.st_mode)) ? "d" : "-");
  349. printf( (st.st_mode & S_IRUSR) ? "r" : "-");
  350. printf( (st.st_mode & S_IWUSR) ? "w" : "-");
  351. printf( (st.st_mode & S_IXUSR) ? "x" : "-");
  352. printf( (st.st_mode & S_IRGRP) ? "r" : "-");
  353. printf( (st.st_mode & S_IWGRP) ? "w" : "-");
  354. printf( (st.st_mode & S_IXGRP) ? "x" : "-");
  355. printf( (st.st_mode & S_IROTH) ? "r" : "-");
  356. printf( (st.st_mode & S_IWOTH) ? "w" : "-");
  357. printf( (st.st_mode & S_IXOTH) ? "x" : "-"); //permissions
  358.  
  359. printf(" %ld",st.st_nlink); //number of links
  360.  
  361. uid_t uid = st.st_uid;
  362. struct passwd *pass = getpwuid(uid);
  363. printf(" %s",pass -> pw_name); //user
  364.  
  365. gid_t gid = st.st_gid;
  366. struct group *grp = getgrgid(gid);
  367. printf(" %s", grp -> gr_name); //group
  368.  
  369. printf(" %ld",st.st_size); //size
  370.  
  371. struct timeval tv;
  372. char str[100];
  373. struct tm *tm;
  374.  
  375. tm = localtime( (time_t*)&st.st_mtim );
  376.  
  377. strftime(str, sizeof(str), "%b %d %H:%M", tm);
  378. printf(" %s", str); //date
  379.  
  380. printf("- %s\n",(char*)ptr[count]);
  381. }
  382. close(fd);
  383. }
  384. else // If normal file
  385. {
  386. int fd = -1; // fd - file descriptor = индентификатор, който използваме за достъп до даден файл
  387. struct stat st;
  388.  
  389. fd = open((char*)ptr[count], O_RDONLY, 0);
  390. if(-1 == fd)
  391. {
  392. printf("\n Opening file/Directory failed\n");
  393. free(ptr);
  394. return -1;
  395. }
  396.  
  397. fstat(fd, &st);
  398.  
  399. printf( (S_ISDIR(st.st_mode)) ? "d" : "-");
  400. printf( (st.st_mode & S_IRUSR) ? "r" : "-");
  401. printf( (st.st_mode & S_IWUSR) ? "w" : "-");
  402. printf( (st.st_mode & S_IXUSR) ? "x" : "-");
  403. printf( (st.st_mode & S_IRGRP) ? "r" : "-");
  404. printf( (st.st_mode & S_IWGRP) ? "w" : "-");
  405. printf( (st.st_mode & S_IXGRP) ? "x" : "-");
  406. printf( (st.st_mode & S_IROTH) ? "r" : "-");
  407. printf( (st.st_mode & S_IWOTH) ? "w" : "-");
  408. printf( (st.st_mode & S_IXOTH) ? "x" : "-"); //permissions
  409.  
  410. printf(" %ld",st.st_nlink); //number of links
  411.  
  412. uid_t uid = st.st_uid;
  413. struct passwd *pass = getpwuid(uid);
  414. printf(" %s",pass -> pw_name); //user
  415.  
  416. gid_t gid = st.st_gid;
  417. struct group *grp = getgrgid(gid);
  418. printf(" %s", grp -> gr_name); //group
  419.  
  420. printf(" %ld",st.st_size); //size
  421.  
  422. struct timeval tv;
  423. char str[100];
  424. struct tm *tm;
  425.  
  426. tm = localtime( (time_t*)&st.st_mtim );
  427.  
  428. strftime(str, sizeof(str), "%b %d %H:%M", tm);
  429. printf(" %s", str); //date
  430.  
  431. printf(" %s\n",(char*)ptr[count]); //file name
  432. }
  433. }
  434. printf("\n");
  435. }
  436. else if (strcmp(argv[1], "ls") == 0 && strcmp(argv[2], "-R") == 0)
  437. {
  438. printf("LS -R\n");
  439. }
  440.  
  441. else { printf("ls: cannot access %s: No such file or directory\n", argv[2]); }
  442.  
  443. free(ptr); //Free the allocated memory
  444. return 0;
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement