summaryrefslogtreecommitdiffstats
path: root/util/mandex.c
diff options
context:
space:
mode:
Diffstat (limited to 'util/mandex.c')
-rw-r--r--util/mandex.c232
1 files changed, 132 insertions, 100 deletions
diff --git a/util/mandex.c b/util/mandex.c
index 9373f814..9f5a0e55 100644
--- a/util/mandex.c
+++ b/util/mandex.c
@@ -1,19 +1,13 @@
-/* $Id: mandex.c,v 1.1 2002/03/07 15:13:46 in2 Exp $ */
-
-/*
- target : 精華區索引程式 (man index)
-
- syntax : mandex [board]
- [board] 有值 ==> 只跑該 board
- 空的 ==> 所有的 boards 都跑
-*/
+/* $Id: mandex.c,v 1.2 2002/03/08 09:21:15 in2 Exp $ */
+/* 'mandex -h' to help */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "config.h"
@@ -24,33 +18,28 @@
#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
+typedef struct
{
char bname[40];
- int ndir;
- int nfile;
- int k;
-};
-typedef struct boardinfo boardinfo;
+ int ndir, nfile, k;
+} boardinfo;
-boardinfo
-board[MAX_BOARD];
+extern int numboards;
+extern boardheader_t *bcache;
+boardinfo board[MAX_BOARD], *biptr;
+char color[4][10] = {"", "", "", ""};
+char fn_index[] = ".index";
+char fn_new[] = ".index.new";
+char index_title[] = "◎ 精華區目錄索引";
+char topdir[128], pgem[512], pndx[512];
+FILE *fndx;
+int ndir, nfile, index_pos, k;
+int nSorted, nb;
+
+int sortbyname(const void *a, const void *b)
+{
+ return strcmp(((boardinfo*)b)->bname, ((boardinfo*)a)->bname);
+}
int k_cmp(b, a)
boardinfo *b, *a;
@@ -85,34 +74,30 @@ mandex(level, num_header, fpath)
return;
fname = strrchr(fpath, '.');
- if (!level)
- {
-
+ if (!level){
printf("%s\r\n",fpath);
strcpy(pgem, fpath);
strcpy(fname, fn_new);
fndx = fopen(fpath, "w");
- if (fndx == NULL)
- {
+ if (fndx == NULL){
fclose(fgem);
return;
}
fprintf(fndx, "序號\t\t\t精華區主題\n"
- "──────────────────────────────────────\n");
+ "───────────────────"
+ "───────────────────\n");
strcpy(pndx, fpath);
ndir = nfile = 0;
index_pos = -1;
}
count = 0;
- while (fread(&fhdr, sizeof(fhdr), 1, fgem) == 1)
- {
+ 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 < 0){
index_pos = count;
unlink(fpath);
}
@@ -120,14 +105,12 @@ mandex(level, num_header, fpath)
stat(fpath, &st);
sprintf(buf, "%.*s%s%3d. %s \n",
-
- 11 * level, num_header, color[level % 4], ++count, fhdr.title); /* Ptt */
+ 11 * level, num_header, color[level % 4], ++count, fhdr.title);
+ /* Ptt */
fputs(buf, fndx);
- if (dashd(fpath))
- {
+ if (dashd(fpath)){
++ndir;
- if (*fhdr.title != '#' && level < 10)
- {
+ if (*fhdr.title != '#' && level < 10){
strcat(fpath, "/.DIR");
mandex(level + 1, buf, fpath);
}
@@ -136,8 +119,7 @@ mandex(level, num_header, fpath)
++nfile;
}
- if (!level)
- {
+ if (!level){
char lpath[MAXPATHLEN];
fclose(fndx);
@@ -146,8 +128,7 @@ mandex(level, num_header, fpath)
strcpy(pndx, fpath);
sprintf(buf, "%s.new", pgem);
- if (index_pos >= 0 || (fndx = fopen(buf, "w")))
- {
+ if (index_pos >= 0 || (fndx = fopen(buf, "w"))){
fname[-1] = 0;
stamplink(fpath, &fhdr);
unlink(fpath);
@@ -156,11 +137,10 @@ mandex(level, num_header, fpath)
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);
+ k = st.st_size; /* Ptt */
+ printf("(%s)[%dK]", fpath, k);
symlink(lpath, fpath);
- if (index_pos < 0)
- {
+ if (index_pos < 0){
fwrite(&fhdr, sizeof(fhdr), 1, fndx);
rewind(fgem);
while (fread(&fhdr, sizeof(fhdr), 1, fgem) == 1)
@@ -169,8 +149,7 @@ mandex(level, num_header, fpath)
fclose(fgem);
rename(buf, pgem);
}
- else
- {
+ else{
fseek(fgem, index_pos * sizeof(fhdr), 0);
fwrite(&fhdr, sizeof(fhdr), 1, fgem);
fclose(fgem);
@@ -182,58 +161,111 @@ mandex(level, num_header, fpath)
}
-int main(int argc, char* argv[]){
+int main(int argc, char* argv[])
+{
boardheader_t *bptr;
- DIR *dirp;
- struct dirent *de;
- int ch, n;
- int place = 0;
- char *fname, fpath[MAXPATHLEN];
-
+ DIR *dirp;
+ struct dirent *de;
+ int ch, n, place = 0, fd;
+ char checkrebuild = 0, *fname, fpath[MAXPATHLEN];
+
+ while( (ch = getopt(argc, argv, "x")) != -1 ){
+ switch( ch ){
+ case 'x':
+ checkrebuild = 1;
+ break;
+ case 'h':
+ printf("NAME \n"
+ " mandex - 精華區索引程式 (man index)\n\n"
+ "SYNOPSIS \n"
+ " mandex [-x] [board] \n\n"
+ "DESCRIPTION \n"
+ "精華區索引 (man index) \n\n"
+ "-x 只有含有 .rebuild的目錄才重製 \n"
+ "board 全部的板 (default to all) \n\n");
+ return 0;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
resolve_boards();
- nb = 0;
- if(argc == 1){
+
+ if( argc == 0 ){
puts("Creating the whole index...");
- chdir(strcpy(topdir, BBSHOME));
- strcpy(fpath, "man/.DIR");
- mandex(0, "", fpath);
+ 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);
+ if( argc == 1 ){
+ sprintf(fpath, "%s/.DIR", argv[0]);
+ mandex(0, "", fpath);
+ return 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/.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++;
+ if( checkrebuild &&
+ (fd = open(BBSHOME "/man/.rank.cache", O_RDONLY)) >= 0 ){
+ read(fd, board, sizeof(board));
+ close(fd);
+ qsort(board, MAX_BOARD, sizeof(boardinfo), sortbyname);
+ for( nb = 0 ; board[nb].bname[0] != 0 ; ++nb )
+ ;
+ nSorted = nb;
+ }
+ else{
+ memset(board, 0, sizeof(board));
+ checkrebuild = 0;
+ nSorted = nb = 0;
+ }
+
+ 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 != '.'){
+ k = 0;
+ if( checkrebuild ){
+ sprintf(fpath, "%s/.rebuild", fname);
+ if( access(fpath, 0) < 0 ){
+ printf("skip no modify board %s\n", fname);
+ continue;
+ }
+ unlink(fpath);
+ }
+ sprintf(fpath, "%s/.DIR", fname);
+ mandex(0, "", fpath);
+ printf("%-14sd: %d\tf: %d\n", fname, ndir, nfile); /* report */
+ if( k ){
+ if( !(biptr = bsearch(fname, board, nSorted,
+ sizeof(boardinfo), sortbyname))){
+ biptr = &board[nb];
+ ++nb;
+ }
+ strcpy(biptr->bname, fname);
+ biptr->ndir = ndir;
+ biptr->nfile = nfile;
+ biptr->k = k;
}
}
- closedir(dirp);
-
- qsort(board, nb, sizeof(boardinfo), k_cmp);
+ }
+ closedir(dirp);
+ qsort(board, nb, sizeof(boardinfo), k_cmp);
+ unlink(BBSHOME "/man/.rank.cache");
+ if( (fd = open(BBSHOME "/man/.rank.cache",
+ O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0660)) >= 0 ){
+ write(fd, board, sizeof(board));
+ close(fd);
+ }
if (!(fndx = fopen(BBSHOME "/etc/topboardman", "w")))
exit(0);
@@ -259,5 +291,5 @@ int main(int argc, char* argv[]){
,bptr->BM);
}
fclose(fndx);
- exit(0);
+ return 0;
}