123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #define _DEFAULT_SOURCE
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <string.h>
- #include "parser.h"
- #include "error.h"
- #include "color.h"
- #include "constante.h"
- int printDir(struct dirent** contentsDir, char* path, int nbFile,
- boolean hiddenFile){
- struct stat info;
- char permission[10] = "---------";
- int j = 0;
- //enleve les fichiers caches
- while(j < nbFile && !hiddenFile && *contentsDir[j]->d_name == '.') j++;
- //Affiche les fichiers
- while(j < nbFile){
- if(stat(contentsDir[j]->d_name, &info) == ERR){
- addperror("Erreur stat");
- return -1;
- }
- /*switch(info.st_mode){
- case S_IRUSR:
- permission[1] = 'r';
- case S_IWUSR:
- permission[2] = 'w';
- case S_IXUSR:
- permission[3] = 'x';
- case S_IRGRP:
- permission[4] = 'r';
- case S_IWGRP:
- permission[5] = 'w';
- case S_IXGRP:
- permission[6] = 'x';
- case S_IROTH:
- permission[7] = 'r';
- case S_IWOTH:
- permission[8] = 'w';
- case S_IXOTH:
- permission[9] = 'x';
- }*/
- printf("%s %d %ld %s\n", permission, info.st_mode, info.st_size, contentsDir[j]->d_name);
- j++;
- }
- printf("\n");
- return 1;
- }
- void ls(int argc, char* argv[]){
- char buffer[BUFFER_SIZE];
- char path[BUFFER_SIZE];
- struct dirent** contentsDir;
- struct stat info;
- int i = 1, j = 0, displayed = 0, nbFile, opt;
- boolean hiddenFile = false;
- boolean checksubDir = false;
- //Recuperation chemin actuel
- if (getcwd(buffer, sizeof (buffer)) == NULL) {
- addperror("Erreur getcwd()");
- return;
- }
- //Gestion des options
- while((opt = getopt(argc, argv, "aR")) != ERR){
- switch(opt){
- case 'a' :
- hiddenFile = true;
- break;
- case 'R' :
- checksubDir = true;
- break;
- default:
- addperror("getotp error");
- }
- }
- //Time to display
- for(i = 1; i < argc; i++){
- if(argv[i][0] != '-'){
- if(stat(argv[i], &info) == ERR){
- addperror("Erreur stat");
- return;
- }
- if(S_ISDIR(info.st_mode)){
- nbFile = scandir(argv[i], &contentsDir, 0, alphasort);
- if (nbFile < 0) {
- addperror("Erreur scandir()");
- return;
- }
- printf("%s : \n\n", argv[i]);
- strcpy(path, buffer);
- strcat(path, "/");
- strcat(path, contentsDir[j]->d_name);
- displayed = printDir(contentsDir, path, nbFile, hiddenFile);
- }
- else{
- printf("%d %ld %s\n", info.st_mode, info.st_size, argv[i]);
- displayed = 1;
- }
- }
- }
- if(!displayed){
- nbFile = scandir(".", &contentsDir, 0, alphasort);
- if (nbFile < 0) {
- addperror("Erreur scandir()");
- return;
- }
- strcpy(path, buffer);
- strcat(path, "/");
- strcat(path, contentsDir[j]->d_name);
- displayed = printDir(contentsDir, path, nbFile, hiddenFile);
- }
- printf("%d %d\n", hiddenFile, checksubDir);
- return;
- }
- int main(int argc, char* argv[]){
- ls(argc, argv);
- printf(RESET);
- }
|