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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
| #include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <regex.h>
void Usage(char const *executable);
void adresse_variable(void const *ptr);
void *fn_calloc (size_t const taille1, size_t const taille2);
char *fn_realloc(char *ptr,size_t const taille);
char **fn_realloc2(char **ptr,size_t const taille);
void Verif_Chemin(char *chemin);
void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL);
char **Lire_Repertoire (char const *chemin);
char **Lire_Fichier(char const *URL);
void Creer_Launcher_XML(char **contenu, char const *fichier_dest);
char *Creer_Nom_Fichier(char const *nom_fichier_desktop);
char *Expression_Reguliere(char const *motif, char const *chaine);
char *Expression_Reguliere_Fichier(FILE *file, char const *motif);
int main(int argc, char *argv[])
{
char *chemin = NULL;
char **tableau_de_fichier = NULL;
char *URL = NULL;
short i = 0;
char **resultat = NULL;
char *nom_fichier = NULL;
if ((argc == 1) || (argc > 2))
{
Usage(argv[0]);
}
else
{
chemin = fn_calloc((strlen(argv[1]) + 1),sizeof(char));
strcpy(chemin,argv[1]);
Verif_Chemin(chemin);
tableau_de_fichier = Lire_Repertoire(chemin);
for (i = 0 ;tableau_de_fichier[i] != NULL ; i++)
{
printf("\n--------------------------------------------------------------------------------------------------------\n");
printf("Fichier n°: %d\n\n",i);
printf("Soumission du fichier:\t%s\n\n", tableau_de_fichier[i]);
URL = Concatenation_URL(chemin,tableau_de_fichier[i],URL);
printf("Soumission de l\'URL:\t%s\n\n", URL);
resultat = Lire_Fichier(URL);
nom_fichier = Creer_Nom_Fichier(tableau_de_fichier[i]);
printf("nom du fichier: %s\n",nom_fichier);
if (nom_fichier != '\0')
{
Creer_Launcher_XML(resultat,nom_fichier);
}
else
{
fprintf(stderr,"La fonction Creer_Nom_Fichier à rencontrer probablement un fichier non gérer: %s",tableau_de_fichier[i]);
}
free(URL);
}
free(chemin);
free(tableau_de_fichier);
}
exit(EXIT_SUCCESS);
}
void Usage(char const *executable)
{
printf("Usage: %s /Path/to/desktop/directory\n", executable);
exit(EXIT_FAILURE);
}
void adresse_variable(void const *ptr)
{
printf("L'adresse de la variable est : %p\n", ptr);
}
void *fn_calloc (size_t const nb_element, size_t const taille)
{
void *ptr = calloc(nb_element, taille);
if (ptr == NULL)
{
fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);
exit(EXIT_FAILURE);
}
return ptr;
}
char *fn_realloc(char *ptr,size_t const taille)
{
char *tmp1 = NULL;
tmp1 = realloc(ptr, (taille + 1) * sizeof(char));
if (tmp1 == NULL)
{
fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__); /** realloc a échoué : on sort de la boucle */
exit(EXIT_FAILURE);
}
else
{
ptr = tmp1;
}
return ptr;
}
char **fn_realloc2(char **ptr,size_t const taille)
{
char **tmp1 = NULL;
tmp1 = realloc(ptr, (taille + 1) * sizeof(char *));
if (tmp1 == NULL)
{
fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__); /** realloc a échoué : on sort de la boucle */
exit(EXIT_FAILURE);
}
else
{
ptr = tmp1;
}
return ptr;
}
void Verif_Chemin(char *chemin)
{
char *tmp = NULL;
size_t Longueur_Chemin = strlen(chemin);
if (strcmp(chemin + Longueur_Chemin-1,"/") != 0)
{
tmp = chemin; /** Allocation temporaire du contenu du tableau */
chemin = fn_realloc(chemin, (strlen("/")+ Longueur_Chemin+1)); /** Agrandissement du tableau */
strcat(chemin,"/"); /** Concaténation de 2 chaines */
}
}
void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL)
{
size_t taille = 0;
taille = strlen(chemin) + strlen(nom_fichier) + 1;
URL = fn_calloc(taille, sizeof(char));
printf("Fonction Concatenation_URL:\t%zu\t%zu\t%zu\nLe chemin:\t%s\tNom du fichier: %s\n",strlen(chemin),strlen(nom_fichier),taille,chemin,nom_fichier);
strcpy(URL,chemin);
strcat(URL,nom_fichier);
printf("URL:\t\t%s\n\n",URL);
return URL;
}
char **Lire_Repertoire (char const *chemin)
{
char **tableau_de_fichier = NULL ;
DIR * rep = opendir(chemin); /** Ouverture du répertoire passé en argument */
if (rep != NULL) /** Vérifie l'ouverture du répertoire */
{
struct dirent * file;
size_t i = 0 ;
tableau_de_fichier = fn_calloc(1,sizeof(char *)); /** Pour marquer la fin de la liste */
tableau_de_fichier[0] = NULL ; /** <-----marque la fin de la liste */
printf("Liste des fichiers .destop:\n");
while ((file = readdir(rep)) != NULL)
{
char * nom_fichier = file->d_name;
if ( strstr(nom_fichier , ".desktop" )!= NULL ) /** Le fichier porte l'extension desktop */
{
printf("\t%zu: %s\n",i, nom_fichier);
tableau_de_fichier[i] = fn_calloc(strlen(nom_fichier) + 1, sizeof(char) ); /** remplace le NULL */
strcpy(tableau_de_fichier[i],nom_fichier);
i++;
tableau_de_fichier = fn_realloc2(tableau_de_fichier,(i+1) * sizeof(char *)); /** Pour marquer la fin de la liste */
tableau_de_fichier[i] = NULL; /** <-----marque la fin de la liste */
}
}
closedir(rep); /** Fermeture du répertoire passez en argument */
}
else
{
perror("le chemin :spécifié est introuvable !\n");
exit(EXIT_FAILURE);
}
return tableau_de_fichier;
}
char **Lire_Fichier(char const *URL)
{
char **motif = NULL;
short i =0;
FILE * file = NULL;
char **resultat = NULL;
file = fopen(URL,"r");
/** Ouverture du fichier .desktop en lecture */
if (file != NULL)
{
printf("\nLe fichier %s est ouvert\n",URL);
motif = (char **)calloc(4,sizeof(char*));
motif[0] = "(Exec=.*[^\n])";
motif[1] = "(StartupNotify=[.*[^\n])";
motif[2] = "(Icon=.*[^\n])";
motif[3] = "(Comment=.*[^\n])";
resultat = (char **)calloc(4,sizeof(char*));
for(i = 0; i < 4; i++)
{
resultat[i] = Expression_Reguliere_Fichier(file, motif[i]);
}
fclose(file);
}
else
{
fprintf(stderr,"Impossible d'ouvrir le fichier :%s \n",URL); /** On affiche un message d'erreur */
exit(EXIT_FAILURE);
}
return resultat;
}
void Creer_Launcher_XML(char **contenu, char const *fichier_dest)
{
short i = 0;
FILE* file = NULL;
file = fopen(fichier_dest, "w+"); /** Ouverture du fichier xml en écriture (vidé de son contenu) */
printf("%s\n",fichier_dest);
if (file != NULL)
{
fprintf(file,"[Entry 0]\n");
for(i = 0; i < 4; i++)
{
if (contenu[i] != NULL)
{
fprintf(file,"%s\n",contenu[i]);
}
}
fclose(file);
}
else
{
perror("Impossible d'ouvrir le fichier!\n"); /** On affiche un message d'erreur */
exit(EXIT_FAILURE);
}
}
char *Creer_Nom_Fichier(char const *nom_fichier_desktop)
{
char *nom_fichier = NULL;
char *motif = NULL;
char *sous_chaine = NULL;
size_t taille_chaine = 0;
motif = "(.*[^desktop]|$)";
sous_chaine = Expression_Reguliere(motif, nom_fichier_desktop);
if (sous_chaine != '\0')
{
taille_chaine = strlen(".config/xfce4/panel/launcher-") + strlen(sous_chaine) + strlen("rc") + 1;
nom_fichier = fn_calloc(taille_chaine , sizeof(char));
strcpy(nom_fichier , ".config/xfce4/panel/launcher-");
strcat(nom_fichier , sous_chaine);
strcat(nom_fichier , "rc");
return nom_fichier;
}
else
{
return '\0';
}
}
char *Expression_Reguliere(char const *motif, char const *chaine)
{
int err = 0;
int match = 0;
char *text = NULL;
size_t taille;
regex_t m;
size_t nmatch = 0;
regmatch_t *pmatch = NULL;
char *site = NULL;
err = regcomp(&m,motif,REG_EXTENDED);
if (err == 0)
{
nmatch = m.re_nsub;
pmatch = fn_calloc (nmatch,sizeof (*pmatch));
match = regexec(&m,chaine, nmatch, pmatch, 0);
if(match == 0)
{
int start = pmatch[0].rm_so;
int end = pmatch[0].rm_eo;
size_t size = end - start;
site = fn_calloc ((size + 1) , sizeof (*site));
strncpy (site, &chaine[start], size);
site[size] = '\0';
printf ("\x1b" "[0;31;47m\n" ); /** rouge sur fond blanc */
fprintf(stdout,"La chaine: \'%s\' a été trouvé.\n",site);
printf ("\x1b" "[0;30;47m\n" ); /** noir sur fond blanc */
}
else if (match == REG_NOMATCH)
{
/*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
}
else
{
taille = regerror (err, &m, NULL, 0);
text = malloc (sizeof (*text) * taille);
if (text)
{
regerror (err, &m, text, taille);
fprintf (stderr, "%s\n", text);
free (text);
}
else
{
fprintf (stderr, "Memoire insuffisante\n");
exit (EXIT_FAILURE);
}
}
regfree(&m);
}
if (site)
{
return site;
}
else
{
return '\0';
}
}
char *Expression_Reguliere_Fichier(FILE *file, char const *motif)
{
short num_ligne = 0;
int err = 0;
int match = 0;
char *text = NULL;
size_t taille;
char ligne[1024];
regex_t m;
size_t nmatch = 0;
regmatch_t *pmatch = NULL;
char *site = NULL;
err = regcomp(&m,motif,REG_EXTENDED);
if (err == 0)
{
nmatch = m.re_nsub;
pmatch = fn_calloc (nmatch,sizeof (*pmatch));
while((fgets(ligne,1024 ,file)) != NULL)
{
match = regexec(&m,ligne, nmatch, pmatch, 0);
if(match == 0)
{
int start = pmatch[0].rm_so;
int end = pmatch[0].rm_eo;
size_t size = end - start;
site = fn_calloc ((size + 1) , sizeof (*site));
strncpy (site, &ligne[start], size);
site[size] = '\0';
printf ("\x1b" "[0;31;47m\n" ); /** rouge sur fond blanc */
fprintf(stdout,"La chaine: \'%s\' a été trouvé à la ligne : %d.\n",site,num_ligne);
printf ("\x1b" "[0;30;47m\n" ); /** noir sur fond blanc */
}
else if (match == REG_NOMATCH)
{
/*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
}
else
{
taille = regerror (err, &m, NULL, 0);
text = malloc (sizeof (*text) * taille);
if (text)
{
regerror (err, &m, text, taille);
fprintf (stderr, "%s\n", text);
free (text);
}
else
{
fprintf (stderr, "Memoire insuffisante\n");
exit (EXIT_FAILURE);
}
}
num_ligne++;
}
regfree(&m);
}
rewind(file);
if (site)
{
return site;
}
else
{
return '\0';
}
} |
Partager