myls.c 3.3 KB

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