This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbfido/mbindex.c

972 lines
19 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Nodelist Compiler
*
*****************************************************************************
2004-01-25 10:57:35 +00:00
* Copyright (C) 1997-2004
2001-08-17 05:46:24 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbsedb.h"
2001-08-17 05:46:24 +00:00
typedef struct _nl_list {
2004-07-10 11:46:35 +00:00
struct _nl_list *next;
struct _nlidx idx;
2001-08-17 05:46:24 +00:00
} nl_list;
2004-07-10 15:03:49 +00:00
typedef struct _nl_user {
struct _nl_user *next;
struct _nlusr udx;
} nl_user;
2001-08-17 05:46:24 +00:00
#include "mbindex.h"
FILE *ifp, *ufp, *ffp;
2004-07-10 15:03:49 +00:00
long total = 0, entries = 0, users = 0;
2001-08-17 05:46:24 +00:00
int filenr = 0;
unsigned short regio;
nl_list *nll = NULL;
2004-07-10 15:03:49 +00:00
nl_user *nlu = NULL;
2001-08-17 05:46:24 +00:00
extern int do_quiet; /* Quiet flag */
extern int show_log; /* Show logging on screen */
time_t t_start; /* Start time */
time_t t_end; /* End time */
2004-07-10 13:26:16 +00:00
2001-08-17 05:46:24 +00:00
/*
* If we don't know what to type
*/
void Help(void)
{
2004-07-10 11:46:35 +00:00
do_quiet = FALSE;
ProgName();
mbse_colour(LIGHTCYAN, BLACK);
2004-07-10 11:46:35 +00:00
printf("\nUsage: mbindex <options>\n\n");
mbse_colour(LIGHTBLUE, BLACK);
2004-07-10 11:46:35 +00:00
printf(" Options are:\n\n");
mbse_colour(CYAN, BLACK);
2004-07-10 11:46:35 +00:00
printf(" -quiet Quiet mode\n");
mbse_colour(LIGHTGRAY, BLACK);
2004-07-10 11:46:35 +00:00
printf("\n");
die(MBERR_COMMANDLINE);
2001-08-17 05:46:24 +00:00
}
/*
* Header, only if not quiet.
*/
void ProgName(void)
{
2004-07-10 11:46:35 +00:00
if (do_quiet)
return;
mbse_colour(WHITE, BLACK);
2004-07-10 11:46:35 +00:00
printf("\nMBINDEX: MBSE BBS %s Nodelist Index Compiler\n", VERSION);
mbse_colour(YELLOW, BLACK);
2004-07-10 11:46:35 +00:00
printf(" %s\n", COPYRIGHT);
mbse_colour(CYAN, BLACK);
2001-08-17 05:46:24 +00:00
}
void die(int onsig)
{
2003-08-03 14:08:07 +00:00
if (onsig && (onsig < NSIG))
signal(onsig, SIG_IGN);
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
ulockprogram((char *)"mbindex");
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (!do_quiet) {
mbse_colour(CYAN, BLACK);
2003-08-03 14:08:07 +00:00
show_log = TRUE;
}
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (IsSema((char *)"mbindex"))
RemoveSema((char *)"mbindex");
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (onsig) {
if (onsig <= NSIG)
WriteError("Terminated on signal %d (%s)", onsig, SigName[onsig]);
else
WriteError("Terminated with error %d", onsig);
}
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
t_end = time(NULL);
Syslog(' ', "MBINDEX finished in %s", t_elapsed(t_start, t_end));
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (!do_quiet)
mbse_colour(LIGHTGRAY, BLACK);
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
ExitClient(onsig);
2001-08-17 05:46:24 +00:00
}
int main(int argc,char *argv[])
{
2003-08-03 14:08:07 +00:00
int i;
char *cmd;
struct passwd *pw;
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
InitConfig();
InitFidonet();
mbse_TermInit(1, 80, 25);
2003-08-03 14:08:07 +00:00
t_start = time(NULL);
umask(002);
/*
* Catch all the signals we can, and ignore the rest.
* Don't listen to SIGTERM.
*/
for (i = 0; i < NSIG; i++) {
if ((i == SIGHUP) || (i == SIGINT) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV))
signal(i, (void (*))die);
2003-12-04 22:07:33 +00:00
else if ((i != SIGKILL) && (i != SIGSTOP))
2003-08-03 14:08:07 +00:00
signal(i, SIG_IGN);
}
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
cmd = xstrcpy((char *)"Command line: mbindex");
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (argc > 2)
Help();
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (argc == 2) {
cmd = xstrcat(cmd, (char *)" ");
cmd = xstrcat(cmd, argv[1]);
2001-08-17 05:46:24 +00:00
2003-08-03 14:08:07 +00:00
if (strncasecmp(argv[1], "-q", 2) == 0)
do_quiet = TRUE;
2002-10-20 20:58:55 +00:00
else
2003-08-03 14:08:07 +00:00
Help();
}
ProgName();
pw = getpwuid(getuid());
2003-09-09 19:39:51 +00:00
InitClient(pw->pw_name, (char *)"mbindex", CFG.location, CFG.logfile,
CFG.util_loglevel, CFG.error_log, CFG.mgrlog, CFG.debuglog);
2003-08-03 14:08:07 +00:00
Syslog(' ', " ");
Syslog(' ', "MBINDEX v%s", VERSION);
Syslog(' ', cmd);
free(cmd);
2004-03-21 12:55:45 +00:00
if (enoughspace(CFG.freespace) == 0)
2003-08-03 14:08:07 +00:00
die(MBERR_DISK_FULL);
if (lockprogram((char *)"mbindex")) {
if (!do_quiet)
printf("Can't lock mbindex, abort.\n");
die(MBERR_NO_PROGLOCK);
}
if (nodebld())
die(MBERR_GENERAL);
else
die(MBERR_OK);
return 0;
2001-08-17 05:46:24 +00:00
}
void tidy_nllist(nl_list **fap)
{
2004-07-10 11:46:35 +00:00
nl_list *tmp, *old;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
for (tmp = *fap; tmp; tmp = old) {
old = tmp->next;
free(tmp);
}
*fap = NULL;
2001-08-17 05:46:24 +00:00
}
int in_nllist(struct _nlidx idx, nl_list **fap, int replace)
{
2004-07-10 11:46:35 +00:00
nl_list *tmp;
for (tmp = *fap; tmp; tmp = tmp->next) {
if ((tmp->idx.zone == idx.zone) && (tmp->idx.net == idx.net) &&
(tmp->idx.node == idx.node) && (tmp->idx.point == idx.point)) {
if (replace) {
tmp->idx = idx;
entries++;
}
regio = tmp->idx.region;
return TRUE;
2001-08-17 05:46:24 +00:00
}
2004-07-10 11:46:35 +00:00
}
return FALSE;
2001-08-17 05:46:24 +00:00
}
void fill_nllist(struct _nlidx idx, nl_list **fap)
{
2004-07-10 11:46:35 +00:00
nl_list *tmp;
tmp = (nl_list *)malloc(sizeof(nl_list));
tmp->next = *fap;
tmp->idx = idx;
*fap = tmp;
total++;
entries++;
2001-08-17 05:46:24 +00:00
}
char *fullpath(char *fname)
{
2004-07-10 11:46:35 +00:00
static char path[PATH_MAX];
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
sprintf(path, "%s/%s", CFG.nodelists, fname);
return path;
2001-08-17 05:46:24 +00:00
}
void sort_nllist(nl_list **fap)
{
2004-07-10 11:46:35 +00:00
nl_list *ta, **vector;
size_t n = 0, i;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
if (*fap == NULL)
return;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
for (ta = *fap; ta; ta = ta->next)
n++;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
vector = (nl_list **)malloc(n * sizeof(nl_list *));
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
i = 0;
for (ta = *fap; ta; ta = ta->next) {
vector[i++] = ta;
}
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
qsort(vector, n, sizeof(nl_list *), (int(*)(const void*, const void *))comp_node);
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
(*fap) = vector[0];
i = 1;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
for (ta = *fap; ta; ta = ta->next) {
if (i < n)
ta->next = vector[i++];
else
ta->next = NULL;
}
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
free(vector);
return;
2001-08-17 05:46:24 +00:00
}
2004-07-10 15:03:49 +00:00
void tidy_nluser(nl_user **fap)
{
nl_user *tmp, *old;
for (tmp = *fap; tmp; tmp = old) {
old = tmp->next;
free(tmp);
}
*fap = NULL;
}
void fill_nluser(struct _nlusr udx, nl_user **fap)
{
nl_user *tmp;
tmp = (nl_user *)malloc(sizeof(nl_user));
tmp->next = *fap;
tmp->udx = udx;
*fap = tmp;
users++;
}
void sort_nluser(nl_user **fap)
{
nl_user *ta, **vector;
size_t n = 0, i;
if (*fap == NULL)
return;
for (ta = *fap; ta; ta = ta->next)
n++;
vector = (nl_user **)malloc(n * sizeof(nl_user *));
i = 0;
for (ta = *fap; ta; ta = ta->next) {
vector[i++] = ta;
}
qsort(vector, n, sizeof(nl_user *), (int(*)(const void*, const void *))comp_user);
(*fap) = vector[0];
i = 1;
for (ta = *fap; ta; ta = ta->next) {
if (i < n)
ta->next = vector[i++];
else
ta->next = NULL;
}
free(vector);
return;
}
2001-08-17 05:46:24 +00:00
int comp_node(nl_list **fap1, nl_list **fap2)
{
2004-07-10 11:46:35 +00:00
if ((*fap1)->idx.zone != (*fap2)->idx.zone)
return ((*fap1)->idx.zone - (*fap2)->idx.zone);
else if ((*fap1)->idx.net != (*fap2)->idx.net)
return ((*fap1)->idx.net - (*fap2)->idx.net);
else if ((*fap1)->idx.node != (*fap2)->idx.node)
return ((*fap1)->idx.node - (*fap2)->idx.node);
else
return ((*fap1)->idx.point - (*fap2)->idx.point);
2001-08-17 05:46:24 +00:00
}
2004-07-10 15:03:49 +00:00
int comp_user(nl_user **fap1, nl_user **fap2)
{
return strcmp((*fap1)->udx.user, (*fap2)->udx.user);
}
2001-08-17 05:46:24 +00:00
int compile(char *nlname, unsigned short zo, unsigned short ne, unsigned short no)
{
2004-07-10 13:26:16 +00:00
int num, i, lineno, boss = FALSE, bossvalid = FALSE;
2004-07-10 11:46:35 +00:00
unsigned short upnet, upnode;
char buf[MAXNLLINELEN], *p, *q;
faddr *tmpa;
FILE *nl;
struct _nlidx ndx;
struct _nlusr udx;
if ((nl = fopen(fullpath(nlname), "r")) == NULL) {
WriteError("$Can't open %s", fullpath(nlname));
2004-07-10 13:26:16 +00:00
return MBERR_INIT_ERROR;
2004-07-10 11:46:35 +00:00
}
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
Syslog('+', "Compiling \"%s\" (%d)", nlname, filenr);
IsDoing("Compile NL %d", filenr +1);
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
memset(&ndx, 0, sizeof(ndx));
ndx.type = NL_NODE;
ndx.fileno = filenr;
upnet = 0;
upnode = 0;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
/*
* If zone is set, it is a overlay segment
*/
if (zo) {
ndx.zone = zo;
ndx.net = ne;
ndx.node = no;
ndx.point = 0;
}
entries = 0;
lineno = 0;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
while (!feof(nl)) {
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
Nopper();
ndx.offset = ftell(nl);
lineno++;
if (fgets(buf, sizeof(buf)-1, nl) == NULL)
continue;
2001-08-17 05:46:24 +00:00
2004-07-10 11:46:35 +00:00
/*
* Next check at <lf> and <eof> characters
*/
if ((*(buf+strlen(buf) -1) != '\n') && (*(buf + strlen(buf) -1) != '\012')) {
while (fgets(buf, sizeof(buf) -1, nl) && (*(buf + strlen(buf) -1) != '\n')) /*void*/;
if (strlen(buf) > 1) /* Suppress EOF character */
Syslog('-', "Nodelist: too long line junked (%d)", lineno);
continue;
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (*(p=buf+strlen(buf) -1) == '\n')
*p-- = '\0';
if (*p == '\r')
*p = '\0';
if ((buf[0] == ';') || (buf[0] == '\032') || (buf[0] == '\0'))
continue;
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (CFG.slow_util && do_quiet) {
if (zo) {
msleep(1);
} else {
if ((lineno % 40) == 0)
msleep(1);
}
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if ((p = strchr(buf, ',')))
*p++ = '\0';
else {
/*
* Extra check for valid datalines, there should be at least one comma.
*/
2004-08-15 10:19:26 +00:00
WriteError("%s(%u): invalid dataline 1", nlname,lineno);
2004-07-10 13:26:16 +00:00
continue;
}
if ((q = strchr(p, ',')))
*q++ = '\0';
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
ndx.type = NL_NONE;
ndx.pflag = 0;
if (buf[0] == '\0') {
if (boss)
ndx.type = NL_POINT;
else
ndx.type = NL_NODE;
} else {
if (strcasecmp(buf,"Boss") == 0) {
ndx.type = NL_POINT;
bossvalid = FALSE;
if ((tmpa=parsefnode(p)) == NULL) {
WriteError("%s(%u): unparsable Boss addr \"%s\"", nlname,lineno,p);
continue;
2001-08-17 05:46:24 +00:00
}
2004-07-10 13:26:16 +00:00
boss = TRUE;
if (tmpa->zone)
ndx.zone = tmpa->zone;
ndx.net = tmpa->net;
ndx.node = tmpa->node;
ndx.point = 0;
tidy_faddr(tmpa);
ndx.type = NL_NONE;
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (in_nllist(ndx, &nll, FALSE)) {
bossvalid = TRUE;
}
continue; /* no further processing */
} else {
boss = FALSE;
ndx.type = NL_NONE;
if (!strcasecmp(buf, "Down")) {
ndx.pflag |= NL_DOWN;
ndx.type = NL_NODE;
}
if (!strcasecmp(buf, "Hold")) {
ndx.pflag |= NL_HOLD;
ndx.type = NL_NODE;
}
if (!strcasecmp(buf, "Pvt")) {
ndx.pflag |= NL_PVT;
ndx.type = NL_NODE;
2001-08-17 05:46:24 +00:00
}
2004-07-10 13:26:16 +00:00
if (!strcasecmp(buf, "Zone"))
ndx.type = NL_ZONE;
if (!strcasecmp(buf, "Region"))
ndx.type = NL_REGION;
if (!strcasecmp(buf, "Host"))
ndx.type = NL_HOST;
if (!strcasecmp(buf, "Hub"))
ndx.type = NL_HUB;
if (!strcasecmp(buf, "Point")) {
ndx.type = NL_POINT;
bossvalid = TRUE;
2001-08-17 05:46:24 +00:00
}
2004-07-10 13:26:16 +00:00
}
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (ndx.type == NL_NONE) {
for (q = buf; *q; q++)
if (*q < ' ')
*q='.';
WriteError("%s(%u): unidentified entry \"%s\"", nlname, lineno, buf);
continue;
}
if ((num=atoi(p)) == 0) {
WriteError("%s(%u): bad numeric \"%s\"", nlname,lineno,p);
continue;
}
/*
* now update the current address
*/
switch (ndx.type) {
case NL_REGION: ndx.net = num;
2001-08-17 05:46:24 +00:00
ndx.node = 0;
ndx.point = 0;
ndx.upnet = ndx.zone;
ndx.upnode= 0;
ndx.region= num;
upnet = num;
upnode = 0;
break;
2004-07-10 13:26:16 +00:00
case NL_ZONE: ndx.zone = num;
2001-08-17 05:46:24 +00:00
ndx.net = num;
ndx.node = 0;
ndx.point = 0;
ndx.upnet = 0;
ndx.upnode= 0;
ndx.region= 0;
upnet = num;
upnode = 0;
break;
2004-07-10 13:26:16 +00:00
case NL_HOST: ndx.net = num;
2001-08-17 05:46:24 +00:00
ndx.node = 0;
ndx.point = 0;
ndx.upnet = ndx.region;
ndx.upnode= 0;
upnet = num;
upnode = 0;
break;
2004-07-10 13:26:16 +00:00
case NL_HUB: ndx.node = num;
2001-08-17 05:46:24 +00:00
ndx.point = 0;
ndx.upnet = ndx.net;
ndx.upnode= 0;
upnet = ndx.net;
upnode = num;
break;
2004-07-10 13:26:16 +00:00
case NL_NODE: ndx.node = num;
2001-08-17 05:46:24 +00:00
ndx.point = 0;
ndx.upnet = upnet;
ndx.upnode= upnode;
break;
2004-07-10 13:26:16 +00:00
case NL_POINT: ndx.point = num;
2001-08-17 05:46:24 +00:00
ndx.upnet = ndx.net;
ndx.upnode= ndx.node;
if ((!ndx.region) && bossvalid)
2004-07-10 13:26:16 +00:00
ndx.region = regio;
2001-08-17 05:46:24 +00:00
break;
2004-07-10 13:26:16 +00:00
}
if (!do_quiet) {
printf("\rZone %-6uRegion %-6uNet %-6uNode %-6uPoint %-6u",
ndx.zone, ndx.region, ndx.net, ndx.node, ndx.point);
fflush(stdout);
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
memset(&udx, 0, sizeof(udx));
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
/*
* Read nodelist line and extract username.
*/
for (i = 0; i < 3; i++) {
p = q;
2004-07-10 15:03:49 +00:00
if (p == NULL) {
2004-08-15 10:19:26 +00:00
WriteError("%s(%u): invalid dataline 3", nlname,lineno);
2004-07-10 13:26:16 +00:00
continue;
2004-07-10 15:03:49 +00:00
}
2004-07-10 13:26:16 +00:00
if ((q = strchr(p, ',')))
*q++ = '\0';
if (q == NULL)
q = p;
}
if (strlen(p) > 35)
2004-07-10 14:02:55 +00:00
p[36] = '\0';
strcpy(udx.user, p);
udx.zone = ndx.zone;
udx.net = ndx.net;
udx.node = ndx.node;
udx.point = ndx.point;
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
/*
2004-07-10 14:02:55 +00:00
* Now search for the baudrate field, just to check if it's there.
2004-07-10 13:26:16 +00:00
*/
for (i = 0; i < 2; i++) {
p = q;
2004-07-10 15:03:49 +00:00
if (p == NULL) {
2004-08-15 10:19:26 +00:00
WriteError("%s(%u): invalid dataline 4", nlname,lineno);
2004-07-10 13:26:16 +00:00
continue;
2004-07-10 15:03:49 +00:00
}
2004-07-10 13:26:16 +00:00
if ((q = strchr(p, ',')))
*q++ = '\0';
if (q == NULL)
q = p;
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
/*
* If zone, net and node given, then this list is an
* overlay so we will call in_list() to replace the
* existing records, or append them if they don't exist.
* Also, only points with a valid boss will be added.
*/
if (zo) {
if (!(in_nllist(ndx, &nll, TRUE))) {
if (ndx.point && bossvalid) {
fill_nllist(ndx, &nll);
}
if (!ndx.point)
fill_nllist(ndx, &nll);
}
} else
fill_nllist(ndx, &nll);
2004-07-10 15:03:49 +00:00
fill_nluser(udx, &nlu);
2004-07-10 13:26:16 +00:00
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
fclose(nl);
Syslog('+', "%d entries", entries);
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (!do_quiet) {
printf(" %ld entries\n", entries);
fflush(stdout);
}
return 0;
2001-08-17 05:46:24 +00:00
}
/*
* Tidy the filearray
*/
void tidy_fdlist(fd_list **fdp)
{
2004-07-10 13:26:16 +00:00
fd_list *tmp, *old;
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
for (tmp = *fdp; tmp; tmp = old) {
old = tmp->next;
free(tmp);
}
*fdp = NULL;
2001-08-17 05:46:24 +00:00
}
/*
* Add a file on the array.
*/
void fill_fdlist(fd_list **fdp, char *filename, time_t filedate)
{
2004-07-10 13:26:16 +00:00
fd_list *tmp;
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
tmp = (fd_list *)malloc(sizeof(fd_list));
tmp->next = *fdp;
sprintf(tmp->fname, "%s", filename);
tmp->fdate = filedate;
*fdp = tmp;
2001-08-17 05:46:24 +00:00
}
int compfdate(fd_list **, fd_list **);
/*
* Sort the array of files by filedate
*/
void sort_fdlist(fd_list **fdp)
{
fd_list *ta, **vector;
size_t n = 0, i;
if (*fdp == NULL) {
return;
}
for (ta = *fdp; ta; ta = ta->next)
n++;
vector = (fd_list **)malloc(n * sizeof(fd_list *));
i = 0;
for (ta = *fdp; ta; ta = ta->next) {
vector[i++] = ta;
}
qsort(vector, n, sizeof(fd_list*), (int(*)(const void*, const void*))compfdate);
(*fdp) = vector[0];
i = 1;
for (ta = *fdp; ta; ta = ta->next) {
if (i < n)
ta->next = vector[i++];
else
ta->next = NULL;
}
free(vector);
return;
}
int compfdate(fd_list **fdp1, fd_list **fdp2)
{
return ((*fdp1)->fdate - (*fdp2)->fdate);
}
/*
* Return the name of the oldest file in the array
*/
char *pull_fdlist(fd_list **fdp)
{
static char buf[65];
fd_list *ta;
if (*fdp == NULL)
return NULL;
ta = *fdp;
memset(&buf, 0, sizeof(buf));
sprintf(buf, "%s", ta->fname);
if (ta->next != NULL)
*fdp = ta->next;
else
*fdp = NULL;
free(ta);
return buf;
}
int makelist(char *base, unsigned short zo, unsigned short ne, unsigned short no)
{
2004-07-10 15:03:49 +00:00
int rc = 0, files = 0;
struct dirent *de;
DIR *dp;
char *p = NULL, *q;
struct _nlfil fdx;
fd_list *fdl = NULL;
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
if (!strlen(base)) {
WriteError("Error, no nodelist defined for %d:%d/%d", zo, ne, no);
return 0;
}
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
if ((dp = opendir(CFG.nodelists)) == NULL) {
WriteError("$Can't open dir %s", CFG.nodelists);
rc = MBERR_GENERAL;
} else {
while ((de = readdir(dp))) {
if (strncmp(de->d_name, base, strlen(base)) == 0) {
2001-08-17 05:46:24 +00:00
/*
2004-07-10 15:03:49 +00:00
* Extension must be at least 2 digits
2001-08-17 05:46:24 +00:00
*/
2004-07-10 15:03:49 +00:00
q = (de->d_name) + strlen(base);
if ((*q == '.') && (strlen(q) > 2) && (strspn(q+1,"0123456789") == (strlen(q)-1))) {
/*
* Add matched filenames to the array
*/
fill_fdlist(&fdl, de->d_name, file_time(fullpath(de->d_name)));
files++;
2001-08-17 05:46:24 +00:00
}
2004-07-10 15:03:49 +00:00
}
}
closedir(dp);
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
if (files == 0) {
Syslog('+', "No nodelist found for %s", base);
return MBERR_GENERAL;
}
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
/*
* Sort found nodelists by age and kill all but the newest 2.
*/
sort_fdlist(&fdl);
while (files) {
p = pull_fdlist(&fdl);
if (files > 2) {
Syslog('+', "Remove old \"%s\"", p);
unlink(fullpath(p));
}
files--;
2001-08-17 05:46:24 +00:00
}
2004-07-10 15:03:49 +00:00
tidy_fdlist(&fdl);
memset(&fdx, 0, sizeof(fdx));
sprintf(fdx.filename, "%s", p);
sprintf(fdx.domain, "%s", fidonet.domain);
fdx.number = filenr;
fwrite(&fdx, sizeof(fdx), 1, ffp);
rc = compile(p, zo, ne, no);
filenr++;
}
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
return rc;
2001-08-17 05:46:24 +00:00
}
int nodebld(void)
{
2004-07-10 13:26:16 +00:00
int rc = 0, i;
2004-07-10 14:02:55 +00:00
char *im, *fm, *um, *old, *new;
2004-07-10 13:26:16 +00:00
struct _nlfil fdx;
2004-07-10 14:02:55 +00:00
FILE *fp;
2004-07-10 13:26:16 +00:00
nl_list *tmp;
2004-07-10 15:03:49 +00:00
nl_user *tmu;
2004-07-10 13:26:16 +00:00
memset(&fdx, 0, sizeof(fdx));
im = xstrcpy(fullpath((char *)"temp.index"));
fm = xstrcpy(fullpath((char *)"temp.files"));
um = xstrcpy(fullpath((char *)"temp.users"));
if ((ifp = fopen(im, "w+")) == NULL) {
WriteError("$Can't create %s",MBSE_SS(im));
return MBERR_GENERAL;
}
if ((ufp = fopen(um, "w+")) == NULL) {
WriteError("$Can't create %s", MBSE_SS(um));
fclose(ifp);
unlink(im);
return MBERR_GENERAL;
}
if ((ffp = fopen(fm, "w+")) == NULL) {
WriteError("$Can't create %s", MBSE_SS(fm));
fclose(ifp);
unlink(im);
fclose(ufp);
unlink(um);
return MBERR_GENERAL;
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (!do_quiet) {
mbse_colour(CYAN, BLACK);
2004-07-10 13:26:16 +00:00
printf("\n");
}
2001-08-17 05:46:24 +00:00
2004-07-10 14:02:55 +00:00
if ((fp = fopen(fidonet_fil, "r")) == 0)
rc = MBERR_GENERAL;
else {
fread(&fidonethdr, sizeof(fidonethdr), 1, fp);
2004-07-10 13:26:16 +00:00
2004-07-10 14:02:55 +00:00
while (fread(&fidonet, fidonethdr.recsize, 1, fp) == 1) {
if (fidonet.available) {
rc = makelist(fidonet.nodelist, 0, 0, 0);
if (rc)
2004-07-10 13:26:16 +00:00
break;
2004-07-10 14:02:55 +00:00
for (i = 0; i < 6; i++) {
if (fidonet.seclist[i].zone) {
rc = makelist(fidonet.seclist[i].nodelist, fidonet.seclist[i].zone,
2004-07-10 13:26:16 +00:00
fidonet.seclist[i].net, fidonet.seclist[i].node);
2004-07-10 14:02:55 +00:00
if (rc)
break;
2004-07-10 13:26:16 +00:00
}
2001-08-17 05:46:24 +00:00
}
2004-07-10 14:02:55 +00:00
if (rc)
break;
2004-07-10 13:26:16 +00:00
}
2001-08-17 05:46:24 +00:00
}
2004-07-10 14:02:55 +00:00
fclose(fp);
2004-07-10 13:26:16 +00:00
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
fclose(ffp);
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
if (rc == 0) {
IsDoing("Sorting nodes");
sort_nllist(&nll);
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
IsDoing("Sorting users");
sort_nluser(&nlu);
2004-07-10 13:26:16 +00:00
IsDoing("Writing files");
for (tmp = nll; tmp; tmp = tmp->next)
fwrite(&tmp->idx, sizeof(struct _nlidx), 1, ifp);
fclose(ifp);
2001-08-17 05:46:24 +00:00
2004-07-10 15:03:49 +00:00
for (tmu = nlu; tmu; tmu = tmu->next)
fwrite(&tmu->udx, sizeof(struct _nlusr), 1, ufp);
fclose(ufp);
2004-07-10 13:26:16 +00:00
Syslog('+', "Compiled %d entries", total);
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
/*
* Rename existing files to old.*, they stay on disk in
* case they are open by some program. The temp.* files
* are then renamed to node.*
*/
old = xstrcpy(fullpath((char *)"old.index"));
new = xstrcpy(fullpath((char *)"node.index"));
unlink(old);
rename(new, old);
rename(im, new);
free(old);
free(new);
old = xstrcpy(fullpath((char *)"old.users"));
new = xstrcpy(fullpath((char *)"node.users"));
unlink(old);
rename(new, old);
rename(um, new);
free(old);
free(new);
old = xstrcpy(fullpath((char *)"old.files"));
new = xstrcpy(fullpath((char *)"node.files"));
unlink(old);
rename(new, old);
rename(fm, new);
free(old);
free(new);
} else {
fclose(ifp);
Syslog('+', "Compile failed, rc=%d", rc);
unlink(im);
unlink(fm);
unlink(um);
}
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
free(im);
free(fm);
free(um);
tidy_nllist(&nll);
2004-07-11 11:56:03 +00:00
tidy_nluser(&nlu);
2001-08-17 05:46:24 +00:00
2004-07-10 13:26:16 +00:00
return rc;
2001-08-17 05:46:24 +00:00
}