home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume30
/
tin
/
part14
/
xindex.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-20
|
3KB
|
158 lines
/*
* Project : NNTP (RFC 977) extension
* Module : xindex.c
* Author : I.Lea
* Created : 07-03-92
* Updated : 22-03-92
* Notes : Add a command to retieve index files from the
* NNTP server so as to save space on the client.
* Ideas borrowed from XTHREAD nntp extension code
* posted by Tim Iverson to alt.sources in mid'91.
* Copyright : (c) Copyright 1992 by Iain Lea
* You may freely copy or redistribute this software,
* so long as there is no profit made from its use, sale
* trade or reproduction. You may not change this copy-
* right notice, and it must be included in any copy made
*/
#include "common.h"
#ifdef XINDEX
#undef DEBUG_XINDEX /* set to define to turn on more debug info */
#define HASH_VALUE 1409 /* mod value for hashing group name */
#ifdef __STDC__
void xindex (int argc, char *argv[]);
static void find_index_file (char *group, char *index_file);
static long hash_groupname (char *group);
#else
void xindex ();
static void find_index_file ();
static long hash_groupname ();
#endif
/*
* Usage: XINDEX GROUP
*
* GROUP Group for which to retrieve index file
*
* This command is NOT documented in RFC977.
*/
void xindex (argc, argv)
int argc;
char *argv[];
{
char line[NNTP_STRLEN];
char group[256];
char index_file[256];
char *cp;
FILE *fp;
/*
* "parse" the argument list
*/
if (argc == 1) {
printf("%d Usage: XINDEX group\r\n", ERR_CMDSYN);
(void) fflush(stdout);
return;
} else {
strncpy (group, argv[1], sizeof (group)-1);
#if defined(SYSLOG) && defined(DEBUG_XINDEX)
syslog(LOG_INFO, "%s xindex %s", hostname, group);
#endif
find_index_file(group, index_file);
if ((fp = fopen(index_file, "r")) == NULL) {
#ifdef SYSLOG
syslog(LOG_INFO, "%s xindex cannot open %s (%s)",
hostname, group, index_file);
#endif
printf("%d XINDEX Cannot open %s\r\n",
ERR_XINDEX, group);
(void) fflush(stdout);
return;
}
printf("%d XINDEX group in index format\r\n", OK_XINDEX);
(void) fflush(stdout);
while (fgets(line, sizeof(line), fp) != NULL) {
if ((cp = index(line, '\n')) != NULL)
*cp = '\0';
putline(line);
}
(void) fclose(fp);
putline(".");
(void) fflush(stdout);
}
}
/*
* Look in <SPOOLDIR>/.index directory for the index file for the
* given group. Hashing the group name gets a number. See if that
* #.1 file exists; if so, read first line. Group we want? If not,
* try #.2. Repeat until no such file or we find the right file.
*/
static void find_index_file (group, index_file)
char *group;
char *index_file;
{
char buf[256], *p;
FILE *fp;
int i = 1;
unsigned long hash;
hash = hash_groupname (group);
while (1) {
sprintf (index_file, "%s/.index/%lu.%d", SPOOLDIR, hash, i);
if ((fp = fopen (index_file, "r")) == NULL) {
return;
}
if (fgets (buf, sizeof (buf), fp) == NULL) {
fclose (fp);
return;
}
fclose (fp);
for (p = buf; *p && *p != '\n'; p++) {
continue;
}
*p = '\0';
if (strcmp (buf, group) == 0) {
return;
}
i++;
}
}
/*
* hash group name for filename of group
*/
static long hash_groupname (group)
char *group;
{
unsigned long hash_value;
unsigned char *ptr = (unsigned char *) group;
hash_value = *ptr++;
while (*ptr)
hash_value = ((hash_value << 1) ^ *ptr++) % HASH_VALUE;
return (hash_value);
}
#endif /* XINDEX */