summaryrefslogtreecommitdiffstats
path: root/util/topusr.c
blob: cbd5098ff6548f846992adf47db8a401368723ce (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* $Id$ */
/* 使用者 上站記錄/文章篇數 排行榜 */
#define _UTIL_C_
#include "bbs.h"

#define REAL_INFO
struct manrec
{
    char userid[IDLEN + 1];
    char username[23];
    int values[3];
};
typedef struct manrec manrec;
struct manrec *allman[3];

userec_t aman;
manrec theman;
int num;
FILE *fp;

#define TYPE_POST       0
#define TYPE_LOGIN      1
#define TYPE_MONEY      2


void
 top(type)
{
    static char *str_type[3] =
    {"發表次數", "進站次數", " 大富翁 "};
    int i, j, rows = (num + 1) / 2;
    char buf1[80], buf2[80];

    if (type != 2)
    fprintf(fp, "\n\n");

    fprintf(fp, "\
  ╭─────╮           [%dm    %8.8s排行榜                   ╭─────╮\n\
  名次─代號───暱稱──────數目──名次─代號───暱稱──────數目\
", type + 44, str_type[type]);
    for (i = 0; i < rows; i++)
    {
        char ch=' ';
        int value;

        if(allman[type][i].values[type] > 1000000000)
        { value=allman[type][i].values[type]/1000000; ch='M';}
        else if(allman[type][i].values[type] > 1000000)
        { value=allman[type][i].values[type]/1000; ch='K';}
        else {value=allman[type][i].values[type]; ch=' ';}
    sprintf(buf1, "[%2d] %-11.11s%-16.16s%5d%c",
        i + 1, allman[type][i].userid, allman[type][i].username,
            value, ch);
    j = i + rows;
        if(allman[type][j].values[type] > 1000000000)
        { value=allman[type][j].values[type]/1000000; ch='M';}
        else if(allman[type][j].values[type] > 1000000)
        { value=allman[type][j].values[type]/1000; ch='K';}
        else {value=allman[type][j].values[type]; ch=' ';}

    sprintf(buf2, "[%2d] %-11.11s%-16.16s%4d%c",
        j + 1, allman[type][j].userid, allman[type][j].username,
        value, ch);
    if (i < 3)
        fprintf(fp, "\n [1;%dm%-40s%s", 31 + i, buf1, buf2);
    else
        fprintf(fp, "\n %-40s%s", buf1, buf2);
    }
}


#ifdef  HAVE_TIN
int
 post_in_tin(char *name)
{
    char buf[256];
    FILE *fh;
    int counter = 0;

    sprintf(buf, "%s/home/%c/%s/.tin/posted", home_path, name[0], name);
    fh = fopen(buf, "r");
    if (fh == NULL)
    return 0;
    else
    {
    while (fgets(buf, 255, fh) != NULL)
        counter++;
    fclose(fh);
    return counter;
    }
}
#endif              /* HAVE_TIN */

int
bad_user_id(const char *userid)
{
    register char ch;
    if (strlen(userid) < 2)
    return 1;
    if (not_alpha(*userid))
    return 1;
    while((ch = *(++userid)))
    {
    if (not_alnum(ch))
        return 1;
    }
    return 0;
}

int main(int argc, char **argv)
{
    int i, j;

    if (argc < 3)
    {
    printf("Usage: %s <num_top> <out-file>\n", argv[0]);
    exit(1);
    }

    num = atoi(argv[1]);
    if (num == 0)
    num = 30;

    attach_SHM();
    if(passwd_init())
    {
    printf("Sorry, the data is not ready.\n");
    exit(0);
    }
    for(i=0; i<3; i++)
     {
       allman[i]=malloc(sizeof(manrec) * num);
       memset(allman[i],0,sizeof(manrec) * num);    
     }
    for(j = 1; j <= MAX_USERS; j++) {
    passwd_query(j, &aman);
        aman.userid[IDLEN]=0;
        aman.username[22]=0;
    if((aman.userlevel & PERM_NOTOP) || !aman.userid[0] || 
       bad_user_id(aman.userid) || 
       strchr(aman.userid, '.'))
    {
        continue;
    }
    else {
        strcpy(theman.userid, aman.userid);
        strcpy(theman.username, aman.username);
        theman.values[TYPE_LOGIN] = aman.numlogins;
            theman.values[TYPE_POST] =  aman.numposts;
            theman.values[TYPE_MONEY] = aman.money;
            for(i=0; i<3; i++)
         {
            int k,l;
                for(k=num-1; k>=0 && allman[i][k].values[i]<theman.values[i];
             k--);
            k++;
            if(k<num)
                    {
                  for(l=num-1; l>k; l--)
                  memcpy(&allman[i][l], &allman[i][l-1], 
                      sizeof(manrec));
                      memcpy(&allman[i][k], &theman, sizeof(manrec));   
                } 
         }
    }
    }
    

    if ((fp = fopen(argv[2], "w")) == NULL)
    {
    printf("cann't open topusr\n");
    return 0;
    }

    top(TYPE_MONEY);
    top(TYPE_POST);
    top(TYPE_LOGIN);

    fclose(fp);
    return 0;
}