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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
static int opt_follow_links=0;
static int opt_apparent_size =0;
/*valid_name retourne true si repertoire different de . ou ..*/
int valid_name(const char *name)
{
return strcmp(name,".") && strcmp(name,"..");
}
int du_file(const char* name)
{
static int profondeur =0; /*variable static, pour verifier le nombre de lien symbolique traverses*/
struct stat obj;
DIR* dir;
struct dirent *dire;
int status, size, fin;
char *pathname = "";
char linkname[PATH_MAX+1];
if(profondeur > 128) /*Si plus de 128 liens symboliques, quitte. Valeur arbitraire*/
{
errno = ELOOP;
perror("Trop de liens symboliques :");
exit(EXIT_FAILURE);
}
status = lstat(name , &obj); /*Stocke les donnees du fichier pointee dans obj*/
/*Gestion d'erreurs du lstat*/
if(status)
{
perror("Probleme de lstat");
exit(EXIT_FAILURE);
}
/*Cas d'un fichier simple*/
if(S_ISREG(obj.st_mode))
{
return opt_apparent_size ? obj.st_size : obj.st_blocks;
}
/*Cas d'un repertoire*/
if(S_ISDIR(obj.st_mode))
{
size= opt_apparent_size?obj.st_size : obj.st_blocks;
/*Ouverture repertoire + Gestion d'erreur opendir*/
if ((dir = opendir(name)) == NULL) {
perror("Probleme d'opendir");
exit(EXIT_FAILURE);
}
while ((dire = readdir(dir))) {
if (valid_name(dire->d_name)) {
snprintf(pathname, PATH_MAX, "%s/%s", name, dire->d_name);
size += du_file(pathname);
}
}
/* gestion des erreurs */
if (errno != 0) {
perror("Probleme d'appel système");
exit(EXIT_FAILURE);
}
closedir(dir);
return size;
}
/*Cas d'un lien symbolique*/
if(S_ISLNK(obj.st_mode))
{
size= opt_apparent_size?obj.st_size : obj.st_blocks;
if(opt_follow_links)
{
profondeur += 1;
fin = readlink(name, linkname, PATH_MAX);
linkname[fin] = '\0';
/* gestion des erreurs */
if (fin == (-1))
{
perror("Probleme de readlink");
exit(EXIT_FAILURE);
}
size += du_file(linkname);
}
return size;
}
return 0;
}
int main(int argc, char* argv[])
{
int i;
for(i=1; i<(argc-1); i++){
if(!strcmp(argv[i],"-L")){
opt_follow_links=1;
}else if(!strcmp(argv[i],"-b")){
opt_apparent_size=1;
}else{
printf("option incorrecte : %s \n", argv[i]);
exit(EXIT_FAILURE);
}
}
printf("Taille du fichier/dossier : %d \n", du_file(argv[argc-1]));
return 0;
} |
Partager