/*----------------------------------------------------------------------------*/ /* Web2cash.c V 1.0 */ /* */ /* Petit utilitaire pour calculer les factures de téléphone quand on surfe */ /* sur le web via une connexion PPP a partir de /var/log/messages */ /* sous Linux */ /* grep 'Connect:' /var/log/messages > connect.dat */ /* grep 'Connection terminated' /var/log/messages > fin.dat */ /* mv connect.dat ../C/filing/ */ /* mv fin.dat ../C/filing/ */ /* ./web2cash > output */ /* cat output */ /* */ /* Auteur : Pascal Terracol (c) AssetstomediA 10/98 */ /* Email : pascal.terracol@paris-lavillette.archi.fr */ /* */ /*----------------------------------------------------------------------------*/ #include #include #include #define DEBUG 0 #define SEUIL_TEMPS 180 #define CREDIT_BASE 0.74 #define COUT_MINUTE_NORMAL 0.28 #define COUT_MINUTE_REDUIT 0.14 #define DATE_PRIMALISTE 22 typedef struct { char mois[3] ; int jours ; int heure ; int minute ; int seconde ; } DATE ; /*-- Date ------------------------*/ /*----------------------------------------------------------------------------*/ /* envir_init ( fdebut, fin ) */ /*----------------------------------------------------------------------------*/ void envir_init ( fdebut, fin ) char *fdebut, *fin ; { FILE *fpd, *fpf, *fopen () ; char buf[80] , buf2[80], buf3[80], buf4[80], buf5[80], buf6[80]; int param_get() ; DATE date_debut ; DATE date_fin ; DATE *long_to_date( ) ; DATE *date_delta ; DATE *date_TOTAL ; long date_to_long( ) ; long ldate_debut ; long ldate_fin ; long ldate_delta ; long ldate_TOTAL = 0 ; /* le total */ float prix = 0.0 ; float PRIXTOTAL = 0.0 ; float cout() ; /* on ouvre le fichier connect.dat */ if ( ( fpd = fopen ( fdebut, "rt" )) == NULL ) { printf("Erreur ouverture du fichier %s !\n",fdebut); exit(0); } /* on ouvre le fichier fin.dat */ if ( ( fpf = fopen ( fin, "rt" )) == NULL ) { printf("Erreur ouverture du fichier %s !\n",fin); exit(0); } while(!feof(fpd)) { /* lecture de connect.dat */ if(!param_get(fpd, buf, buf2, buf3, buf4, buf5, buf6 ) ) { printf("fin de fichier %s \n", fdebut ); /* exit(0); on sort plus tard !!!!!!*/ } sprintf(date_debut.mois, "%s", buf) ; /* le mois */ date_debut.jours = atoi(buf2) ; /* le jour */ date_debut.heure = atoi(buf3) ; /* l'heure */ date_debut.minute = atoi(buf4) ; /* la minute */ date_debut.seconde= atoi(buf5) ; /* la seconde */ /* lecture de fin.dat */ if(!param_get(fpf, buf, buf2, buf3, buf4, buf5, buf6 ) ) { printf("fin de fichier %s \n", fin ); exit(0); } sprintf(date_fin.mois, "%s", buf) ; /* le mois */ date_fin.jours = atoi(buf2) ; /* le jour */ date_fin.heure = atoi(buf3) ; /* l'heure */ date_fin.minute = atoi(buf4) ; /* la minute */ date_fin.seconde= atoi(buf5) ; /* la seconde */ printf("%3s %2d %2d %2d %2d \n", date_debut.mois, date_debut.jours, date_debut.heure, date_debut.minute, date_debut.seconde ) ; printf("%3s %2d %2d %2d %2d ", date_fin.mois, date_fin.jours, date_fin.heure, date_fin.minute, date_fin.seconde ) ; ldate_debut = date_to_long( date_debut ) ; /* conversion date -> long debut */ ldate_fin = date_to_long( date_fin ) ; /* "" "" fin */ ldate_delta = ldate_fin - ldate_debut ; /* calcul delta en long */ date_delta = long_to_date(ldate_delta) ; /* conversion delta en date */ printf("duree %2d %2d %2d %2d : ", date_delta->jours, date_delta->heure, date_delta->minute, date_delta->seconde ) ; prix = cout( date_debut, ldate_delta ) ; PRIXTOTAL += prix ; printf("%7.2f FF TTC %9.2f FF TTC", prix, PRIXTOTAL ) ; ldate_TOTAL += ldate_delta ; date_TOTAL = long_to_date(ldate_TOTAL) ; printf(": Temps cumulÈ %2d %2d %2d %2d \n", date_TOTAL->jours, date_TOTAL->heure, date_TOTAL->minute, date_TOTAL->seconde ) ; } fclose( fpd ) ; fclose( fpf ) ; } /*----------------------------------------------------------------------------*/ /* param_get(fp,ident,buffer) */ /* lit dans fp sur l'ident et retourne buffer */ /*----------------------------------------------------------------------------*/ int param_get(fp,buffer, b2, b3, b4, b5, b6) FILE *fp ; char *buffer, *b2, *b3 , *b4, *b5, *b6 ; { char ligne[128],format[128]; int stat ; strcat(format,"%s%s%[^:]%*[:]%[^:]%*[:]%s%[^\n\r]"); fgets(ligne,128,fp); if(!feof(fp)) { if( DEBUG )printf("sscanf %s", ligne); if( (stat = sscanf(ligne, format, buffer, b2, b3, b4, b5, b6) ) == 0 ) { printf("Pb conversion sscanf \n") ; return(0) ; } return(1) ; } else return(0); } /*----------------------------------------------------------------------------*/ /* calcul du cout */ /*----------------------------------------------------------------------------*/ float cout( date_appel, duree) DATE date_appel ; long duree ; { float prix = 0.0 ; float cout_minute ; long minutes ; long secondes ; if( duree < SEUIL_TEMPS ) /* on est en dessous des 3 minutes */ { prix = CREDIT_BASE ; } else /* on est en dessus des 3 minutes */ { duree -= SEUIL_TEMPS ; minutes = duree / 60 ; secondes = duree % 60 ; if(date_appel.heure >= 19 || date_appel.heure < 8 ) cout_minute = COUT_MINUTE_REDUIT ; else cout_minute = COUT_MINUTE_NORMAL ; prix = CREDIT_BASE + ( cout_minute * minutes ) + ( cout_minute * ( float ) secondes / 60.0 ) ; } return( prix ) ; } /*----------------------------------------------------------------------------*/ /* conversion de long -> DATE */ /*----------------------------------------------------------------------------*/ DATE *long_to_date( ldate ) long ldate ; { static DATE date ; date.jours = ldate / 86400 ; ldate = ldate - ( long ) date.jours * 86400 ; date.heure = ldate / 3600 ; ldate = ldate - (long ) date.heure * 3600 ; date.minute = ldate / 60 ; date.seconde = ldate % 60 ; return( &date ) ; } /*----------------------------------------------------------------------------*/ /* conversion de DATE -> long */ /*----------------------------------------------------------------------------*/ long date_to_long( date ) DATE date ; { long ldate ; ldate = ( long ) date.jours * 86400 + ( long ) date.heure * 3600 + ( long ) date.minute * 60 + ( long ) date.seconde ; return ( long ) ldate ; } int main( argc, argv ) int argc; char *argv[] ; { char filedebut[80]; char filefin[80]; if (argc > 2) /* interprétation des arguments */ { sprintf(filedebut, "%s", argv[1]) ; sprintf(filefin, "%s", argv[2]) ; envir_init( filedebut, filefin ) ; } else { printf("Usage : openfile connect.dat fin.dat \n"); exit(4); } return(0); }