/* Calculates total time used at ISP */
/* Works with dip-3.3.7o-uri */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
main(int argc, char **argv)
{
int started = 0, total = 0, used;
int hour, min, sec;
char s[1026], s2[30], startdate[20], *p;
char user[16];
char login_s[50], logout_s[50];
FILE *f;
struct passwd *pw;
if (! (f = fopen("/var/adm/messages", "r"))) {
perror("open");
exit(1);
}
if (argc > 1) {
if (argc == 2)
strcpy(startdate, argv[1]);
else
sprintf(startdate, "%s %2s", argv[1], argv[2]);
}
pw = getpwuid(getuid());
memset(user, 0, sizeof(user));
strncpy(user, pw->pw_name, sizeof(user));
sprintf(login_s, "]: %s dial-up", user);
sprintf(logout_s, "]: %s down", user);
while (fgets(s, 1024, f)) {
if ((! started) && (argc > 1)) {
if (argc == 2) {
if (strncmp(s, startdate, 3))
continue;
} else {
if (strncmp(s, startdate, 6))
continue;
}
}
started = 1;
if (strstr(s, login_s)) {
strncpy(s2, s, 15);
s2[15] = 0;
printf("Login: %s\n", s2);
} else if (strstr(s, logout_s)) {
strncpy(s2, s, 15);
s2[15] = 0;
printf("Logout: %s ", s2);
memset(s2, 0, 30);
p = strstr(s, "online") + 7;
strncpy(s2, p, strlen(p) - 2);
used = atoi(s2);
hour = used / 3600;
min = (used / 60) % 60;
sec = used % 60;
printf("Time used: %d hours %d minutes %d seconds\n\n",
hour, min, sec);
total += used;
}
}
fclose(f);
hour = total / 3600;
min = (total / 60) % 60;
sec = total % 60;
printf("\nTotal time used: %d hours %d minutes %d seconds\n\n",
hour, min, sec);
}