myls.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. void lsBasics(int argc, char* argv[]){
  11. char buffer[BUFFER_SIZE];
  12. struct dirent** contentsDir;
  13. struct stat info;
  14. char path[BUFFER_SIZE];
  15. int i = 0, nbFile;
  16. //Recuperation chemin actuel
  17. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  18. addperror("Erreur getcwd()");
  19. return;
  20. }
  21. //Ouverture et lecture DIR
  22. /*if((path = opendir(buffer)) == NULL){
  23. addperror("Erreur opendir()")
  24. return;
  25. }*/
  26. //Recup la liste des fichiers dans le dossier courant
  27. nbFile = scandir(buffer, &contentsDir, 0, alphasort);
  28. if (nbFile < 0) {
  29. addperror("Erreur scandir()");
  30. return;
  31. }
  32. //Affiche les fichiers
  33. while(i < nbFile){
  34. strcpy(path, buffer);
  35. strcat(path, "/");
  36. strcat(path, contentsDir[i]->d_name);
  37. printf("%s\n", path);
  38. if(stat(path, &info) == ERR)
  39. addperror("Erreur stat");
  40. printf("%d %ld %s\n", info.st_mode, info.st_size, contentsDir[i]->d_name);
  41. i++;
  42. }
  43. return;
  44. }
  45. int main(int argc, char* argv[]){
  46. lsBasics(argc, argv);
  47. printf(RESET);
  48. }