myls.c 3.3 KB

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