J'ai crée ce petit programme parce que j'accède souvent à mon fichier host pour le modifier.
Simplement, vous lancez le programme, il détermine le chemin d'accès au fichier host puis vous demande d'entrer la valeur à ajouter à celui ci.
Je partage donc la source à tout hasard pour les personnes intéressée, à titre "pédagogique" et aussi pour recevoir des critiques sur la qualité du code.
Celui-ci a été développé sous code blocks. C'est un projet d'extension .cbp.
Source / Exemple :
main.c
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "file.h"
char *getHostPath();
int getKeyboardInput( char *buffer, int *length );
void menu();
int main (void)
{
menu();
/* On détermine le chemin d'accès au fichier hosts */
char *hostPath = getHostPath();
char *hostBuffer = NULL;
int *hostSize = (int*)malloc(sizeof(int));
/* On mémorise dans une chaine de caractères le fichier host */
memorizeFile( hostPath, &hostBuffer, hostSize );
char addToHostBuffer[127];
int *addToHostSize = (int*)malloc(sizeof(int));
/* On saisie la ligne à ajouter dans le fichier hosts */
getKeyboardInput( addToHostBuffer, addToHostSize );
/* On ajoute la saisie à la ligne suivante du fichier hosts */
addCharInString( &hostBuffer, '\n', strlen(hostBuffer) );
char *newHost;
/* On concatène la saisie au fichier hosts */
concat( &newHost, hostBuffer, addToHostBuffer);
/* On enregistre la saisie dans le fichier host */
saveFile( hostPath, newHost );
/* Désallocation des ressources mémoires */
free(addToHostSize);
free(hostBuffer);
free(hostSize);
free(newHost);
return 0;
}
void menu()
{
system("title EASY HOST");
printf("\n\n +-----------------------------------------------------+");
printf("\n | EASY HOST |");
printf("\n +-----------------------------------------------------+\n\n");
printf(" > Add a new value : example -> 127.0.0.1 facebook.fr\n\n");
printf(" > value : ");
}
char *getHostPath()
{
const char *homeDrive = getenv("HOMEDRIVE");
char *hostPath;
concat(&hostPath, homeDrive, "\\Windows\\System32\\drivers\\etc\\hosts");
return hostPath;
}
int getKeyboardInput( char *buffer, int *length )
{
/* */
if ( buffer != NULL )
{
/* Si la saisie a reussie */
if ( fgets( buffer, *length, stdin ) != NULL )
{
/* On supprime le possible retour chariot */
while ( *buffer != '\0' )
{
if ( *buffer == '\n' )
{
}
buffer++;
(*length)++;
}
/* On retourne 1 : la saisie a correctement été traité */
/* On vide stdin en cas d'overflow du buffer */
fflush(stdin);
return 1;
}
/* Si erreur on retourne 0 */
else {
return 0;
}
}
/* Si erreur, on retourne -1 */
else {
return -1;
}
}
file.h
#ifndef _FILE_H_
#define _FILE_H_
/*
Fonction qui enregistre un fichier dans une chaine de caractère
Entrée : filepath un pointeur sur le chemin du fichier
buffer un double pointeur sur la chaine qui va contenir le fichier
bufferSize un pointeur sur la longueur du fichier
void memorizeFile( char *filePath, char **buffer, int *bufferSize )
{
FILE *fichier=fopen(filePath,"r");
if( fichier != NULL && buffer != NULL && bufferSize != NULL)
{
char c;
- bufferSize = 0;
- buffer = NULL;
char *reallocatedBuffer=NULL;
while( fscanf(fichier,"%c",&c) != EOF )
{
reallocatedBuffer = realloc( reallocatedBuffer, (*bufferSize+1)*sizeof(char));
if ( reallocatedBuffer != NULL )
{
- buffer = reallocatedBuffer;
(*buffer)[*bufferSize] = c;
- bufferSize= *bufferSize+1;
}
else {
- buffer=NULL;
- bufferSize=0;
printf("\n\n memorizeFile : 2 ");
}
}
}
else perror("\n\n memorizeFile 1 : ");
fclose(fichier);
}
/*
Fonction qui enregistre une chaine de caractère dans un fichier
Entrée : filepath un pointeur sur le chemin du fichier
string un pointeur sur la chaine dont le contenu est à enregistrer
void saveFile( char *filePath, char *string )
{
FILE *file=fopen(filePath,"w");
printf("OK");
if( file != NULL && filePath != NULL )
{
int i;
for( i=0 ; i<strLen(string)+1 ; i++ )
{
fprintf( file, "%c", string[i] );
printf( "%c", string[i] );
}
}
else perror("\n\n saveFile : ");
fclose(file);
}
#endif
string.h
#ifndef _STRING_H_
#define _STRING_H_
/*
Fonction qui retourne la longueur d'une chaine
Entrée : s un pointeur sur la chaine
Sortie : la longueur de la chaine
int strLen( char *s )
{
if ( s != NULL )
{
int length=0;
while ( *s != '\0')
{
length++;
s++;
}
return length;
}
else perror("\n strLen ");
}
/*
Fonction qui concatène deux chaines
Entrée : s1 un pointeur sur la première chaine
s2 un pointeur sur la seconde chaine
s un double pointeur sur le resultat de la concaténation
void concat( char **s, char *s1, char *s2 )
{
- s = (char*)malloc( (strLen(s1)+strLen(s2)+1)*sizeof(char) );
if ( s != NULL && s1 != NULL && s2 != NULL )
{
int i;
for ( i=0 ; i<strLen(s1) ; i++ )
{
(*s)[i] = s1[i];
}
for ( i=strLen(s1) ; i<strLen(s1)+strLen(s2)+1 ; i++ )
{
(*s)[i] = s2[i-strLen(s1)];
}
}
else perror("\n concat : \n");
}
/*
Fonction qui rajoute un caractère dans une chaine
Entrée : s un double pointeur sur la chaine
c le caractère à insérer
offset la position du caractère à insérer
void addCharInString( char **s, char c, int offset)
{
if ( s != NULL)
{
char *s1 = (char*)malloc( (strLen(*s)-offset+1)*sizeof(char) );
if ( s1 != NULL )
{
int i;
for ( i=offset ; i<strLen(*s)+1 ; i++ )
{
s1[i-offset]=(*s)[i];
}
char *reallocatedString = (char*)realloc( *s, (strLen(*s)+2)*sizeof(char) );
if ( reallocatedString != NULL )
{
(*s)[offset]=c;
for( i=offset+1 ; i<strLen(*s)+2 ; i++)
{
(*s)[i]=s1[i-offset-1];
}
}
else perror("\n addCharInString 3");
}
else perror("\n addCharInString 2");
}
else perror("\n addCharInString 1");
}
#endif
Conclusion :
Merci de votre participation. Bonne continuation.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.