myls.c 9.8 KB

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