/***********************************************************************/ /* */ /* Diald accounting (make sure that looging is enabled in diald.conf) */ /* */ /* by Boris Lutz (lutz@.n-r-g.com) 26.01.1999 */ /* */ /***********************************************************************/ /* This version does calculate the connection costs according to swiss */ /* phone prices. If you want to use it in another country plese contact */ /* me. I'd like to add support for diffrent countries with a startup */ /* otion soon. */ #include #include #include int atoi_len(char *str, int len); int top_int(int a, int b); int main() { int i = 0; long Onlinetime = 0, BytesReceiv = 0, BytesTrans = 0, UpTimes = 0, time_tmp, connect_time, charge = 0; char s_buffer[20][100], Log_Date[64], first_run = 1; FILE *Logfile; Logfile=fopen("/var/log/diald.log","r"); if (Logfile == (FILE *) NULL) { perror("can not open log file\n"); exit(-37); } while(!feof(Logfile)) { fscanf(Logfile,"%s",s_buffer[++i % 20]); if(strncmp(s_buffer[i % 20], "duration", 8) == 0) { fscanf(Logfile,"%s",s_buffer[++i % 20]); if(first_run == 1) { strcpy(Log_Date, s_buffer[(i - 8) % 20]); strcat(Log_Date, " "); strcat(Log_Date, s_buffer[(i - 7) % 20]); first_run = 0; } printf("\nDate: %s %s\n", s_buffer[(i - 9) % 20], s_buffer[(i - 6) % 20]); printf("Seconds online: %s\n",s_buffer[i % 20]); time_tmp = atoi(s_buffer[i % 20]); connect_time = atoi_len(s_buffer[(i - 6) % 20], 2); /* the connection costs are calculated here */ if(connect_time >= 22 || connect_time <= 6) { charge += top_int(time_tmp, 360); } else if(strncmp(s_buffer[(i - 9) % 20], "Sat", 3) == 0 || strncmp(s_buffer[(i - 9) % 20], "Sun", 3) == 0) { charge += top_int(time_tmp, 180); } else if(connect_time >= 17 || connect_time <= 8){ charge += top_int(time_tmp, 180); } else { charge += top_int(time_tmp, 90); } UpTimes ++; Onlinetime += time_tmp; } if(strncmp(s_buffer[i % 20], "transmitted", 11) == 0) { fscanf(Logfile,"%s",s_buffer[++i % 20]); printf("Bytes transmitted: %s\n",s_buffer[i % 20]); BytesTrans += atoi(s_buffer[i % 20]); } if(strncmp(s_buffer[i % 20], "received", 8) == 0) { fscanf(Logfile,"%s",s_buffer[++i % 20]); printf("Bytes received: %s\n",s_buffer[i % 20]); BytesReceiv += atoi(s_buffer[i % 20]); } } fclose(Logfile); printf("\n\nLog starting at: %s\n", Log_Date); printf("-----------------------------\n"); printf("Up:\t\t %6ld times\n", UpTimes); printf("Time online:\t %3ld:", Onlinetime / 60L); printf("%ld", (Onlinetime % 60) / 10); printf("%ld MM:SS\n", (Onlinetime % 60) % 10); printf("Charge:\t\t %3ld.%ld0 sFr\n", charge / 10, charge %10); printf("Received:\t %6ld kB\n", BytesReceiv / 1024L); printf("Transmitted:\t %6ld kB\n", BytesTrans / 1024L); return 0; } int atoi_len(char *str, int len) { int i, mult, retr = 0; for(mult=1,i=len-1;i>=0;i--,mult*=10) retr += (str[i] - 0x30) * mult; return retr; } int top_int(int a, int b) { return ((a % b == 0) ? (a / b) : (a / b + 1) ); }