myls.c 3.5 KB

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