myls.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #define _POSIX_C_SOURCE 200809L
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <dirent.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <grp.h>
  10. #include <pwd.h>
  11. #include "error.h"
  12. #include "color.h"
  13. #include "myls.h"
  14. void printls(char* path, char* filename){
  15. int length;
  16. char* completePath;
  17. char permission[11];
  18. char mois[5];
  19. struct stat info;
  20. struct group* grp;
  21. struct passwd* user;
  22. struct tm* date;
  23. //Si le nom est en 2 partie
  24. if(filename != NULL){
  25. //Création chemin vers le fichier
  26. length = strlen(path) + strlen(filename) + 2;
  27. completePath = malloc(sizeof(char) * length);
  28. memset(completePath, 0, length);
  29. if(path[strlen(path)-1] != '/'){
  30. snprintf(completePath, length, "%s/%s", path, filename);
  31. } else {
  32. snprintf(completePath, length, "%s%s", path, filename);
  33. }
  34. } else {
  35. filename = path;
  36. completePath = path;
  37. path = NULL;
  38. }
  39. //Recup info fichier
  40. if(stat(completePath, &info) == ERR){
  41. addperror("Erreur stat");
  42. return;
  43. }
  44. if(path != NULL){
  45. free(completePath);
  46. }
  47. //Calcul permission
  48. memset(permission, 0, 11);
  49. if(S_ISDIR(info.st_mode)){
  50. permission[0] = 'd';
  51. }
  52. else if(S_ISBLK(info.st_mode)){
  53. permission[0] = 'b';
  54. }
  55. else if(S_ISCHR(info.st_mode)){
  56. permission[0] = 'c';
  57. }
  58. #ifdef S_ISFIFO
  59. else if(S_ISFIFO(info.st_mode)){
  60. permission[0] = 'p';
  61. }
  62. #endif
  63. #ifdef S_ISLINK
  64. else if(S_ISLINK(info.st_mode)){
  65. permission[0] = 'l';
  66. }
  67. #endif
  68. #ifdef S_ISSOCK
  69. else if(S_ISSOCK(info.st_mode)){
  70. permission[0] = 's';
  71. }
  72. #endif
  73. else
  74. permission[0] = '-';
  75. info.st_mode & S_IRUSR ? (permission[1] = 'r') : (permission[1] = '-');
  76. info.st_mode & S_IWUSR ? (permission[2] = 'w') : (permission[2] = '-');
  77. info.st_mode & S_IXUSR ? (permission[3] = 'x') : (permission[3] = '-');
  78. info.st_mode & S_IRGRP ? (permission[4] = 'r') : (permission[4] = '-');
  79. info.st_mode & S_IWGRP ? (permission[5] = 'w') : (permission[5] = '-');
  80. info.st_mode & S_IXGRP ? (permission[6] = 'x') : (permission[6] = '-');
  81. info.st_mode & S_IROTH ? (permission[7] = 'r') : (permission[7] = '-');
  82. info.st_mode & S_IWOTH ? (permission[8] = 'w') : (permission[8] = '-');
  83. info.st_mode & S_IXOTH ? (permission[9] = 'x') : (permission[9] = '-');
  84. //Recup le groupe et l'utilisateur
  85. grp = getgrgid(info.st_gid);
  86. user = getpwuid(info.st_uid);
  87. //Recup la date
  88. memset(mois, 0, 5);
  89. date = gmtime(&info.st_mtime);
  90. switch(date->tm_mon){
  91. case 0:
  92. strcpy(mois, "jan.");
  93. break;
  94. case 1:
  95. strcpy(mois, "fev.");
  96. break;
  97. case 2:
  98. strcpy(mois, "mar.");
  99. break;
  100. case 3:
  101. strcpy(mois, "avr.");
  102. break;
  103. case 4:
  104. strcpy(mois, "mai.");
  105. break;
  106. case 5:
  107. strcpy(mois, "jui.");
  108. break;
  109. case 6:
  110. strcpy(mois, "jul.");
  111. break;
  112. case 7:
  113. strcpy(mois, "aou.");
  114. break;
  115. case 8:
  116. strcpy(mois, "sep.");
  117. break;
  118. case 9:
  119. strcpy(mois, "oct.");
  120. break;
  121. case 10:
  122. strcpy(mois, "nov.");
  123. break;
  124. case 11:
  125. strcpy(mois, "dec.");
  126. break;
  127. }
  128. //Affiche
  129. printf("%s X %s %s %ld %s %d %d:%d ", permission, user->pw_name, grp->gr_name, info.st_size, mois, date->tm_mday, date->tm_hour, date->tm_min);
  130. //color the name
  131. if(permission[0] == 'd'){
  132. printf(BLUE "%s\n" RESET, filename);
  133. }
  134. else if(permission[3] == 'x'){
  135. printf(GREEN "%s\n" RESET, filename);
  136. }
  137. else{
  138. printf("%s\n", filename);
  139. }
  140. }
  141. void printdir(char* path, boolean subdir, boolean hidden){
  142. struct dirent** contentsDir;
  143. int nbFile;
  144. int j = 0;
  145. //Recup info
  146. if((nbFile = scandir(path, &contentsDir, 0, alphasort)) == ERR){
  147. addperror("Erreur scandir()");
  148. return;
  149. }
  150. //Si sous dossier on affiche le dossier d'origine
  151. if(subdir){
  152. printf("%s :\n", path);
  153. }
  154. //Si besoins passe les fichiers cachés
  155. while(j < nbFile && !hidden && *contentsDir[j]->d_name == '.') j++;
  156. //Parcours les fichiers du dossier
  157. while(j < nbFile){
  158. printls(path, contentsDir[j]->d_name);
  159. j++;
  160. }
  161. //Si on affiche les sous dossiers on parcours le contenue pour les trouver
  162. if(subdir){
  163. char* completePath;
  164. int length;
  165. struct stat info;
  166. j = 0;
  167. //Si besoins passe les fichiers cachés
  168. while(j < nbFile && !hidden && *contentsDir[j]->d_name == '.') j++;
  169. //Cherche les sous dossiers
  170. while(j < nbFile){
  171. length = strlen(contentsDir[j]->d_name);
  172. if(strncmp(contentsDir[j]->d_name, ".", length) == 0 || strncmp(contentsDir[j]->d_name, "..", length) == 0){
  173. j++;
  174. continue;
  175. }
  176. //Création chemin vers le dossier
  177. length += strlen(path) + 2;
  178. completePath = malloc(sizeof(char) * length);
  179. memset(completePath, 0, length);
  180. if(path[strlen(path)-1] != '/'){
  181. snprintf(completePath, length, "%s/%s", path, contentsDir[j]->d_name);
  182. } else {
  183. snprintf(completePath, length, "%s%s", path, contentsDir[j]->d_name);
  184. }
  185. //Recup info fichier
  186. if(stat(completePath, &info) == ERR){
  187. addperror("Erreur stat");
  188. free(completePath);
  189. j++;
  190. continue;
  191. }
  192. //Si c'est un dossier
  193. if(S_ISDIR(info.st_mode)){
  194. //On l'affiche
  195. printf("\n");
  196. printdir(completePath, subdir, hidden);
  197. }
  198. //Tour suivant
  199. free(completePath);
  200. j++;
  201. }
  202. }
  203. //Nettoyage
  204. while (nbFile--) {
  205. free(contentsDir[nbFile]);
  206. }
  207. free(contentsDir);
  208. }
  209. int main(int argc, char* argv[]){
  210. struct stat info;
  211. int i = 1, displayed = 0, opt;
  212. boolean hiddenFile = false;
  213. boolean displaySubDir = false;
  214. //Gestion des options
  215. while((opt = getopt(argc, argv, "aR")) != ERR){
  216. switch(opt){
  217. case 'a' :
  218. hiddenFile = true;
  219. break;
  220. case 'R' :
  221. displaySubDir = true;
  222. break;
  223. default:
  224. addperror("getotp error");
  225. }
  226. }
  227. //Time to display
  228. for(i = 1; i < argc; i++){
  229. if(argv[i][0] != '-'){
  230. if(stat(argv[i], &info) == ERR){
  231. addperror("Erreur stat");
  232. return EXIT_FAILURE;
  233. }
  234. if(S_ISDIR(info.st_mode)){
  235. //Affiche le dossier
  236. printdir(argv[i], displaySubDir, hiddenFile);
  237. displayed = true;
  238. }
  239. else{
  240. printls(argv[i], NULL);
  241. displayed = true;
  242. }
  243. }
  244. }
  245. //Si aucun dossier ou fichier en argument
  246. if(!displayed){
  247. //On affiche le dossier courrant
  248. printdir(".", displaySubDir, hiddenFile);
  249. }
  250. return EXIT_SUCCESS;
  251. }