home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 1999 June
/
PCWorld_1999-06_cd.bin
/
Komunik
/
Offline
/
RFAT
/
Rfat.exe
/
RFAT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-20
|
10KB
|
364 lines
#include <stdlib.h>
#include <stdio.h>
#include <alloc.h>
#include <io.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#define INPUTNAME "fat.db"
#define URL_OFFSET 8
#define DOS_OFFSET 33
#define TYPE_OFFSET 71
#define strncpy0(dst,src,c) strncpy(dst,src,c);*((dst)+(c))=0;
typedef struct { char *sch,*loc,*pth,*fil,*prt;} Turl;
typedef struct Tpol *Ppol;
struct Tpol{ char *url,*dos;
char HTML;
Ppol nxt;};
Ppol first=NULL;
Turl urla;
int searchfor (char *what,char *from,char **to)
{
int i;
*to=from;
do {
for (i=0;i<=strlen(what);i++) if (**to==what[i]) return(**to);
(*to)++;
}while (1);
}
void urlsplit (char *urls,Turl *surl)
{
char *pos,*urle;
int zn;
memset(surl,0,sizeof(*surl));
zn=searchfor (":/#",urls,&urle);
if (zn==':') {
surl->sch=(char*)malloc(urle-urls+1);
strncpy0(surl->sch,urls,urle-urls);
urls=urle+1;
zn=searchfor("/#",urls,&urle);
}
if (zn=='/' && (*(urle+1)=='/')) {
urls=urle+2;
zn=searchfor ("/#",urls,&urle);
surl->loc=(char*)malloc (urle-urls+1);
strncpy0 (surl->loc,urls,urle-urls);
urls=urle;
}
urle=urls;pos=NULL;
while (*urle) if (*urle++=='/') pos=urle-1;
if (pos!=NULL) {
surl->pth=(char*)malloc (pos-urls+2);
strncpy0 (surl->pth,urls,pos-urls+1);
urls=pos+1;
}
urle=urls;
while (*urle && *urle!='#') urle++;
if (urle-urls) {
surl->fil=(char*)malloc (urle-urls+1);strncpy0 (surl->fil,urls,urle-urls);
}
if (*urle=='#') surl->prt=strdup (urle+1);
return;
}
Turl urls;
void Rename (char *url, char *dos)
{ char name[20]={0},c;
Turl urlx;
int i;
urlsplit (url,&urlx);
for (i=0;urlx.fil[i] && urlx.fil[i]!='.' && i<8;i++) name[i]=urlx.fil[i];
if (urlx.fil[i]) {
for (int ii=strlen(urlx.fil);urlx.fil[ii]!='.' && ii;ii--);
if (ii) strncpy(name+i,urlx.fil+ii,4);
}
for (c='0';rename(dos,name) && c<='9';c++) name[0]=c;
if (c<='9') strcpy (dos,name);
}
void addpol (char *url,char *dos,char *typ)
{ Ppol p;
char name[50];
if ( (p=(Tpol*)malloc(sizeof(*p))) ==NULL) {
printf ("Not enough memory!\n");
exit(1);
}
strcpy (name,dos);
Rename (url,name);
p->url=strdup(url);
p->dos=strdup(name);
p->HTML=!strcmpi("text/html",typ);
// printf ("%-12s %s\n",p->dos,p->url);
p->nxt=first;
first=p;
}
void readblock (char *name)
{ FILE *f;
char ldc[4]={'|','/','-','\\'};
unsigned char *buf;
typedef struct {
unsigned int URL,DOS;} Tblck;
Tblck *blck;
int i,cnt;
printf ("Reading %s ...",name);
if ((f=fopen (name,"rb"))==NULL) {
printf ("File %s not found\n",name);
exit (1);
}
fseek (f,0x1000,SEEK_SET);
buf=(unsigned char*)malloc (0x1000);
while (fread (buf,0x1000,1,f)) {
if (*buf>200) continue;
cnt=*buf/2;
// if (*buf%2!=0) {printf ("!!!");};
blck=(Tblck*)(buf+2);
for (i=0;i<cnt;i++) {
if (!access(buf+blck[i].DOS+DOS_OFFSET,0x00))
addpol (buf+blck[i].URL+URL_OFFSET,buf+blck[i].DOS+DOS_OFFSET,buf+blck[i].DOS+TYPE_OFFSET+strlen(buf+blck[i].DOS+DOS_OFFSET));
putchar (ldc[i%4]);putchar (0x8);
}
}
fclose (f);
printf ("done.\n");
}
Turl url;
FILE *fi,*fo,*fm;
void list ()
{ Ppol p=first;
while (p!=NULL) {
fprintf (fm,"%s %s\n",p->dos,p->url);
p=p->nxt;
}
fprintf (fm,"\n");
}
enum Tlem {VYK,EQ,STR,STP,ATR} lem;
char lematr[200];
char symb;
void readchar (int cpy)
{ if (cpy) fputc(symb,fo);
symb=fgetc(fi);
}
enum Tlem la (int cpy)
{
int i;
while (symb==' '||symb=='\n') readchar(1);
switch (symb) {
case '=':readchar(1);return EQ;
case '>':fputc('>',fo);case -1:return STP;
case '!':fputc('!',fo);return VYK;
case '"':i=0;
readchar(cpy);
while (symb!=-1&&symb!='"') {
lematr[i++]=symb;
readchar(cpy);
}
lematr[i]=0;
readchar(cpy);
return STR;
default:i=0;
while (symb!=-1&&symb!='>'&&symb!='='&&symb!=' '&&symb!='\n') {
lematr[i++]=symb;
readchar(cpy);
}
lematr[i]=0;
if (i>200) sound (1000);
return ATR;
}
}
void freeurl (Turl x)
{
if (x.sch!=NULL) free (x.sch);
if (x.loc!=NULL) free (x.loc);
if (x.pth!=NULL) free (x.pth);
if (x.fil!=NULL) free (x.fil);
if (x.prt!=NULL) free (x.prt);
}
void makepth (char *a, char *r,char *ret)
{
char tmp[200],*f,*fs;
strcpy(tmp,a);
strcat (tmp,r);
f=tmp;
while (*f) {
if (!strncmp(f,"/./",3)) strcpy (f,f+2);
f++;
}
while ((f=strstr(tmp,"/../"))!=NULL) {
fs=f-1;
while (fs>=tmp&&*fs!='/') fs--;
if (fs<tmp) break;
strcpy (fs,f+3);
f=fs;
}
strcpy (ret,tmp);
}
char * findurl (Turl urla,Turl urlr,char *dos)
{
Ppol p=first;
char tmp [200],tmp2[200];
// printf ("pamet:%u\n",coreleft());
if (urlr.sch==NULL) urlr.sch=strdup(urla.sch);
if (urlr.loc==NULL) {
urlr.loc=strdup(urla.loc);
if (urlr.pth==NULL) {
urlr.pth=strdup(urla.pth);
if (urlr.fil==NULL) urlr.fil=strdup(urla.fil);
} else if (urlr.pth[0]!='/') {
strcpy(tmp,urlr.pth);
if (urlr.fil!=NULL) strcat (strcat (tmp,urlr.fil),"/");
makepth(urla.pth,tmp,tmp2);
urlr.pth=(char*)realloc(urlr.pth,strlen(tmp2)+1);
strcpy (urlr.pth,tmp2);
*urlr.fil=0;
}
}
strcat (strcpy (tmp,urlr.sch),"://");
if (urlr.loc!=NULL) strcat (tmp,urlr.loc);
if (urlr.pth!=NULL) strcat (tmp,urlr.pth);
if (urlr.fil!=NULL) strcat (tmp,urlr.fil);
strcpy (tmp2,tmp);
if (tmp[strlen(tmp2)-1]!='/') strcat (tmp2,"/"); else tmp2[strlen(tmp2)-1]=0;
// printf ("\n Looking for:%s\n%s\n",tmp,tmp2);
while (p!=NULL) {
if (!strcmp(tmp,p->url)) break;
if (!strcmp(tmp2,p->url)) break;
p=p->nxt;
}
if (p!=NULL){
// printf ("Found %s\n",p->url);
strcpy (dos,p->dos);
if (urlr.prt!=NULL) strcat (strcat (dos,"#"),urlr.prt);
freeurl (urlr);
return dos;
} else {
fprintf (fm,"NF: %s %s\n",tmp,tmp2);
freeurl (urlr);
return NULL;
}
}
void searchforatr (char *atr)
{
char dosnm[200];
Turl urlr;
while (lem!=STP)
if ((lem=la(1))==ATR&& !strcmpi(lematr,atr))
if ((lem=la(1))==EQ) {lem=la(0);
if (lem==STR || lem==ATR) {
urlsplit (lematr,&urlr);
if (findurl (urla,urlr,dosnm)!=NULL) {
printf ("y");
fprintf (fo,"\"%s\"",dosnm);
} else {
printf ("N");
fprintf(fo,"\"%s\"",lematr);
}
}
}
}
void changelinks ()
{
Ppol p=first;
int c;
char dosnm[50];
mkdir ("NEW");
while (p!=NULL) {
if (p->HTML) {
printf ("DOS: %s\nURL: %s\n",p->dos,p->url);
fi=fopen (p->dos,"r");
strcat (strcpy (dosnm,"NEW\\"),p->dos);
fo=fopen (dosnm,"w");
urlsplit (p->url,&urla);
while ( (c=fgetc(fi)) !=-1) {
if (c=='<') {
fputc(c,fo);
symb=fgetc(fi);
lem=la(1);
switch (lem) {case VYK:do fputc(c=fgetc(fi),fo);
while (c!='>');
break;
case ATR:
if (!strcmpi(lematr,"BASE")) {
while (lem!=STP)
if ((lem=la(1))==ATR&& !strcmpi(lematr,"HREF"))
if ((lem=la(1))==EQ) {lem=la(0);
if (lem==STR || lem==ATR) {
freeurl(urla);
urlsplit (lematr,&urla);
fprintf (fo,"\"/.\"");
}
}
} else if (!strcmpi(lematr,"A"))
searchforatr ("HREF");
else if (!strcmpi(lematr,"IMG"))
searchforatr ("SRC");
else if (!strcmpi(lematr,"BODY"))
searchforatr ("BACKGROUND");
else if (!strcmpi(lematr,"AREA"))
searchforatr ("HREF");
else if (!strcmpi(lematr,"FRAME"))
searchforatr ("SRC");
else while (lem!=STP) lem=la(1);
}
} else fputc (c,fo);
}
fclose (fi);
fclose (fo);
freeurl(urla);
printf ("\n");
}
p=p->nxt;
}
}
void main (int argc, char *argv[])
{
printf (" RFAT 1.0ß 1997\n");
/*
urlsplit (argv[1],&url);
if (url.sch!=NULL) printf ("SCH: %s\n",url.sch);
if (url.loc!=NULL) printf ("LOC: %s\n",url.loc);
if (url.pth!=NULL) printf ("PTH: %s\n",url.pth);
if (url.fil!=NULL) printf ("FIL: %s\n",url.fil);
if (url.prt!=NULL) printf ("PRT: %s\n",url.prt);
exit(1);
*/
if (argc==1) readblock (INPUTNAME); else
readblock (argv[1]);
printf ("Free mem :%u\n",coreleft());
// getch();
if ((fm=fopen ("msg.txt","w"))==NULL) {
printf ("Can't open MSG file\n");
exit (1);
}
list ();
changelinks ();
printf ("\Free mem :%u\n",coreleft());
fclose (fm);
}