Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: query.c
发信站: 哈工大紫丁香 (2002年06月09日21:31:13 星期天), 站内信件
/*
* NoseyParker, the search engine for FTP archives
* Copyright (C) 1993-96 by Jiri A. Randus
*
* 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.
*
* The author can be reached as follows:
* Internet: <Jiri.Randus@vslib.cz>
* Phone: ++42 48 5227374
* SnailMail: Jiri Randus
* KIN HF TU v Liberci
* Halkova 6
* 46117 Liberec
* Czech Republic
*/
#include "parker.h"
int InitConn(FILE **Server, FILE **Server2)
{
int h;
struct sockaddr_in sa;
char IP[MAX];
unsigned char *p;
struct hostent *host;
alarm(120);
if((h=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0) {
printf("Socket failed\n");
perror("socket");
exit(103);
}
sa.sin_family=AF_INET;
sa.sin_port=htons(PARKERPORT);
if(!(host=gethostbyname(PARKERSERVER))) {
perror("gethostbyname failed");
return(S_ERROR);
}
p=(unsigned char *)*host->h_addr_list;
sprintf(IP,"%d.%d.%d.%d",*p,*(p+1),*(p+2),*(p+3));
sa.sin_addr.s_addr=inet_addr(IP);
if(!(*Server=fdopen(h,"r"))) {perror("Can't open Server"); exit(102);}
if(!(*Server2=fdopen(h,"w"))) {perror("Can't open Server2"); exit(102);}
setbuf(*Server,NULL);
setbuf(*Server2,NULL);
if(connect(h,(struct sockaddr *)&sa, sizeof(sa))<0) {
printf("Connect failed");
perror("connect");
close(h);
exit(101);
}
return(S_OK);
}
void PStatus(char *StatT, int fd)
{
char buffer[MAX];
FILE *Client, *Server, *Server2;
if(fd==-1) {printf("Not a valid file descriptor\n"); return;}
InitConn(&Server,&Server2);
if(!(Client=fdopen(fd,"w"))) {perror("Can't open Client"); exit(102);}
fgets(buffer,MAX,Server);
if(*buffer!='2') {printf("%s\nThe server refuses the connection\n",buffer)
;exit(105);}
fprintf(Server2,"%s\n",StatT);
fflush(Server2);
while(1)
{
fgets(buffer,MAX,Server);
alarm(120);
if(feof(Server) || (*buffer!='2') || (buffer[3]!='-')) break;
if(!fprintf(Client,"%s",&buffer[4])) break;
}
alarm(0);
fclose(Server);
fclose(Server2);
fclose(Client);
}
int PQuery(char *typeS, char *search, int fd, char *hits,
char *restart, char *typesearch)
{
char buffer[MAX];
FILE *Client, *Server, *Server2;
char RestartKey[MAX];
char *ptr;
if(fd==-1) {printf("Not a valid file descriptor\n"); return(S_ERROR);}
InitConn(&Server,&Server2);
if(!(Client=fdopen(fd,"w"))) {perror("Can't open Client"); exit(102);}
fgets(buffer,MAX,Server);
if(*buffer!='2') {printf("%s\nThe server refuses the connection\n",buffer)
;exit(105);}
if(hits && *hits) { /* specify hits */
fprintf(Server2,"hits %s\n",hits);
fflush(Server2);
fgets(buffer,MAX,Server);
}
if(restart && *restart) { /* specify restart */
fprintf(Server2,"restart %s\n",restart);
fflush(Server2);
fgets(buffer,MAX,Server);
}
if(typesearch && *typesearch) { /* specify restart */
fprintf(Server2,"%s\n",typesearch);
fflush(Server2);
fgets(buffer,MAX,Server);
}
*RestartKey='\0';
fprintf(Server2,"%s %s\n",typeS,search);
fflush(Server2);
fgets(buffer,MAX,Server);
if(*buffer!='1')
{printf("%s\nThe server refuses the search \n",buffer);exit(106);}
while(1)
{
fgets(buffer,MAX,Server);
if(feof(Server)) break;
alarm(120);
if(!strncmp(buffer, "257 ", 4)) break;
if(!strncmp(buffer, "258 ", 4)) break;
if(*buffer<' ') continue; /* skip empty lines */
if(!fprintf(Client,"%s",buffer+4)) break;
}
/* was there a restart string specified ? */
if((ptr=strchr(buffer,'"'))!=NULL) { /* Yes, it was */
strcpy(RestartKey,ptr+1);
ptr=RestartKey+strlen(RestartKey)-1;
while((ptr>=RestartKey) && (*ptr!='"')) ptr--;
if(*ptr=='"') *ptr='\0';
fprintf(Client,"@%s\n",RestartKey);
}
fclose(Server);
fclose(Server2);
fclose(Client);
alarm(0);
return(S_OK);
}
int PSkip(char *host)
{
char buffer[MAX];
FILE *Server, *Server2;
int rc;
InitConn(&Server, &Server2);
fgets(buffer,MAX,Server);
if(*buffer!='2') {fclose(Server);fclose(Server2);return(1);}
fprintf(Server2,"topdir %s\n",host);
fflush(Server2);
fgets(buffer,MAX,Server);
if(*buffer!='2') rc=strlen(buffer+4);
else rc=1;
fclose(Server);
fclose(Server2);
return(1);
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.696毫秒