Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: gopherd.c
发信站: 哈工大紫丁香 (2002年06月09日21:26:08 星期天), 站内信件
/*
* 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
*/
#define PARKERGD_VERSION "1.1"
#include "parker.h"
char Command[MAX];
char Command2[MAX];
char type;
char *typeS;
char *whichindex;
char *search;
/*
Files with these extensions will be type 0 Gopher items (text files)
Everything else is type 9 (binary)
*/
char *texts[]={ ".txt", "README", "READ.ME", "read.me", "INSTALL", "install"
,
".lst", ".lsm", "Makefile", ".FAQ", ".faq", ".list", ".c", ".
h",
".sh", ".nfo", ".cc", ".cpp", ".readme", ".Notes", ".html",
".shtml", ".patch", ".doc", ".ascii", ".bat",
NULL
};
void Menu(void)
{
printf("0NoseyParker Status\t0/status\t%s\t1996\r\n",SERVER);
printf("7NoseyParker Substring Search\t7/substring/files\t%s\t1996\r\n",SE
RVER);
printf("7NoseyParker Wildcards Search\t7/wildcards/files\t%s\t1996\r\n",SE
RVER);
printf("7NoseyParker Egrep Search (much slower!)\t7/egrep\t%s\t1996\r\n",S
ERVER);
printf(".\r\n");
}
void main(void)
{
char *ptr, *ptr2;
int fd[2];
FILE *serv;
pid_t child;
int i;
alarm(CGIALARM);
dup2(0,1);
dup2(0,2);
fgets(Command,MAX,stdin);
if(feof(stdin)) exit(0); /* Input closed prematurely */
if(strlen(Command)<5) /* Generate the top menu */
{
Menu();
return;
}
if(!(typeS=strchr(Command,'/'))) exit(0);
typeS++;
/* Status report */
if(!strncmp(typeS,"status",6)) {PStatus(typeS,1); exit(0);}
if((ptr2=strchr(typeS,'/'))!=NULL) { /* index explicitely quoted */
*ptr2++='\0';
whichindex=ptr=ptr2;
}
else { /* host search is default */
whichindex="hosts";
ptr=typeS;
}
if(!(ptr2=strchr(ptr,'\t')) &&
!(ptr2=strchr(ptr,'/'))) exit(0);
*ptr2++='\0';
search=ptr2;
/* Cut off possible Gopher+ extensions */
if((ptr2=strchr(ptr2,'\t'))!=NULL) *ptr2='\0';
pipe(fd);
child=fork();
if(child==-1) {perror("fork");exit(0);}
if(!child) {
close(fd[0]);
PQuery(typeS,search,fd[1],NULL,NULL,whichindex);
close(fd[1]);
exit(0);
}
close(fd[1]);
serv=fdopen(fd[0],"r");
if(!serv) {perror("fdopen"); exit(0);}
while(!feof(serv))
{
fgets(Command,MAX,serv);
if(feof(serv)) break;
if((ptr=strchr(Command,'\n'))!=NULL) *ptr='\0';
strcpy(Command2,Command);
if((ptr=strchr(Command,':'))!=NULL) *ptr='@';
if((ptr=strchr(Command,' '))!=NULL) *ptr='\0';
if(*Command!='*') /* host search */
{
if(Command[strlen(Command)-1]=='/')
{
type='1';
Command2[strlen(Command2)-1]='\0';
}
else type='9';
for(i=0;texts[i];i++)
if(!strncmp(&Command[strlen(Command)-strlen(texts[i])],texts[i],
strlen(texts[i]))) {type='0';break;}
printf("%c%s\tftp:%s\t%s\t%s\n", type, Command2, Command,
GO4GW_FTP, GO4GW_FTP_PORT);
}
else /* files search */
{
strcpy(Command2, Command2+2);
ptr=Command;
while((ptr2=strchr(ptr,'/'))!=NULL)
if(*(ptr2+1)>=' ') ptr=ptr2+1; else break;
strcpy(Command, ptr);
printf("1%s\t/wildcards/hosts/%s\t%s\t1996\n", Command2, Command,
SERVER);
}
}
fclose(serv);
printf(".\n");
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.537毫秒