Fonction controlant l'unicite d'un script
Bonjour ou bonsoir à toutes et à tous.
Travaillant sous Unix en C, j'ai développé une petite fonction pour controler l'unicite d'un script.
Code:
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
| /**************************************************
* Fonction : ControlerUniciteScript
*
* Description :
* Controle l'unicite du traitement lance
*
* Syntaxe :
* void ControlerUniciteScript(char szNomScript[], FILE * pfLog)
*
* Arguments :
* - char szNomScript[] : Nom du traitement
* - FILE * pfLog : handle du fichier de log
*
* Val. Retour :
* -
*
*******************************************************/
void ControlerUniciteScript(char szNomScript[], FILE * pfLog)
{
int i;
int j = 0;
char szBufferCommande[50];
char szBuffer[50];
char szBufferSortie[50];
FILE * pfPop;
sprintf(szBufferCommande, "ps -a | grep %s | wc -l", szNomScript);
pfPop = popen(szBufferCommande, "r");
if (pfPop == (FILE *) NULL)
{
/*Remplir le fichier de log sur ce cas d'erreur */;
}
while(fgets(szBuffer, sizeof(szBuffer), pfPop) != (char *) NULL)
{
szBuffer[strlen(szBuffer)-1] = '\0' ;
trim(szBuffer, szBufferSortie);
if(strcmp(szBufferSortie, "1") != 0)
{
/* remplir le fichier de log sur ce cas d'erreur */
}
}
} |
Pour la fonction trim, cf : Fonction trim
Voila.
Pour le cas où cela pourrait servir à quelqu'un, enfin... Si ce n'est pas trop mal écrit bien sûr :mrgreen:.
Bahan