myls.c 8.7 KB

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