File: misc.c

package info (click to toggle)
gup 0.5.17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 200 kB
  • sloc: ansic: 1,609; sh: 211; makefile: 34
file content (226 lines) | stat: -rw-r--r-- 4,633 bytes parent folder | download | duplicates (4)
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
#include "gup.h"

/*
 * Read the group file into memory.  This routine serves a dual purpose
 * of reading the active file too!  An exclusion list can be specified
 * when reading the active file...
 */
LIST *read_groups(int fd, LIST *exclusion_list)
{
    LIST *list;
    GROUP *gp;
    char *name;
    int not, exclude;
#ifdef STDIO_READGROUP
    char *cp;
    char lbuf[MAX_LINE_SIZE];
#else
    struct stat sb;
    int length;
#endif

    list = create_list();

#ifdef STDIO_READGROUP
    while (fgets(lbuf, sizeof(lbuf) - 1, fp)) {		/* Load them all up */
	lbuf[strlen(lbuf) - 1] = '\0';	/* Zip trailing \n */

	/* Ignore leading and trailing spaces */
	for (name = lbuf; *name; name++)
	    if (!isspace(*name))
		break;

	for (cp = name; *cp; cp++)
	    if (isspace(*cp)) {
		*cp++ = '\0';
		break;
	    }

	if (name[0] == '!') {
	    name++;
	    not = GUP_EXCLUDE;
	} else if (name[0] == '@') {
	    name++;
	    not = GUP_POISON;
	} else
	    not = GUP_INCLUDE

	/* check that this group is not on the site's exclusion list */
	exclude = FALSE;
	TRAVERSE(exclusion_list, gp) {
	    if (wildmat(name, gp->name))
		exclude = !gp->u.not;
	}

	if (!exclude) {
	    gp = create_group(not, name);
	    add_group(list, gp);	/* Link into list */
	}
    }
#else
    if (fstat(fd, &sb) < 0) {
	logit(L_BOTH, "ERROR: read_groups: could not stat file (%s)",
		strerror(errno));
	return list;
    }
    /* grab ourselves a buffer */
    name = malloc(sb.st_size + 1);
    if (!name)
	gupout(1, "Could not malloc space for groups list");

    /* slurp it in */
    length = read(fd, name, (int) sb.st_size);
    if (length != sb.st_size)
	gupout(1, "Error reading groups file");

    /* terminate the end */
    name[length] = 0;

    /*
     * hurl through the newsgroups, assigning descriptions to active_list
     * groups as we go
     */
    while (*name) {
	char *eoln, *p;

	/*
	 * null-terminate the end of the line, to prevent anything
	 * screaming off through the entire file
	 */
	if ((eoln = strchr(name, '\n')))
	    *eoln = 0;
	else
	    logit(L_LOG, "WARNING: premature end of groups file");

	/* locate end of group name */
	if ((p = strchr(name, ' ')))
	    *p = '\0';			/* found it */

	if (name[0] == '!') {
	    name++;
	    not = GUP_EXCLUDE;
	} else if (name[0] == '@') {
	    name++;
	    not = GUP_POISON;
	} else
	    not = GUP_INCLUDE;

	/* check that this group is not on the site's exclusion list */
	exclude = FALSE;
	TRAVERSE(exclusion_list, gp) {
	    if (wildmat(name, gp->name))
		exclude = !gp->u.not;
	}

	if (!exclude) {
	    gp = create_group(not, name);
	    add_group(list, gp);	/* Link into list */
	}
	/* advance to next line */
	name = eoln + 1;
    }
#endif

    return list;
}

/* Check to see if a specific group is in the subscribed list */
int subscribed(LIST *sub_list, const char *gname)
{
    int unsub = TRUE;
    GROUP *gp;

    TRAVERSE(sub_list, gp) {
	if ((unsub != gp->u.not) && wildmat(gname, gp->name))
	    unsub = gp->u.not;
    }
    return !unsub;
}

LIST *create_list(void)
{
    LIST *l;

    l = malloc(sizeof(LIST));
    if (!l)
	gupout(1, "malloc of LIST failed");
    l->head = l->tail = NULL;
    l->length = 0;
    return l;
}

void add_group(LIST *list, GROUP *group)
{
    if (!list->head)
	list->head = list->tail = group;
    else {
	list->tail->next = group;
	list->tail = group;
    }
    list->length++;
    group->next = NULL;
}

void remove_group(LIST *list, GROUP *group)
{
    GROUP *prev, *tmp;

    prev = NULL;
    for (tmp = list->head; tmp; tmp = tmp->next) {
	if (tmp == group)
	    break;
	prev = tmp;
    }

    if (!tmp) {
	logit(L_BOTH, "ERROR: remove_group can't find group");
	return;
    }
    /* unlink */
    if (!prev)
	list->head = group->next;	/* the first item was deleted */
    else
	prev->next = group->next;

    if (!group->next)
	list->tail = prev;

    list->length--;

    /* don't free() the element, since TRAVERSE() will be doing naughty
       things then (if we're called from within a TRAVERSE, that is) */

    /* if this was a problem, which it isn't, we would put group onto
       a free-list for reclamation */
}

extern GROUP *create_group(int not_flag, const char *name)
{
    GROUP *gp;

    gp = malloc(sizeof(GROUP));
    if (!gp)
	gupout(1, "malloc of GROUP failed");

    gp->next = NULL;
    gp->u.not = not_flag;
    gp->name = xstrdup(name);
    gp->desc = "";

    return gp;
}

void destroy_group(GROUP *gp)
{
    free(gp);
}

char *xstrdup(const char *str)
{
    char *res;

    res = malloc(strlen(str) + 1);
    if (!res)
	gupout(1, "xstrdup of %d failed", strlen(str));
    return strcpy(res, str);
}