myls.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #define _DEFAULT_SOURCE
  2. #include <dirent.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "parser.h"
  8. #include "error.h"
  9. #include "color.h"
  10. #include "constante.h"
  11. int printDir(struct dirent** contentsDir, char* path, int nbFile, struct stat info,
  12. boolean hiddenFile){
  13. //char permission[10] = "----------";
  14. int j = 0;
  15. //enleve les fichiers caches
  16. while(j < nbFile && !hiddenFile && *contentsDir[j]->d_name == '.') j++;
  17. //Affiche les fichiers
  18. while(j < nbFile){
  19. /*switch(info.st_mode){
  20. case S_IRUSR:
  21. permission[1] = 'r';
  22. case S_IWUSR:
  23. permission[2] = 'w';
  24. case S_IXUSR:
  25. permission[3] = 'x';
  26. case S_IRGRP:
  27. permission[4] = 'r';
  28. case S_IWGRP:
  29. permission[5] = 'w';
  30. case S_IXGRP:
  31. permission[6] = 'x';
  32. case S_IROTH:
  33. permission[7] = 'r';
  34. case S_IWOTH:
  35. permission[8] = 'w';
  36. case S_IXOTH:
  37. permission[9] = 'x';
  38. }*/
  39. printf("%d %ld %s\n", info.st_mode, info.st_size, contentsDir[j]->d_name);
  40. //printf("%s", permission);
  41. j++;
  42. }
  43. printf("\n");
  44. return 1;
  45. }
  46. void ls(int argc, char* argv[]){
  47. char buffer[BUFFER_SIZE];
  48. char path[BUFFER_SIZE];
  49. struct dirent** contentsDir;
  50. struct stat info;
  51. int i = 1, j = 0, displayed = 0, nbFile, opt;
  52. boolean hiddenFile = false;
  53. boolean checksubDir = false;
  54. //Recuperation chemin actuel
  55. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  56. addperror("Erreur getcwd()");
  57. return;
  58. }
  59. //Gestion des options
  60. while((opt = getopt(argc, argv, "aR")) != ERR){
  61. switch(opt){
  62. case 'a' :
  63. hiddenFile = true;
  64. break;
  65. case 'R' :
  66. checksubDir = true;
  67. break;
  68. default:
  69. addperror("getotp error");
  70. }
  71. }
  72. //Time to display
  73. for(i = 1; i < argc; i++){
  74. if(argv[i][0] != '-'){
  75. strcpy(path, buffer);
  76. strcat(path, "/");
  77. strcat(path, argv[i]);
  78. if(stat(path, &info) == ERR)
  79. addperror("Erreur stat");
  80. if(S_ISDIR(info.st_mode)){
  81. nbFile = scandir(argv[i], &contentsDir, 0, alphasort);
  82. if (nbFile < 0) {
  83. addperror("Erreur scandir()");
  84. return;
  85. }
  86. printf("%s : \n\n", argv[i]);
  87. strcpy(path, buffer);
  88. strcat(path, "/");
  89. strcat(path, contentsDir[j]->d_name);
  90. displayed = printDir(contentsDir, path, nbFile, info, hiddenFile);
  91. }
  92. else{
  93. printf("%d %ld %s\n", info.st_mode, info.st_size, argv[i]);
  94. displayed = 1;
  95. }
  96. }
  97. }
  98. if(!displayed){
  99. nbFile = scandir(".", &contentsDir, 0, alphasort);
  100. if (nbFile < 0) {
  101. addperror("Erreur scandir()");
  102. return;
  103. }
  104. strcpy(path, buffer);
  105. strcat(path, "/");
  106. strcat(path, contentsDir[j]->d_name);
  107. displayed = printDir(contentsDir, path, nbFile, info, hiddenFile);
  108. }
  109. printf("%d %d\n", hiddenFile, checksubDir);
  110. return;
  111. }
  112. int main(int argc, char* argv[]){
  113. ls(argc, argv);
  114. printf(RESET);
  115. }