myls.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. if(grp == NULL){
  93. return;
  94. }
  95. user = getpwuid(info.st_uid);
  96. if(user == NULL){
  97. return;
  98. }
  99. //Recup la date
  100. memset(mois, 0, 5);
  101. date = gmtime(&info.st_mtime);
  102. switch(date->tm_mon){
  103. case 0:
  104. strcpy(mois, "jan.");
  105. break;
  106. case 1:
  107. strcpy(mois, "fev.");
  108. break;
  109. case 2:
  110. strcpy(mois, "mar.");
  111. break;
  112. case 3:
  113. strcpy(mois, "avr.");
  114. break;
  115. case 4:
  116. strcpy(mois, "mai.");
  117. break;
  118. case 5:
  119. strcpy(mois, "jui.");
  120. break;
  121. case 6:
  122. strcpy(mois, "jul.");
  123. break;
  124. case 7:
  125. strcpy(mois, "aou.");
  126. break;
  127. case 8:
  128. strcpy(mois, "sep.");
  129. break;
  130. case 9:
  131. strcpy(mois, "oct.");
  132. break;
  133. case 10:
  134. strcpy(mois, "nov.");
  135. break;
  136. case 11:
  137. strcpy(mois, "dec.");
  138. break;
  139. }
  140. //Met l'heure sur 2 chiffre
  141. memset(heure, 0, 3);
  142. if(date->tm_hour < 10){
  143. snprintf(heure, 3, "0%d", date->tm_hour);
  144. } else {
  145. snprintf(heure, 3, "%d", date->tm_hour);
  146. }
  147. //Met les minutes sur 2 chiffre
  148. memset(minute, 0, 3);
  149. if(date->tm_min < 10){
  150. snprintf(minute, 3, "0%d", date->tm_min);
  151. } else {
  152. snprintf(minute, 3, "%d", date->tm_min);
  153. }
  154. //Affiche
  155. 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);
  156. //color the name
  157. if(permission[0] == 'd'){
  158. printf(BLUE "%s\n" RESET, filename);
  159. }
  160. else if(permission[3] == 'x'){
  161. printf(GREEN "%s\n" RESET, filename);
  162. }
  163. else{
  164. printf("%s\n", filename);
  165. }
  166. }
  167. /**
  168. * Affiche tous le contenu d'un dossier
  169. * @param char* Le chemin vers le dossier
  170. * @param boolean Affiche ou non les sous dossiers
  171. * @param boolean Affiche ou non les fichiers/dossiers cachés
  172. */
  173. void printdir(char* path, boolean subdir, boolean hidden){
  174. struct dirent** contentsDir;
  175. int nbFile;
  176. int j = 0;
  177. //Recup info
  178. if((nbFile = scandir(path, &contentsDir, 0, alphasort)) == ERR){
  179. addperror("Erreur scandir()");
  180. return;
  181. }
  182. //Si sous dossier on affiche le dossier d'origine
  183. if(subdir){
  184. printf("%s :\n", path);
  185. }
  186. //Si besoins passe les fichiers cachés
  187. while(j < nbFile && !hidden && *contentsDir[j]->d_name == '.') j++;
  188. //Parcours les fichiers du dossier
  189. while(j < nbFile){
  190. printls(path, contentsDir[j]->d_name);
  191. j++;
  192. }
  193. //Si on affiche les sous dossiers on parcours le contenue pour les trouver
  194. if(subdir){
  195. char* completePath;
  196. int length;
  197. struct stat info;
  198. j = 0;
  199. //Si besoins passe les fichiers cachés
  200. while(j < nbFile && !hidden && *contentsDir[j]->d_name == '.') j++;
  201. //Cherche les sous dossiers
  202. while(j < nbFile){
  203. length = strlen(contentsDir[j]->d_name);
  204. if(strncmp(contentsDir[j]->d_name, ".", length) == 0 || strncmp(contentsDir[j]->d_name, "..", length) == 0){
  205. j++;
  206. continue;
  207. }
  208. //Création chemin vers le dossier
  209. length += strlen(path) + 2;
  210. completePath = malloc(sizeof(char) * length);
  211. memset(completePath, 0, length);
  212. if(path[strlen(path)-1] != '/'){
  213. snprintf(completePath, length, "%s/%s", path, contentsDir[j]->d_name);
  214. } else {
  215. snprintf(completePath, length, "%s%s", path, contentsDir[j]->d_name);
  216. }
  217. //Recup info fichier
  218. if(stat(completePath, &info) == ERR){
  219. addperror("Erreur stat");
  220. free(completePath);
  221. j++;
  222. continue;
  223. }
  224. //Si c'est un dossier
  225. if(S_ISDIR(info.st_mode)){
  226. //On l'affiche
  227. printf("\n");
  228. printdir(completePath, subdir, hidden);
  229. }
  230. //Tour suivant
  231. free(completePath);
  232. j++;
  233. }
  234. }
  235. //Nettoyage
  236. while (nbFile--) {
  237. free(contentsDir[nbFile]);
  238. }
  239. free(contentsDir);
  240. }
  241. int main(int argc, char* argv[]){
  242. struct stat info;
  243. int i = 1, displayed = 0, opt;
  244. boolean hiddenFile = false;
  245. boolean displaySubDir = false;
  246. //Gestion des options
  247. while((opt = getopt(argc, argv, "aR")) != ERR){
  248. switch(opt){
  249. case 'a' :
  250. hiddenFile = true;
  251. break;
  252. case 'R' :
  253. displaySubDir = true;
  254. break;
  255. default:
  256. addperror("getotp error");
  257. }
  258. }
  259. //Time to display
  260. for(i = 1; i < argc; i++){
  261. if(argv[i][0] != '-'){
  262. if(stat(argv[i], &info) == ERR){
  263. addperror("Erreur stat");
  264. return EXIT_FAILURE;
  265. }
  266. if(S_ISDIR(info.st_mode)){
  267. //Affiche le dossier
  268. printdir(argv[i], displaySubDir, hiddenFile);
  269. displayed = true;
  270. }
  271. else{
  272. printls(argv[i], NULL);
  273. displayed = true;
  274. }
  275. }
  276. }
  277. //Si aucun dossier ou fichier en argument
  278. if(!displayed){
  279. //On affiche le dossier courrant
  280. printdir(".", displaySubDir, hiddenFile);
  281. }
  282. return EXIT_SUCCESS;
  283. }