1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
int main(int argc, char *argv[]){
struct stat info;
struct dirent *entree;
DIR *repertoire;
repertoire = opendir(".");
int optch;
extern int opterr;
char format[] = "ail";
char droits[8][4] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
int droits_prop, droits_grp, droits_autres;
opterr = 1;
while ((optch = getopt(argc, argv, format)) != -1)
switch (optch) {
case 'a':
/* gere ls -a*/
while (entree=readdir(repertoire)) {
printf("%s\n",entree->d_name);
}
/*gere ls -ai*/
while ((optch = getopt(argc, argv, format)) != -1)
switch (optch) {
case 'i':
while (entree=readdir(repertoire)) {
printf("%d : %s\n", entree->d_ino, entree->d_name);
}
break;
}
/* gere ls -i*/
case 'i':
while (entree = readdir(repertoire)) {
if(entree->d_name[0] != '.')
printf("%d : %s\n", entree->d_ino, entree->d_name);
}
break;
/* gere ls -l*/
case 'l':
while (entree=readdir(repertoire)) {
lstat(entree->d_name,&info);
droits_prop = (info.st_mode >> 6) &7;
droits_grp = (info.st_mode >> 3) &7;
droits_autres = info.st_mode &7;
/* affichage droit et non du fichier */
printf("%s%s%s\t %s\n",droits[droits_prop],droits[droits_grp],droits[droits_autres],entree->d_name);
}
break;
/* option non connu */
default:
printf("Saisir une option : -l, -a, -ai, -ls \n");
break;
}
return 0;
} |
Partager