myls.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. void lsBasics(int argc, char* argv[]){
  12. char buffer[BUFFER_SIZE];
  13. struct dirent** contentsDir;
  14. struct stat info;
  15. char path[BUFFER_SIZE];
  16. char permission[10] = "----------";
  17. int i = 0, nbFile, opt;
  18. boolean hiddenFile = false;
  19. boolean checksubDir = false;
  20. //Recuperation chemin actuel
  21. if (getcwd(buffer, sizeof (buffer)) == NULL) {
  22. addperror("Erreur getcwd()");
  23. return;
  24. }
  25. //Ouverture et lecture DIR - inutile
  26. /*if((path = opendir(buffer)) == NULL){
  27. addperror("Erreur opendir()")
  28. return;
  29. }*/
  30. //Recup la liste des fichiers dans le dossier courant
  31. nbFile = scandir(".", &contentsDir, 0, alphasort);
  32. if (nbFile < 0) {
  33. addperror("Erreur scandir()");
  34. return;
  35. }
  36. //Gestion des options
  37. while((opt = getopt(argc, argv, "aR")) != ERR){
  38. switch(opt){
  39. case 'a' :
  40. hiddenFile = true;
  41. break;
  42. case 'R' :
  43. checksubDir = true;
  44. break;
  45. default:
  46. addperror("getotp error");
  47. }
  48. }
  49. //enleve les fichiers caches
  50. while(i < nbFile && !hiddenFile && *contentsDir[i]->d_name == '.') i++;
  51. //Affiche les fichiers
  52. while(i < nbFile){
  53. strcpy(path, buffer);
  54. strcat(path, "/");
  55. strcat(path, contentsDir[i]->d_name);
  56. if(stat(path, &info) == ERR)
  57. addperror("Erreur stat");
  58. printf("%d %ld %s\n", info.st_mode, info.st_size, contentsDir[i]->d_name);
  59. i++;
  60. }
  61. printf("%d %d\n", hiddenFile, checksubDir);
  62. return;
  63. }
  64. int main(int argc, char* argv[]){
  65. lsBasics(argc, argv);
  66. printf(RESET);
  67. }