home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
-
- #define SYSTEMS 100 /* Max number of systems to track */
- #define SYS_SIZE 11 /* Number of chars in system name (including null) */
- #define BUF_SIZE 80 /* Max chars in line */
-
- FILE *fopen(), *costs_file;
-
- struct costs {
- char system[SYS_SIZE];
- int first;
- int cost;
- int minimum;
- long minutes;
- long tot_cost;
- int used;
- } system_costs[SYSTEMS];
-
- struct costs *c_ptr;
-
- #define max(a,b) ((a)>(b)?(a):(b))
-
- int systems;
-
- int lookfor();
-
- int main() {
- int mins;
- int i;
- char sys[SYS_SIZE];
- long this_cost;
- int sc;
- char buffer[BUF_SIZE];
- long total;
- long tot_mins;
-
- /*
- Read the costs file, one record per system.
- */
- costs_file = fopen("/usr/lib/uucp/uucost/costs","r");
- c_ptr = system_costs;
-
- while (fgets (buffer, (int)sizeof(buffer), costs_file)) {
- if (buffer[0] != '#') { /* ignore comments */
- sc = sscanf(buffer, "%s%d%d%d", c_ptr->system, &c_ptr->first,
- &c_ptr->cost, &c_ptr->minimum);
- if (sc == 4) {
- c_ptr->minutes = 0L;
- c_ptr->tot_cost = 0L;
- c_ptr->used = 0;
- c_ptr++;
- } else {
- (void)fprintf (stderr, "Format error in costs file. ");
- (void)fprintf (stderr, "Input was: %s\n", buffer);
- }
- }
- }
-
- systems = c_ptr - system_costs; /* number of system_costs entries used */
-
- /*
- For each line of standard input:
-
- 1) look up system in systems_costs structure
- 2) add the time to the running count for this system
- 3) figure the cost of the session and add it in
- */
- while (scanf("%s%s%d", sys, buffer, &mins) != EOF) {
- if ((i = lookfor(sys)) == -1) {
- (void) fprintf (stderr,"System %s not found in costs file.\n", sys);
- } else {
- c_ptr = &system_costs[i];
- c_ptr->used = 1;
- c_ptr->minutes += (long)mins;
- this_cost = (long)c_ptr->first;
- this_cost += (long)c_ptr->cost * (long)(mins-1);
- this_cost = max(this_cost, (long)c_ptr->minimum);
- c_ptr->tot_cost += this_cost;
- #ifdef DEBUG
- (void) printf ("%-10s %-30s %3d mins = $%3ld.%.2ld\n",
- sys, buffer, mins, this_cost/100L, this_cost%100L);
- #endif /* DEBUG */
- }
- }
-
- /*
- For each system that had activity:
- 1) print the name and minutes and cost
- 2) keep total of minutes used
- 3) keep total of cents used
- */
- total = 0L;
- tot_mins = 0L;
- (void) printf ("\nSystem Mins Cost\n");
- for (c_ptr = system_costs; c_ptr < &system_costs[systems]; c_ptr++) {
- if (c_ptr->used) {
- (void) printf ("%-10s %5ld $%3ld.%.2ld\n", c_ptr->system,
- c_ptr->minutes, c_ptr->tot_cost/100L, c_ptr->tot_cost%100L);
- total += c_ptr->tot_cost;
- tot_mins += c_ptr->minutes;
- }
- }
- (void) printf ("\nTOTAL %5ld $%3ld.%.2ld\n",
- tot_mins, total/100L, total%100L);
- return (0);
- }
-
-
- int lookfor(sys)
- char *sys;
- {
- int i;
-
- for (i = 0; i < systems; i++) {
- if (!strcmp (sys, system_costs[i].system))
- return(i);
- }
- return (-1); /* Indicate system not found */
- }
-