File: checkgroups.c

package info (click to toggle)
leafnode 1.11.11-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,468 kB
  • sloc: ansic: 10,914; sh: 1,748; xml: 628; makefile: 291; perl: 84; sed: 4
file content (122 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (7)
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
/*
 * Checkgroups: script that updates the description of newsgroups
 * Input file : a checkgroups script that contains the name of
 *              the newsgroup and its description in one line
 *
 * Written and copyrighted by Cornelius Krasel, April 1997
 * Source code borrows a lot from fetch(1).
 *
 * See file COPYING for restrictions on the use of this software.
 */

#include "leafnode.h"
#include "ln_log.h"

#include <sys/types.h>
#include <ctype.h>
#include "system.h"
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>

int debug = 0;
int verbose = 0;

static void
process_input(char *s)
{
    FILE *f;
    char *l;
    static time_t now;

    if (!now) now = time(NULL);

    f = fopen(s, "r");
    if (!f) {
	fprintf(stderr, "cannot open %s: %s\n", s, strerror(errno));
	return;
    }

    while ((l = getaline(f))) {
	char *p;
	struct newsgroup *g;

	p = l;
	if (isalnum((unsigned char)*p)) {
	    while (*p && !isspace((unsigned char)*p))
		p++;
	    if (*p)
		*p++ = '\0';
	    if ((g = findgroup(l)) != NULL) {
		fprintf(stderr, "%s\n", l);
		if (strlen(p) > 0) {
		    if (g->desc)
			free(g->desc);
		    g->desc = critstrdup(p, "process_input");
		}
	    } else {
		fprintf(stderr, "%s NEW\n", l);
		insertgroup(l, 1, 0, now);
		if (*p)
		    newgroupdesc(l, p);
	    }
	}
    }

    fclose(f);
    mergegroups();
}

int
main(int argc, char *argv[])
{
    int fd;
    myopenlog("checkgroups");

    fd = open(".", O_RDONLY);
    if (fd < 0) {
	perror("checkgroups: cannot store cwd");
	exit(EXIT_FAILURE);
    }

    if (!initvars(argv[0]))
	exit(EXIT_FAILURE);

    if (fchdir(fd)) {
	perror("checkgroups: cannot restore cwd");
	exit(EXIT_FAILURE);
    }
    (void)close(fd);

    if (argc != 2) {
	fprintf(stderr, "Usage: %s checkgroupsfile\n", argv[0]);
	exit(1);
    }

    umask(2);

    if (readconfig(0)) {
	fprintf(stderr, "cannot read configuration.\n");
	exit(EXIT_FAILURE);
    }

    if (try_lock(timeout_lock)) {
	fprintf(stderr, "could not obtain lock.\n");
	exit(EXIT_FAILURE);
    }

    readactive();		/* read groupinfo file */
    if (!active)
	fakeactive();		/* make sure we have up to date water marks for existing groups */
    process_input(argv[1]);
    if (writeactive()) ln_log(LNLOG_SERR, LNLOG_CTOP, "Error writing groupinfo.");
    unlink(lockfile);
    freeactive(active);
    freegetaline();
    exit(0);
}