summaryrefslogtreecommitdiffstats
path: root/util/yearsold.c
blob: 3229b709e1a4884ddd7161a0605a255b66a47e2f (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* $Id: yearsold.c,v 1.5 2003/07/20 00:55:34 in2 Exp $ */
/* 站上年齡統計 */
#define _UTIL_C_
#include "bbs.h"

#define MAX_LINE        16

struct userec_t cuser;

void
fouts(fp, buf, mode)
FILE *fp;
char buf[], mode;
{
    static char state = '0';

    if (state != mode)
    fprintf(fp, "[3%cm", state = mode);
    if (buf[0])
    {
    fprintf(fp, buf);
    buf[0] = 0;
    }
}

int main(int argc, char **argv)
{
    int i, j, k;
    char buf[256];
    FILE *fp;
    int year, max, item, maxyear;
    long totalyear;
    int act[25];
    time_t now;
    struct tm *ptime;

    now = time(NULL);
    ptime = localtime(&now);

    attach_SHM();
    if(passwd_mmap())
    exit(1);
    
    memset(act, 0, sizeof(act));
    for(k = 1; k <= MAX_USERS; k++) {
    passwd_query(k, &cuser);
    if (((ptime->tm_year - cuser.year) < 10) || ((ptime->tm_year - cuser.year) >
                             33))
        continue;

    act[ptime->tm_year - cuser.year - 10]++;
    act[24]++;
    }
    
    for (i = max = totalyear = maxyear = 0; i < 24; i++)
    {
    totalyear += act[i] * (i + 10);
    if (act[i] > max)
    {
        max = act[i];
        maxyear = i;
    }
    }

    item = max / MAX_LINE + 1;

    if ((fp = fopen(BBSHOME"/etc/yearsold", "w")) == NULL)
    {
    printf("cann't open etc/yearsold\n");
    return 1;
    }

    fprintf(fp, "\t\t\t  " BBSNAME
        " 年齡統計 [%02d/%02d/%02d] \n\n",
        ptime->tm_year % 100, ptime->tm_mon + 1, ptime->tm_mday);
    for (i = MAX_LINE + 1; i > 0; i--)
    {
    strcpy(buf, "   ");
    for (j = 0; j < 24; j++)
    {
        max = item * i;
        year = act[j];
        if (year && (max > year) && (max - item <= year))
        {
        fouts(fp, buf, '7');
        fprintf(fp, "%-3d", year);
        }
        else if (max <= year)
        {
        fouts(fp, buf, '4');
        fprintf(fp, "█ ");
        }
        else
        strcat(buf, "   ");
    }
    fprintf(fp, "\n");
    }


    fprintf(fp, "   "
        "10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n\n"
        "\t\t      有效統計人次:%-9d平均年齡:%d\n"
        ,act[24], (int)totalyear / act[24]);
    fclose(fp);
    return 0;
}