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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
char *allocate_1D_char( long n )
{
char *ptr = NULL;
if ((ptr = (char*)malloc(n*sizeof(char))) == NULL )
{
printf("\nallocate_1D_char_ptr ; %s", strerror(errno));
free(ptr);
ptr=NULL;
}
ptr[0]='\0';
return ptr;
}
void deallocate_1D_char( char *p )
{
if ( !p )
printf("deallocate.h::deallocate_1D_char -> %s",strerror(errno));
free(p);
p=NULL;
}
unsigned long long int str_lengh(const char *S)
{
unsigned long long int c = 0;
while( *S != '\0')
{
c++;
S++;
}
return c;
}
char *str_concat(char *s1, char *s2)
{
unsigned long long int i;
unsigned long long int s1_lengh = str_lengh(s1);
unsigned long long int s2_lengh = str_lengh(s2);
unsigned long long int s0_lengh = s1_lengh + s2_lengh;
char *s0 = allocate_1D_char( s0_lengh + 1 );
s0[s0_lengh]='\0';
for ( i=0 ; i<s1_lengh ; i++ )
s0[i]=s1[i];
for ( i=s1_lengh ; i<s1_lengh+s2_lengh ; i++ )
s0[i]=s2[i-s1_lengh];
return s0;
}
DIR *directory_open(char * directory_path)
{
DIR *directory = NULL;
if ( (directory=opendir(directory_path)) == NULL )
//printf("\nopen_directory ; %s -> %s ", strerror(errno), directory_path);
//printf("\ndirectory@%p opened ; %s", directory, directory_path);
return directory;
}
DIR *is_directory(char * directory_path)
{
return opendir(directory_path);
}
void directory_close(DIR *directory)
{
/*if ( */closedir(directory);/*==-1 )*/
//printf("\nclose_directory ; %s", strerror(errno));
//printf("\ndirectory@%p closed", directory);
}
void directory_read(DIR *directory, char *directory_path)
{
directory = directory_open(directory_path);
struct dirent *entity = NULL;
while( (entity = readdir(directory)) != NULL )
printf("\n%s", entity->d_name);
directory_close(directory);
}
char *get_entity_path( char *directory_path, char *entity_name)
{
return str_concat(str_concat(directory_path, "\\"), entity_name);
}
void directory_browse(DIR *directory, char *directory_path)
{
directory = directory_open(directory_path);
struct dirent *entity=NULL;
seekdir(directory,2);
while( (entity=readdir(directory)) != NULL )
{
char *entity_path = get_entity_path(directory_path, entity->d_name);
if ( is_directory(entity_path) != NULL )
{
printf("\nDIR@%s",entity_path);
directory_browse(directory, entity_path);
}
else printf("\nFILE@%s",entity_path);
deallocate_1D_char(entity_path);
}
directory_close(directory);
}
char *directory_search_entity(DIR *directory, char *directory_path, char *entity_name)
{
directory = directory_open(directory_path);
struct dirent *entity=NULL;
seekdir(directory,2);
while( (entity=readdir(directory)) != NULL )
{
char *entity_path = get_entity_path(directory_path, entity->d_name);
if ( is_directory(entity_path) != NULL )
directory_search_entity(directory, entity_path, entity_name);
else
{
if ( strcmp( entity->d_name, entity_name ) == 0 )
{
deallocate_1D_char(entity_name);
directory_close(directory);
return entity_path;
}
}
deallocate_1D_char(entity_path);
}
directory_close(directory);
return NULL;
}
void directory_count_entity(DIR *directory, char *directory_path, unsigned int *f_counter, unsigned int *d_counter)
{
directory = directory_open(directory_path);
struct dirent *entity=NULL;
seekdir(directory,2);
while( (entity=readdir(directory)) != NULL )
{
char *entity_path = get_entity_path(directory_path, entity->d_name);
if ( is_directory(entity_path) != NULL )
{
(*d_counter)++;
directory_count_entity(directory, entity_path, f_counter, d_counter);
}
else (*f_counter)++;
free(entity_path);
}
directory_close(directory);
}
int main()
{
DIR *directory=NULL;
//Parcours récursir du dossier c:\fraps
directory_browse(directory, "c:\\fraps" );
//Compter le nombre de fichiers et de dossiers dans le dossier c:\fraps
unsigned int f=0;
unsigned int d=0;
directory_count_entity(directory, "c:\\fraps", &f, &d);
printf("\n\nfile_count = %d \ndir_count = %d", f, d);
//Chercher le chemin du fichier uninstall.exe dans le dossier c:\fraps
char *r = directory_search_entity(directory, "c:\\fraps", "uninstall.exe");
printf("\n\nChemin du fichier ca trouver : %s", r);
return 0;
} |
Partager