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
|
#include "config.h"
#include "utils.h"
#include <stdio.h>
#include <errno.h>
#include <X11/Intrinsic.h>
#include "mesg.h"
#include "mesg_strings.h"
#include "error_hnds.h"
#include "server.h"
#include "resources.h"
#include "news.h"
#include "activecache.h"
#include "cache.h"
void active_cache_read(filename)
char *filename;
{
FILE *input;
static char *line = 0, *ptr;
static int line_size;
int ret, len;
if (! (input = fopen(filename, "r"))) {
if (errno != ENOENT)
mesgPane(XRN_SERIOUS, 0, CANT_OPEN_FILE_MSG, filename, errmsg(errno));
return;
}
if (! line) {
line_size = 80; /* arbitrary starting value */
line = XtMalloc(line_size);
ptr = line;
}
while (fgets(ptr, line_size - (ptr - line), input)) {
if (ptr[(len = strlen(ptr))-1] != '\n') {
if ((len + (ptr - line)) == (line_size - 1)) {
len += ptr - line;
line_size *= 2;
line = XtRealloc(line, line_size);
ptr = line + len;
}
else {
ptr += len;
}
continue;
}
ptr = line;
if (*line == CACHE_ACTIVE_CHAR) {
switch (ret = parse_active_line(line + 1, TRUE, 0)) {
case ACTIVE_IGNORED:
case ACTIVE_NEW:
case ACTIVE_OLD:
break;
case ACTIVE_BOGUS:
mesgPane(XRN_SERIOUS, 0, BOGUS_ACTIVE_CACHE_MSG, line);
break;
default:
mesgPane(XRN_SERIOUS, 0, UNKNOWN_FUNC_RESPONSE_MSG, ret,
"parse_active_line", "active_cache_read");
break;
}
}
}
(void) fclose(input);
}
/*
Write the active cache based on the newsgroup array. Ignored
newsgroups are not written. Returns 0 on success, 1 on failure.
*/
#define WRITE_LINE { \
if (fputs(line, output) == EOF) { \
mesgPane(XRN_SERIOUS, 0, ERROR_WRITING_SAVE_FILE_MSG, tmpfile, errmsg(errno)); \
(void) fclose(input); \
(void) fclose(output); \
XtFree(tmpfile); \
return -1; \
} \
}
int active_cache_write(
_ANSIDECL(char *, filename),
_ANSIDECL(struct newsgroup **, Newsrc),
_ANSIDECL(ng_num, num_groups),
_ANSIDECL(Boolean, write_entries)
)
_KNRDECL(char *, filename)
_KNRDECL(struct newsgroup **, Newsrc)
_KNRDECL(ng_num, num_groups)
_KNRDECL(Boolean, write_entries)
{
FILE *input, *output;
char *tmpfile;
char last_chopped = 0, last_active = 0;
char line[BUFSIZ];
ng_num i;
tmpfile = utTempFile(filename);
if (! (output = fopen(tmpfile, "w"))) {
mesgPane(XRN_SERIOUS, 0, CANT_OPEN_TEMP_MSG, tmpfile, errmsg(errno));
XtFree(tmpfile);
return -1;
}
if ((input = fopen(filename, "r"))) {
while (fgets(line, sizeof(line), input)) {
if (line[strlen(line) - 1] != '\n') {
if (last_chopped) {
if (! last_active) {
WRITE_LINE;
}
}
else {
if (*line == CACHE_ACTIVE_CHAR) {
last_active = 1;
}
else {
WRITE_LINE;
}
last_chopped = 1;
}
continue;
}
if (*line != CACHE_ACTIVE_CHAR)
WRITE_LINE;
}
(void) fclose(input);
}
else {
if (errno != ENOENT)
mesgPane(XRN_SERIOUS, 0, CANT_OPEN_FILE_MSG, filename, errmsg(errno));
}
if (write_entries) {
for (i = 0; i < num_groups; i++) {
(void) fprintf(output, "%c%s\n", CACHE_ACTIVE_CHAR,
unparse_active_line(Newsrc[i]));
if (ferror(output)) {
mesgPane(XRN_SERIOUS, 0, ERROR_WRITING_SAVE_FILE_MSG, tmpfile,
errmsg(errno));
(void) fclose(output);
XtFree(tmpfile);
return -1;
}
}
}
if (fclose(output) == EOF) {
mesgPane(XRN_SERIOUS, 0, ERROR_WRITING_SAVE_FILE_MSG, tmpfile,
errmsg(errno));
XtFree(tmpfile);
return -1;
}
if (rename(tmpfile, filename)) {
mesgPane(XRN_SERIOUS, 0, ERROR_RENAMING_MSG, tmpfile, filename,
errmsg(errno));
XtFree(tmpfile);
return -1;
}
XtFree(tmpfile);
return 0;
}
#undef WRITE_LINE
|