Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. 2. Podaj różnice między dowiązaniem symbolicznym, a zwykłym.
  2.  
  3. Zadanie - rozszerzyć interpreter ls z materiałów przykładowych o opcje -a (nie pomija ukrytych) i -R (rekurencyjnie wyświetla zawartości podkatalogów) korzystając z getopt. W razie nie podania ścieżki do katalogu ma operować na bieżącym. Miało działać zarówno ls -a -R jak i ls -aR.
  4.  
  5.  
  6. Kod:
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/param.h>
  14. #include <dirent.h>
  15. #include <errno.h>
  16. #include <string.h>
  17.  
  18. int pokazkat(char *kat, int af, int rf)
  19. {
  20. DIR *dir;
  21. struct dirent *pozycja;
  22. struct stat info;
  23. int i;
  24.  
  25. if(chdir(kat) == -1) {
  26. perror("Blad chdir");
  27. return 1;
  28. }
  29.  
  30. if((dir = opendir(".")) == NULL) {
  31. perror("Blad opendir");
  32. return 1;
  33. }
  34.  
  35. errno = 0;
  36. while((pozycja = readdir(dir)) != NULL)
  37. {
  38. if(!af) if(pozycja->d_name[0] == '.') continue;
  39.  
  40. if(rf) for(i=0; i<rf-1; i++) printf("+");
  41. printf("%s\n", pozycja->d_name);
  42. if(rf) {
  43. if(lstat(pozycja->d_name, &info) == -1) {
  44. perror("Blad lstat");
  45. return 1;
  46. }
  47. if(S_ISDIR(info.st_mode) &&
  48. pozycja->d_name[1] != '.' &&
  49. strcmp(pozycja->d_name, "."))
  50. pokazkat(pozycja->d_name, af, rf+1);
  51. }
  52. }
  53. if(rf>1) {
  54. if(chdir("..") == -1)
  55. {
  56. perror("Blad chdir");
  57. return 1;
  58. }
  59. }
  60.  
  61. if(errno) {
  62. printf("%s ", pozycja->d_name);
  63. perror("Blad readdir");
  64. return 1;
  65. }
  66.  
  67. if(closedir(dir) == -1) {
  68. perror("Blad closedir");
  69. return 1;
  70. }
  71.  
  72. return 0;
  73. }
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77. int aflag = 0, rflag = 0, c;
  78.  
  79. while((c = getopt(argc, argv, "aR")) != -1) {
  80. switch(c) {
  81. case 'a': aflag = 1; break;
  82. case 'R': rflag = 1; break;
  83. case '?': printf("Nieprawidlowy parametr: %c\n", optopt);
  84. }
  85. }
  86.  
  87. if(argv[optind] != NULL) {
  88. return pokazkat(argv[optind], aflag, rflag);
  89. } else {
  90. return pokazkat(".", aflag, rflag);
  91. }
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement