myls.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. struct dirent** contentsDir;
  52. struct stat info;
  53. int i = 1, displayed = 0, nbFile, opt;
  54. boolean hiddenFile = false;
  55. boolean checksubDir = false;
  56. //Gestion des options
  57. while((opt = getopt(argc, argv, "aR")) != ERR){
  58. switch(opt){
  59. case 'a' :
  60. hiddenFile = true;
  61. break;
  62. case 'R' :
  63. checksubDir = true;
  64. break;
  65. default:
  66. addperror("getotp error");
  67. }
  68. }
  69. //Time to display
  70. for(i = 1; i < argc; i++){
  71. if(argv[i][0] != '-'){
  72. if(stat(argv[i], &info) == ERR){
  73. addperror("Erreur stat");
  74. return;
  75. }
  76. if(S_ISDIR(info.st_mode)){
  77. nbFile = scandir(argv[i], &contentsDir, 0, alphasort);
  78. if (nbFile < 0) {
  79. addperror("Erreur scandir()");
  80. return;
  81. }
  82. printf("%s : \n\n", argv[i]);
  83. displayed = printDir(contentsDir, argv[i], nbFile, hiddenFile);
  84. }
  85. else{
  86. printf("%d %ld %s\n", info.st_mode, info.st_size, argv[i]);
  87. displayed = 1;
  88. }
  89. }
  90. }
  91. if(!displayed){
  92. nbFile = scandir(".", &contentsDir, 0, alphasort);
  93. if (nbFile < 0) {
  94. addperror("Erreur scandir()");
  95. return;
  96. }
  97. displayed = printDir(contentsDir, ".", nbFile, hiddenFile);
  98. }
  99. printf("%d %d\n", hiddenFile, checksubDir);
  100. return;
  101. }
  102. int main(int argc, char* argv[]){
  103. ls(argc, argv);
  104. printf(RESET);
  105. }