myls.c 8.2 KB

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