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
|
/* $Id: defdist.c 6135 2003-01-19 01:15:40Z rra $
**
*/
#include "config.h"
#include "clibrary.h"
#include <ctype.h>
#include <errno.h>
#include "inn/innconf.h"
#include "libinn.h"
#include "paths.h"
typedef struct _DDENTRY {
char *Pattern;
char *Value;
int Weight;
} DDENTRY;
struct _DDHANDLE {
int Count;
DDENTRY *Entries;
DDENTRY *Current;
};
typedef struct _DDHANDLE DDHANDLE;
struct _DDHANDLE *
DDstart(FILE *FromServer, FILE *ToServer)
{
DDHANDLE *h;
DDENTRY *ep;
FILE *F;
char buff[BUFSIZ];
char *p;
char *q;
char *path;
int i;
int fd;
char *name = NULL;
/* Open the file. */
path = concatpath(innconf->pathetc, _PATH_DISTPATS);
F = fopen(path, "r");
free(path);
if (F == NULL) {
/* Not available locally; try remotely. */
if (FromServer == NULL || ToServer == NULL)
/* We're probably nnrpd running on the server and the
* file isn't installed. Oh well. */
return NULL;
name = concatpath(innconf->pathtmp, _PATH_TEMPACTIVE);
fd = mkstemp(name);
if (fd < 0)
return NULL;
close(fd);
if ((F = CA_listopen(name, FromServer, ToServer,
"distrib.pats")) == NULL)
return NULL;
}
/* Count lines. */
for (i = 0; fgets(buff, sizeof buff, F) != NULL; i++)
continue;
/* Allocate space for the handle. */
if ((h = xmalloc(sizeof(DDHANDLE))) == NULL) {
i = errno;
fclose(F);
if (name != NULL)
unlink(name);
errno = i;
return NULL;
}
h->Count = 0;
h->Current = NULL;
if (i == 0) {
return NULL ;
} else if ((h->Entries = xmalloc(sizeof(DDENTRY) * i)) == NULL) {
i = errno;
free(h);
fclose(F);
if (name != NULL)
unlink(name);
errno = i;
return NULL;
}
fseeko(F, 0, SEEK_SET);
for (ep = h->Entries; fgets(buff, sizeof buff, F) != NULL; ) {
if ((p = strchr(buff, '\n')) != NULL)
*p = '\0';
if (buff[0] == '\0' || buff[0] == '#')
continue;
if ((p = strchr(buff, ':')) == NULL
|| (q = strchr(p + 1, ':')) == NULL)
continue;
*p++ = '\0';
ep->Weight = atoi(buff);
ep->Pattern = xstrdup(p);
q = strchr(ep->Pattern, ':');
*q++ = '\0';
ep->Value = q;
ep++;
}
h->Count = ep - h->Entries;
fclose(F);
if (name != NULL)
unlink(name);
return h;
}
void
DDcheck(DDHANDLE *h, char *group)
{
DDENTRY *ep;
int i;
int w;
if (h == NULL || group == NULL)
return;
w = h->Current ? h->Current->Weight : -1;
for (ep = h->Entries, i = h->Count; --i >= 0; ep++)
if (ep->Weight > w && uwildmat(group, ep->Pattern)) {
h->Current = ep;
w = ep->Weight;
}
}
char *
DDend(DDHANDLE *h)
{
static char NIL[] = "";
char *p;
int i;
DDENTRY *ep;
if (h == NULL) {
p = NIL;
return xstrdup(p);
}
if (h->Current == NULL)
p = NIL;
else
p = h->Current->Value;
p = xstrdup(p);
for (ep = h->Entries, i = h->Count; --i >= 0; ep++)
free(ep->Pattern);
free(h->Entries);
free(h);
return p;
}
#if defined(TEST)
int
main(int ac, char *av[])
{
struct _DDHANDLE *h;
char *p;
FILE *FromServer;
FILE *ToServer;
char buff[SMBUF];
if (NNTPremoteopen(NNTP_PORT, &FromServer, &ToServer, buff) < 0) {
if ((p = strchr(buff, '\n')) != NULL)
*p = '\0';
if ((p = strchr(buff, '\r')) != NULL)
*p = '\0';
if (buff[0])
fprintf(stderr, "%s\n", buff);
else
perror("Can't connect");
exit(1);
}
if ((h = DDstart(FromServer, ToServer)) == NULL)
perror("Init failed, proceeding anyway");
while ((p = *++av) != NULL)
DDcheck(h, p);
p = DDend(h);
printf(">%s<\n", p);
exit(0);
/* NOTREACHED */
}
#endif /* defined(TEST) */
|