-
Notifications
You must be signed in to change notification settings - Fork 1
/
newsrc.c
210 lines (163 loc) · 4.35 KB
/
newsrc.c
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
/*
$NiH: newsrc.c,v 1.7 2002/04/10 16:23:29 wiz Exp $
newsrc.c -- .newsrc handling
Copyright (C) 2002 Dieter Baron and Thomas Klausner
This file is part of cg, a program to assemble and decode binary Usenet
postings. The authors can be contacted at <nih@giga.or.at>
This program 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "util.h"
#include "ranges.h"
#include "globals.h"
extern char *newsrc;
extern int parserc (char *gname, FILE *rc, long lo, long hi, long no_arts);
static void writegrouptorc (FILE *copy, char *compstr);
int
readrc(char *group, long lower, long upper, long no_art)
{
FILE *rc;
if ((rc=fopen(newsrc, "r")) == NULL) {
if (errno != ENOENT)
fprintf(stderr, "%s: couldn't open %s for reading\n", prg,
newsrc);
rcmap = range_init(lower, upper, no_art);
return 0;
}
rcmap = NULL;
parserc(group, rc, lower, upper, no_art);
if (rcmap == NULL)
rcmap = range_init(lower, upper, no_art);
fclose(rc);
return(0);
}
int
writerc(char *group)
{
static int modified = 0;
FILE *rc, *copy;
int bool, found;
char *temp, b[BUFSIZE], *compstr;
if (modified == 0) {
sprintf(b, "%s~", newsrc);
remove(b);
if (link(newsrc, b) < 0 && errno != ENOENT) {
fprintf(stderr, "%s: can't backup newsrc `%s' (left unchanged): "
"%s\n",
prg, newsrc, strerror(errno));
return 1;
}
modified = 1;
}
if ((rc=fopen(newsrc, "r")) == NULL) {
if (errno != ENOENT)
fprintf(stderr, "%s: couldn't open %s for reading\n", prg,
newsrc);
}
temp=(char *)xmalloc(strlen(newsrc)+11);
sprintf(temp, "%s.%d", newsrc, (int)getpid());
if ((copy=fopen(temp, "w")) == NULL) {
fprintf(stderr, "%s: couldn't open %s for writing\n", prg,
temp);
if (rc)
fclose(rc);
return 1;
}
compstr=xmalloc(strlen(group)+2);
sprintf(compstr, "%s:", group);
found=0;
if (rc)
while(!ferror(rc) && fgets(b, BUFSIZE, rc) != NULL) {
/* is this the group that gets rewritten ? */
found+=bool=!strncmp(compstr, b, strlen(compstr));
while (b[strlen(b)-1] != '\n') {
if (!bool)
fputs(b, copy);
if (fgets (b, BUFSIZE, rc) == NULL)
break;
}
if (!bool)
fputs(b, copy);
if ((found == 1) && bool) {
writegrouptorc(copy, compstr);
}
if (ferror(copy)) {
fprintf(stderr, "%s: couldn't write to %s: %s\n",
prg, temp, strerror(errno));
fclose(copy);
fclose(rc);
free(temp);
free(compstr);
unlink(temp);
return(-1);
}
}
if (!found)
writegrouptorc(copy, compstr);
if (ferror(copy)) {
fprintf(stderr, "%s: couldn't write to %s: %s\n",
prg, temp, strerror(errno));
fclose(copy);
if (rc)
fclose(rc);
free(temp);
free(compstr);
unlink(temp);
return(-1);
}
free(compstr);
if (rc && ferror(rc)) {
fprintf(stderr, "%s: couldn't read from %s: %s\n",
prg, newsrc, strerror(errno));
fclose(copy);
fclose(rc);
unlink(temp);
free(temp);
return(-1);
}
if (rc)
fclose(rc);
fclose(copy);
if (rename(temp, newsrc) != 0) {
fprintf(stderr, "%s: couldn't rename %s to %s: %s\n",
prg, temp, newsrc, strerror(errno));
free(temp);
return -1;
}
free(temp);
range_free(rcmap);
return(0);
}
static void
writegrouptorc (FILE *copy, char *compstr)
{
int lower, upper, first;
fprintf(copy, "%s ", compstr);
lower = upper = first = 0;
while (range_get(rcmap, &lower, &upper, 1) == 0) {
if (lower==upper) {
fprintf(copy, "%s%d", (first == 0) ? "" : ",",
lower);
}
else
fprintf(copy, "%s%d-%d", (first == 0) ? "" : ",",
lower, upper);
if (first == 0)
first = 1;
}
putc('\n', copy);
}