myls.c 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #define _DEFAULT_SOURCE
  2. #include <dirent.h>
  3. #include "parser.h"
  4. void lsBasics(int argc, char* argv[]){
  5. char buffer[BUFFER_SIZE];
  6. struct dirent** contentsDir;
  7. int i = 0, nbFile;
  8. //Recuperation chemin actuel
  9. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  10. addperror("Erreur getcwd()");
  11. return;
  12. }
  13. //Ouverture et lecture DIR
  14. if((path = opendir(buffer)) == NULL){
  15. addperror("Erreur opendir()")
  16. return;
  17. }
  18. //Recup la liste des fichiers dans le dossier courant
  19. nbFile = scandir(buffer, &contentsDir, 0, alphasort);
  20. if (nbFile < 0) {
  21. addperror("Erreur scandir()");
  22. return;
  23. }
  24. //Affiche les fichiers
  25. while(i < nbFile){
  26. printf("%s\n", contentsDir[i]->d_name);
  27. i++;
  28. }
  29. return;
  30. }
  31. int main(int argc, char* argv[]){
  32. lsBasics(argc, argv);
  33. }