Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: psh.c
发信站: 哈工大紫丁香 (2002年06月09日21:30:56 星期天), 站内信件
/*
* 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 PSH_VERSION "3.01"
#include "parker.h"
#define NewHandler(handle,file) { if(handle!=-1) close(handle); \
handle=open(file,O_RDWR | O_CREAT); \
unlink(file); }
char wrkfile[MAX];
char mailfile[MAX];
char choice[MAX];
char seekfor[MAX];
char mailto[MAX];
char Line[MAX];
int workhandler=-1;
int compressOutput=0;
char *files[][2]={HELLOTXT, CZHELLOTXT,
HELPTXT, CZHELPTXT,
SOLONGTXT, CZSOLONGTXT,
TOOBIGTXT, CZTOOBIGTXT,
TYPETXT, CZTYPETXT,
LOOKFORTXT, CZLOOKFORTXT,
SEEKINGTXT, CZSEEKINGTXT,
CHOICETXT, CZCHOICETXT,
MAILRESTXT, CZMAILRESTXT,
YNTXT, CZYNTXT,
ADDRESSTXT, CZADDRESSTXT,
EHADDRTXT, CZEHADDRTXT,
CANTRESTXT, CZCANTRESTXT,
MAILINGTXT, CZMAILINGTXT,
INACTIVITYTXT, CZINACTIVITYTXT,
HANGUPTXT, CZHANGUPTXT,
MENUTXT, CZMENUTXT
};
int language=1;
char *mailline[]={ MAIL, mailto, NULL};
char *smore[]={ SMORE, SMOREOPT, NULL};
char *env[]={ "TERM=vt100", NULL };
void ReadLine(FILE *f, char *p)
{
while(!feof(f))
{
if((*p=getc(f))=='\n') break;
else p++;
}
*p='\0';
}
void Print2(int fd)
{
pid_t ps;
int j;
if(fd==-1) {printf("Nothing available, cannot open file\n");return;}
lseek(fd,0L,SEEK_SET);
alarm(ALTIME*3);
if((ps=fork())==-1) {printf("Fork failed\n"); return;}
if(!ps) {
close(0);
dup(fd);
close(fd);
execve(SMOREPATH,smore,env);
perror("More exec failed");
exit(1);
}
while(wait(&j)!=ps);
kill(ps,SIGKILL);
close(fd);
}
void Print(char *f)
{
int fd;
if((fd=open(f,O_RDONLY))!=-1) {
Print2(fd);
close(fd);
}
}
void Echo(char *f)
{
FILE *fh;
int c;
char *ptr;
if(fh=fopen(f,"r")) {
ptr=Line;
while(!feof(fh))
{
c=fgetc(fh);
if(feof(fh)||(c=='#')) break;
fputc(c, stdout);
}
fclose(fh);
}
else printf("Can't open '%s'\n",f);
fflush(stdout);
}
void Search(void)
{
ECHO(4);
scanf("%s",choice);
*choice=toupper(*choice);
alarm(0);
switch(*choice)
{
case 'E': ECHO(5);
scanf("%s",seekfor);
ECHO(6);
printf("`%s'...\n",seekfor);
NewHandler(workhandler,wrkfile);
if(workhandler!=-1)
PQuery("egrep",seekfor,dup(workhandler),NULL,NULL,NULL);
Print2(dup(workhandler));
return;
case 'S': ECHO(5);
scanf("%s",seekfor);
ECHO(6);
printf("`%s'...\n",seekfor);
NewHandler(workhandler,wrkfile);
if(workhandler!=-1)
PQuery("substring",seekfor,dup(workhandler),NULL,NULL,NULL);
Print2(dup(workhandler));
return;
case 'W': ECHO(5);
scanf("%s",seekfor);
ECHO(6);
printf("`%s'...\n",seekfor);
NewHandler(workhandler,wrkfile);
if(workhandler!=-1)
PQuery("wildcards",seekfor,dup(workhandler),NULL,NULL,NULL);
Print2(dup(workhandler));
return;
default: ECHO(7);
}
}
void MailTo(void)
{
FILE *fi,*fo;
int h,c;
pid_t child;
h=dup(workhandler);
if(lseek(h,0,SEEK_END)>MAXMAIL) {
close(h);
ECHO(3);
Log("telnet: results NOT mailed because of size",NULL,NULL);
return;
}
lseek(h,0L,SEEK_SET);
*choice='\0';
if(*mailto) {
ECHO(8);
printf("`%s' ? ",mailto);
ECHO(9);
scanf("%s",choice);
*choice=toupper(*choice);
}
if(!(*mailto) || (*choice=='N')) {
ECHO(10);
scanf("%s",mailto);
}
if(!strchr(mailto,'@')) {
ECHO(11);
*mailto='\0';
return;
}
if(!(fi=fdopen(h,"r"))) {
ECHO(12);
return;
}
if(!(fo=fopen(mailfile,"w"))) {printf("Can't open the mail file\n"); retur
n;}
ECHO(13);
Log("telnet: mailing results to %s",mailto,NULL);
fprintf(fo,FROM_LINE);
fprintf(fo,"To: %s\n",mailto);
fprintf(fo,"Subject: Result of your search for `%s'\n\n",seekfor);
fprintf(fo,"*** This is the result of your search in the NoseyParker's dat
abase ***\n");
while(c=getc(fi),!feof(fi)) putc(c,fo);
fprintf(fo,"\n.\n");
fclose(fi);
fclose(fo);
if((child=fork())==-1) {printf("Fork failed\n"); return;}
if(!child) {
setsid();
close(0);
open(mailfile,O_RDONLY);
execve(MAILPATH, mailline, NULL);
perror("Mail exec failed");
exit(1);
}
}
void Alrm(void)
{
printf("\007\n");
ECHO(14);
unlink(wrkfile);
unlink(mailfile);
kill(0,SIGHUP);
exit(102);
}
void Hup(void)
{
unlink(wrkfile);
unlink(mailfile);
ECHO(15);
exit(101);
}
void Interactive(void)
{
int exitcl;
int p[2];
pid_t ps;
ECHO(0);
printf("\nNoseyParker %s, Shell V%s\n\n",VERSION,PSH_VERSION);
exitcl=0;
while(!exitcl)
{
if(chdir(PARKER_HOME))
{printf("Cannot chdir to PARKER_HOME '%s'\n",PARKER_HOME); exit(100);}
ECHO(16);
printf("--> ");
fflush(stdout);
alarm(ALTIME);
scanf("%s",choice);
alarm(ALTIME);
if(feof(stdin)) {exitcl=1; printf("<EOF>\n");}
else switch(atoi(choice))
{
case 1: Print(files[1][language]);
break;
case 2: Search();
break;
case 3: MailTo();
break;
case 4: fflush(stdout);
pipe(p);
if((ps=fork())==0) {close(p[1]);Print2(p[0]);exit(0);}
close(p[0]);
PStatus("status",p[1]);
while(wait(NULL)!=ps);
kill(ps,SIGKILL);
break;
case 5: language=1-language;
ECHO(0);
break;
case 6: exitcl=1;
break;
default: ECHO(7);
}
printf("\n");
}
ECHO(2);
}
void main(void)
{
if(chdir(PARKER_HOME))
{printf("Cannot chdir to PARKER_HOME '%s'\n",PARKER_HOME); exit(100);}
nice(PSH_NICE);
signal(SIGHUP,(void (*)(int))Hup);
signal(SIGINT,(void (*)(int))Hup);
signal(SIGALRM,(void (*)(int))Alrm);
strcpy(wrkfile,tempnam("/usr/tmp","psh"));
strcpy(mailfile,tempnam("/usr/tmp","pshm"));
Interactive();
unlink(wrkfile);
unlink(mailfile);
if(workhandler!=-1) close(workhandler);
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.561毫秒