Metropoli BBS
VIEWER: updactiv.c MODE: TEXT (ASCII)
/*
 * This code was written by Harald Kipp
 *
 * Parts were taken from the bnews expire daemon written
 * by Rick Adams, who included the following notice:
 *
 *  "Permission is hereby granted to copy, reproduce, redistribute or
 *   otherwise use this software as long as: there is no monetary
 *   profit gained specifically from the use or reproduction or this
 *   software, it is not sold, rented, traded or otherwise marketed, and
 *   this copyright notice is included prominently in any copy made."
 *
 * Bug reports should be sent to
 *
 *  harald@os2point.ping.de
 *  harald@sesam.com
 *  Fido: 2:2448/434
 *
 * This module contains routines to update the active file.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include <lprintf.h>
#include <chanlib.h>

#include "config.h"
#include "expire.h"


/************************************************************************/
/*                                                                      */
/************************************************************************/
int UpdateActive(void)
{
    FILE *ofp, *nfp;
    long maxart, minart;
    int  lineno = 0;
    char *cp;
    char mc;
    char *line;
    char *ngname;
    char *ngdir;

    if((ofp = xopen(cfg.activefile, "rt")) == NULL) {
        lperror(cfg.activefile);
        return(0);
    }
    if((nfp = xopen(cfg.newactfile, "wt")) == NULL) {
        fclose(ofp);
        lperror(cfg.newactfile);
        return(0);
    }

    line = malloc(BUFSIZ);
    ngname = malloc(BUFSIZ);
    ngdir = malloc(_MAX_PATH);

    /*
     * Loop for each line in current active file
     */
    while(fgets(line, BUFSIZ, ofp)) {
        if(sscanf(line, "%s %ld %ld %c", ngname, &maxart, &minart, &mc) < 4) {
            lprintf("Line %d in active file invalid: %s", lineno, line);
            continue;
        }
        lineno++;

        /*
         * If this groups is excluded from update, then
         * we simply transfer the line to the new file.
         */
        if(!ngmatch(ngname, cfg.groups)) {
            if(fputs(line, nfp)) {
                lperror(cfg.newactfile);
                lineno = 0;
                break;
            }
	    continue;
	}

        /*
         * Build path to the newsgroup and scan directory
         */
        strcpy(ngdir, cfg.newsdir);
        strcat(ngdir, "\\");
        cp = strchr(ngdir, '\0');
        strcpy(cp, ngname);
        while(*cp) {
            if(*cp == '.')
                *cp = '\\';
            cp++;
        }
        scan_minmax(ngdir, &minart, &maxart);

        /*
         * Write result to new active file
         */
        if(fprintf(nfp, "%s %lu %lu %c\n", ngname, maxart, minart, mc) <= 0) {
            lperror(cfg.newactfile);
            lineno = 0;
            break;
        }
    }
    if(fclose(nfp)) {
        lperror(cfg.newactfile);
        lineno = 0;
    }
    fclose(ofp);

    if(lineno) {
        if(DOLOG(LOG_TESTMODE))
            lprintf("Would delete old %s", cfg.oldactfile);
        else if(unlink(cfg.oldactfile) == 0)
            lprintf("%s deleted", cfg.oldactfile);

        if(DOLOG(LOG_TESTMODE))
            lprintf("Would rename %s to %s", cfg.activefile, cfg.oldactfile);
        else if(rename(cfg.activefile, cfg.oldactfile)) {
            lprintf("Failed renaming %s to %s", cfg.activefile, cfg.oldactfile);
            lineno = 0;
        }
    }
    if(lineno) {
        if(DOLOG(LOG_TESTMODE))
            lprintf("Would rename %s to %s", cfg.newactfile, cfg.activefile);
        else if(rename(cfg.newactfile, cfg.activefile)) {
            lprintf("Failed renaming %s to %s", cfg.newactfile, cfg.activefile);
            lineno = 0;
        }
    }

    free(ngname);
    free(line);
    free(ngdir);

    return(lineno);
}
[ RETURN TO DIRECTORY ]