summaryrefslogtreecommitdiffstats
path: root/util/daymandex.c
blob: d2921d073e7502261a0ec48a96e4bfa4fe9ad751 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* $Id: daymandex.c,v 1.1 2002/03/07 15:13:46 in2 Exp $ */

/*
  target : 精華區索引程式 (man index)
  
  syntax : mandex [board]
  [board] 有值 ==> 只跑該 board
  空的 ==> 所有的 boards 都跑
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
#include "pttstruct.h"
#include "util.h"

#ifndef MAXPATHLEN
#define MAXPATHLEN  1024
#endif

extern int numboards;
extern boardheader_t *bcache;

char color[4][10] =
{"", "", "", ""};
char fn_index[] = ".index";
char fn_new[] = ".index.new";
char index_title[] = "◎ 精華區目錄索引";
FILE *fndx;
int ndir;
int nfile;
int index_pos;
char topdir[128], pgem[512], pndx[512];

int nb = 0;         /* board 數 */

struct boardinfo
{
    char bname[40];
    int ndir;
    int nfile;
    int k;
};
typedef struct boardinfo boardinfo;

boardinfo
board[MAX_BOARD];

int k_cmp(b, a)
    boardinfo *b, *a;
{
    return ((a->k / 100 + a->ndir + a->nfile) - (b->k / 100 + b->ndir + b->nfile));
}

int dashd(fname)
    char *fname;
{
    struct stat st;

    return (stat(fname, &st) == 0 && S_ISDIR(st.st_mode));
}


/* visit the hierarchy recursively */

void
mandex(level, num_header, fpath)
    int level;
    char *fpath, *num_header;
{
    FILE *fgem;
    char *fname, buf[256];
    struct stat st;
    int count;
    fileheader_t fhdr;

    fgem = fopen(fpath, "r+");
    if (fgem == NULL)
    return;

    fname = strrchr(fpath, '.');
    if (!level)
    {

        printf("%s\r\n",fpath);
    strcpy(pgem, fpath);

    strcpy(fname, fn_new);
    fndx = fopen(fpath, "w");
    if (fndx == NULL)
    {
        fclose(fgem);
        return;
    }
    fprintf(fndx, "序號\t\t\t精華區主題\n"
        "──────────────────────────────────────\n");
    strcpy(pndx, fpath);
    ndir = nfile = 0;
    index_pos = -1;
    }

    count = 0;
    while (fread(&fhdr, sizeof(fhdr), 1, fgem) == 1)
    {
    strcpy(fname, fhdr.filename);
        if (!fname[0]) continue;
    if (!level && !strncmp(fhdr.title, index_title, strlen(index_title))
        && index_pos < 0)
    {
        index_pos = count;
        unlink(fpath);
    }
    st.st_size = 0;
    stat(fpath, &st);

    sprintf(buf, "%.*s%s%3d. %s \n",

        11 * level, num_header, color[level % 4], ++count, fhdr.title); /* Ptt */
    fputs(buf, fndx);
    if (dashd(fpath))
    {
        ++ndir;
        if (*fhdr.title != '#' && level < 10)
        {
        strcat(fpath, "/.DIR");
        mandex(level + 1, buf, fpath);
        }
    }
    else
        ++nfile;
    }

    if (!level)
    {
    char lpath[MAXPATHLEN];

    fclose(fndx);
    strcpy(fname, fn_index);
    rename(pndx, fpath);
    strcpy(pndx, fpath);

    sprintf(buf, "%s.new", pgem);
    if (index_pos >= 0 || (fndx = fopen(buf, "w")))
    {
        fname[-1] = 0;
        stamplink(fpath, &fhdr);
        unlink(fpath);
        strcpy(fhdr.owner, "每天自動更新");
        sprintf(lpath, "%s/%s", topdir, pndx);
        st.st_size = 0;
        stat(lpath, &st);
        sprintf(fhdr.title, "%s (%.1fk)", index_title, st.st_size / 1024.);
        board[nb].k = st.st_size;   /* Ptt */
        printf("(%d)[%dK]", nb, board[nb].k);
        symlink(lpath, fpath);
        if (index_pos < 0)
        {
        fwrite(&fhdr, sizeof(fhdr), 1, fndx);
        rewind(fgem);
        while (fread(&fhdr, sizeof(fhdr), 1, fgem) == 1)
            fwrite(&fhdr, sizeof(fhdr), 1, fndx);
        fclose(fndx);
        fclose(fgem);
        rename(buf, pgem);
        }
        else
        {
        fseek(fgem, index_pos * sizeof(fhdr), 0);
        fwrite(&fhdr, sizeof(fhdr), 1, fgem);
        fclose(fgem);
        }
        return;
    }
    }
    fclose(fgem);
}


int main(int argc, char* argv[]){
    boardheader_t *bptr;
    DIR *dirp;
    struct dirent *de;
    int ch, n;
    int place = 0;
    char *fname, fpath[MAXPATHLEN];
    
    resolve_boards();
    nb = 0;
    if(argc == 1){
    puts("Creating the whole index...");
    chdir(strcpy(topdir, BBSHOME));
    strcpy(fpath, "man/.DIR");
    mandex(0, "", fpath);
    }
    
    
    chdir(strcpy(topdir, BBSHOME "/man/boards"));

    if(argc > 1) {
    sprintf(fpath, "%s/.DIR",  argv[1]);
    mandex(0, "", fpath);
    exit(0);
    }

    /* process all boards */
    
    if(!(dirp = opendir(topdir))) {
        printf("## unable to enter [man/boards]\n");
        exit(-1);
    }
    
    while((de = readdir(dirp))){
        fname = de->d_name;
        ch = fname[0];
        if (ch != '.'){
        board[nb].k = 0;
        strcpy(board[nb].bname, fname);
        sprintf(fpath, "%s/.rebuild", fname);
        if( access(fpath, 0) >= 0 ){
            unlink(fpath);

            sprintf(fpath, "%s/.DIR", fname);
            mandex(0, "", fpath);
            printf("%-14sd: %d\tf: %d\n", fname, ndir, nfile);
            /* report */
            board[nb].ndir = ndir;
            board[nb].nfile = nfile;
            if (board[nb].k)
            nb++;
        }
        }
    }
    closedir(dirp);

    qsort(board, nb, sizeof(boardinfo), k_cmp);
    
    if (!(fndx = fopen(BBSHOME "/etc/topboardman", "w")))
    exit(0);
    
    fprintf(fndx, "排名        看 版  目錄數   檔案數"
        "     byte數   總 分     版   主          \n");
    
    for (ch = 0; ch < nb; ch++){
    for (n = 0; n < numboards; n++){
        bptr = &bcache[n];
        if (!strcmp(bptr->brdname, board[ch].bname))
        break;
    }
    if (n >= numboards ||
        (bptr->brdattr & (BRD_BAD | BRD_NOCOUNT | BRD_HIDE))) 
        continue;
    if (board[ch].ndir + board[ch].nfile < 5)
        break;
    fprintf(fndx, "%3d.%15s %5d %7d %10d   %6d %-24.24s\n",
        ++place,
        board[ch].bname,
        board[ch].ndir, board[ch].nfile, board[ch].k
        ,board[ch].k / 100 + board[ch].nfile + board[ch].ndir
        ,bptr->BM);
    }
    fclose(fndx);
    exit(0);
}