123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #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[11] = "---------";
- char completePath[1024];
- int j = 0;
- //enleve les fichiers caches
- while(j < nbFile && !hiddenFile && *contentsDir[j]->d_name == '.') j++;
- //Affiche les fichiers
- while(j < nbFile){
- path[strlen(path)-1] != '/' ? sprintf(completePath, "%s/%s", path, contentsDir[j]->d_name) : sprintf(completePath, "%s%s", path, contentsDir[j]->d_name);
- if(stat(completePath, &info) == ERR){
- addperror("Erreur stat");
- return -1;
- }
- info.st_mode & S_IRUSR ? (permission[1] = 'r') : (permission[1] = '-');
- info.st_mode & S_IWUSR ? (permission[2] = 'w') : (permission[2] = '-');
- info.st_mode & S_IXUSR ? (permission[3] = 'x') : (permission[3] = '-');
- info.st_mode & S_IRGRP ? (permission[4] = 'r') : (permission[4] = '-');
- info.st_mode & S_IWGRP ? (permission[5] = 'w') : (permission[5] = '-');
- info.st_mode & S_IXGRP ? (permission[6] = 'x') : (permission[6] = '-');
- info.st_mode & S_IROTH ? (permission[7] = 'r') : (permission[7] = '-');
- info.st_mode & S_IWOTH ? (permission[8] = 'w') : (permission[8] = '-');
- info.st_mode & S_IXOTH ? (permission[9] = 'x') : (permission[9] = '-');
- printf("%s %ld %s\n", permission, info.st_size, contentsDir[j]->d_name);
- j++;
- }
- printf("\n");
- return 1;
- }
- void ls(int argc, char* argv[]){
- struct dirent** contentsDir;
- struct stat info;
- int i = 1, displayed = 0, nbFile, opt;
- boolean hiddenFile = false;
- boolean checksubDir = false;
- //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)){
- if((nbFile = scandir(argv[i], &contentsDir, 0, alphasort)) == ERR){
- addperror("Erreur scandir()");
- return;
- }
- printf("%s : \n\n", argv[i]);
- displayed = printDir(contentsDir, argv[i], 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;
- }
- displayed = printDir(contentsDir, ".", nbFile, hiddenFile);
- }
- printf("%d %d\n", hiddenFile, checksubDir);
- return;
- }
- int main(int argc, char* argv[]){
- ls(argc, argv);
- printf(RESET);
- }
|