Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
index.c
Package: wwwoffle-2.5e.tgz [view]
Upload User: seven77cht
Upload Date: 2007-01-04
Package Size: 486k
Code Size: 38k
Category:
Browser Client
Development Platform:
Unix_Linux
- /***************************************
- $Header: /home/amb/wwwoffle/RCS/index.c 2.63 1999/09/06 18:54:11 amb Exp $
- WWWOFFLE - World Wide Web Offline Explorer - Version 2.5.
- Generate an index of the web pages that are cached in the system.
- ******************/ /******************
- Written by Andrew M. Bishop
- This file Copyright 1997,98,99 Andrew M. Bishop
- It may be distributed under the GNU Public License, version 2, or
- any higher version. See section COPYING of the GNU Public license
- for conditions under which this file may be redistributed.
- ***************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <utime.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "wwwoffle.h"
- #include "misc.h"
- #include "proto.h"
- #include "config.h"
- #include "errors.h"
- /*+ A type to contain the information required to sort the files. +*/
- typedef struct _FileIndex
- {
- URL *url; /*+ The URL of the file. +*/
- char *name; /*+ The name of the URL. +*/
- char *host; /*+ The host of the URL. +*/
- char *type; /*+ The type (file extension) of the URL. +*/
- long time; /*+ The time of the file (access or modification as appropriate). +*/
- }
- FileIndex;
- /*+ The method of sorting used. +*/
- static char *sorttype[]={"none" ,
- "mtime" ,
- "atime" ,
- "dated" ,
- "alpha" ,
- "domain",
- "type" ,
- "none" };
- /*+ An enumerated type for the sort modes. +*/
- typedef enum _SortMode
- {
- None, /*+ No sorting, natural order. +*/
- MTime, /*+ Sort by modification time +*/
- ATime, /*+ Sort by access time. +*/
- Dated, /*+ Sort by modification time with separators. +*/
- Alpha, /*+ Sort Alphabetically. +*/
- Domain, /*+ Sort by domain name. +*/
- Type, /*+ Sort by type (file extension). +*/
- NSortModes /*+ The number of sort modes. +*/
- }
- SortMode;
- /*+ The list of files. +*/
- static FileIndex **files=NULL;
- static int nfiles=0;
- /*+ The earliest time that files can appear in the dated list. +*/
- static time_t earliest=0;
- /*+ The current time. +*/
- static time_t now;
- static void IndexRoot(int fd);
- static void IndexProtocol(int fd,char *proto,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void IndexHost(int fd,char *proto,char *host,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void IndexOutgoing(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void IndexMonitor(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void IndexLastTime(int fd,char *name,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void IndexLatest(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt);
- static void add_dir(char *name,SortMode mode);
- static void add_file(char *name,SortMode mode);
- static void dated_separator(int fd,int file,int *lastdays,int *lasthours);
- static int sort_alpha(FileIndex **a,FileIndex **b);
- static int sort_type(FileIndex **a,FileIndex **b);
- static int sort_domain(FileIndex **a,FileIndex **b);
- static int sort_time(FileIndex **a,FileIndex **b);
- /*++++++++++++++++++++++++++++++++++++++
- Generate an index of the pages that are in the cache.
- int fd The file descriptor to write the output to.
- URL *Url The URL that specifies the path to generate the index for.
- ++++++++++++++++++++++++++++++++++++++*/
- void IndexPage(int fd,URL *Url)
- {
- char *newpath;
- char *proto=NULL,*host="";
- SortMode sort=NSortModes,s;
- Protocol *protocol=NULL;
- int outgoing=0,monitor=0,lasttime=0,prevtime=0,latest=0,main=0;
- int delopt=0,refopt=0,monopt=0,allopt=0;
- int i;
- if(!strcmp(Url->path,"/index/url/"))
- {
- URL *indexUrl;
- if(Url->args && (indexUrl=SplitURL(Url->args)))
- {
- char *localhost=GetLocalHost(1);
- char *relocate=(char*)malloc(strlen(localhost)+strlen(indexUrl->proto)+strlen(indexUrl->host)+32);
- sprintf(relocate,"http://%s/index/%s/%s?sort=%srn",localhost,indexUrl->proto,indexUrl->host,sorttype[Alpha]);
- HTMLMessage(fd,301,"WWWOFFLE Index URL Redirect",relocate,"IndexURLRedirect",
- "url",Url->args,
- "link",relocate+7+strlen(localhost),
- NULL);
- FreeURL(indexUrl);
- free(localhost);
- free(relocate);
- }
- else
- HTMLMessage(fd,404,"WWWOFFLE Illegal Index Page",NULL,"IndexIllegal",
- "url",Url->pathp,
- NULL);
- return;
- }
- newpath=(char*)malloc(strlen(Url->path)-6);
- strcpy(newpath,Url->path+7);
- if(*newpath && newpath[strlen(newpath)-1]=='/')
- newpath[strlen(newpath)-1]=0;
- for(i=0;newpath[i];i++)
- if(newpath[i]=='/')
- {
- newpath[i]=0;
- host=newpath+i+1;
- break;
- }
- proto=newpath;
- for(i=0;i<NProtocols;i++)
- if(!strcmp(proto,Protocols[i].name))
- protocol=&Protocols[i];
- outgoing=!strcmp(proto,"outgoing");
- monitor =!strcmp(proto,"monitor");
- lasttime=!strcmp(proto,"lasttime");
- prevtime=!strncmp(proto,"prevtime",8) && atoi(proto+8)>=0 && atoi(proto+8)<=NUM_PREVTIME_DIR;
- latest =!strcmp(proto,"latest");
- main=(!protocol && !outgoing && !monitor && !lasttime && !prevtime && !latest);
- if(Url->args)
- {
- char *p=Url->args,*amp;
- do
- {
- amp=strchr(p,'&');
- if(amp)
- *amp=0;
- if(!strncmp(p,"sort=",5))
- for(s=0;s<NSortModes;s++)
- if(!strcmp(p+5,sorttype[s]))
- {sort=s;break;}
- if(!strcmp(p,"delete"))
- delopt=1;
- if(!strcmp(p,"refresh"))
- refopt=1;
- if(!strcmp(p,"monitor"))
- monopt=1;
- if(!strcmp(p,"all"))
- allopt=1;
- if(amp)
- {p=amp+1;*amp='&';}
- }
- while(amp);
- }
- files=NULL;
- nfiles=0;
- now=time(NULL);
- earliest=0;
- if((*host && (strchr(host,'/') || !strcmp(host,"..") || !strcmp(host,"."))) ||
- (main && Url->path[7]) ||
- ((outgoing || monitor || lasttime || prevtime || latest) && *host))
- HTMLMessage(fd,404,"WWWOFFLE Illegal Index Page",NULL,"IndexIllegal",
- "url",Url->pathp,
- NULL);
- else if(sort==NSortModes && (outgoing || monitor || lasttime || prevtime || protocol))
- {
- char *localhost=GetLocalHost(1);
- char *relocate=(char*)malloc(strlen(localhost)+strlen(Url->path)+strlen(sorttype[0])+16);
- sprintf(relocate,"http://%s%s?sort=%s",localhost,Url->path,sorttype[0]);
- HTMLMessage(fd,301,"WWWOFFLE Index Redirect",relocate,"IndexRedirect",
- "url",Url->pathp,
- "link",relocate+7+strlen(localhost),
- NULL);
- free(relocate);
- free(localhost);
- }
- else
- {
- HTMLMessageHead(fd,200,"WWWOFFLE Index",
- NULL);
- HTMLMessageBody(fd,"Index-Head",
- "type",prevtime?"prevtime":proto,
- "subtype",prevtime?proto+8:host,
- "sort",sorttype[sort],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- if(outgoing)
- IndexOutgoing(fd,sort,delopt,refopt,monopt,allopt);
- else if(monitor)
- IndexMonitor(fd,sort,delopt,refopt,monopt,allopt);
- else if(lasttime || prevtime)
- IndexLastTime(fd,proto,sort,delopt,refopt,monopt,allopt);
- else if(latest)
- IndexLatest(fd,sort,delopt,refopt,monopt,allopt);
- else if(protocol && !*host)
- IndexProtocol(fd,proto,sort,delopt,refopt,monopt,allopt);
- else if(protocol && *host)
- IndexHost(fd,proto,host,sort,delopt,refopt,monopt,allopt);
- else
- IndexRoot(fd);
- HTMLMessageBody(fd,"Index-Tail",
- "type",prevtime?"prevtime":proto,
- "subtype",prevtime?proto+8:host,
- "sort",sorttype[sort],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- }
- free(newpath);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Index the root of the cache.
- int fd The file descriptor to write to.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexRoot(int fd)
- {
- HTMLMessageBody(fd,"IndexRoot-Body",NULL);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Index the hosts for one protocol in the cache.
- int fd The file descriptor to write to.
- char *proto The protocol to index.
- SortMode mode The sort mode to use.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexProtocol(int fd,char *proto,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- DIR *dir;
- struct dirent* ent;
- int i;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- /* Open the spool directory. */
- if(chdir(proto))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not indexed.",proto);
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open spool directory '%s' [%!s]; index failed.",proto);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read spool directory '%s' [%!s]; index failed.",proto);closedir(dir);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip .. */
- /* Get all of the host sub-directories. */
- while((ent=readdir(dir)))
- add_dir(ent->d_name,mode);
- closedir(dir);
- chdir("..");
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha || mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_domain);
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexProtocol-Head",
- "proto",proto,
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexProtocol-Body",
- "proto",proto,
- "host",files[i]->name,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- free(files[i]->name);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nfiles);
- sprintf(notindexed,"%d",0);
- HTMLMessageBody(fd,"IndexProtocol-Tail",
- "proto",proto,
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Create an index of the pages on a host.
- int fd The file descriptor to write to.
- char *proto The protocol to index.
- char *host The name of the subdirectory.
- SortMode mode The sort mode to use.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexHost(int fd,char *proto,char *host,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- DIR *dir;
- struct dirent* ent;
- int i,nindexed=0;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- /* Open the spool subdirectory. */
- if(chdir(proto))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not indexed.",proto);
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- /* Open the spool subdirectory. */
- #if defined(__CYGWIN__)
- if(strchr(host,':'))
- {
- char *colon=strchr(host,':');
- *colon='!';
- }
- #endif
- if(chdir(host))
- {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s]; not indexed.",proto,host);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open directory '%s/%s' [%!s]; not indexed.",proto,host);chdir("../..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read directory '%s/%s' [%!s]; not indexed.",proto,host);closedir(dir);chdir("../..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip .. */
- #if defined(__CYGWIN__)
- if(strchr(host,'!'))
- {
- char *bang=strchr(host,'!');
- *bang=':';
- }
- #endif
- /* Add all of the file names. */
- while((ent=readdir(dir)))
- add_file(ent->d_name,mode);
- closedir(dir);
- chdir("../..");
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha || mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_type);
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexHost-Head",
- "proto",proto,
- "host",host,
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(allopt || !IsNotIndexed(files[i]->url,"host"))
- {
- char *decurl=URLDecode(files[i]->url->pathp,0);
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexHost-Body",
- "url",files[i]->url->name,
- "link",files[i]->url->link,
- "item",decurl,
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- NULL);
- free(decurl);
- nindexed++;
- }
- FreeURL(files[i]->url);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nindexed);
- sprintf(notindexed,"%d",nfiles-nindexed);
- HTMLMessageBody(fd,"IndexHost-Tail",
- "proto",proto,
- "host",host,
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Create an index of the requests that are waiting in the outgoing directory.
- int fd The file descriptor to write to.
- SortMode mode The method to use to sort the names.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexOutgoing(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- DIR *dir;
- struct dirent* ent;
- int i,nindexed=0;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- /* Open the outgoing subdirectory. */
- if(chdir("outgoing"))
- {PrintMessage(Warning,"Cannot change to directory 'outgoing' [%!s]; not indexed.");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open directory 'outgoing' [%!s]; not indexed.");chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read directory 'outgoing' [%!s]; not indexed.");closedir(dir);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip .. */
- /* Add all of the file names. */
- while((ent=readdir(dir)))
- add_file(ent->d_name,mode);
- closedir(dir);
- chdir("..");
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_type);
- else if(mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_domain);
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexOutgoing-Head",
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(allopt || !IsNotIndexed(files[i]->url,"outgoing"))
- {
- char *decurl=URLDecode(files[i]->url->name,0);
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexOutgoing-Body",
- "url",files[i]->url->name,
- "link",files[i]->url->link,
- "item",decurl,
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- NULL);
- free(decurl);
- nindexed++;
- }
- FreeURL(files[i]->url);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nindexed);
- sprintf(notindexed,"%d",nfiles-nindexed);
- HTMLMessageBody(fd,"IndexOutgoing-Tail",
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Create an index of the requests that are in the monitor directory.
- int fd The file descriptor to write to.
- SortMode mode The method to use to sort the names.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexMonitor(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- DIR *dir;
- struct dirent* ent;
- int i,nindexed=0;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- /* Open the monitor subdirectory. */
- if(chdir("monitor"))
- {PrintMessage(Warning,"Cannot change to directory 'monitor' [%!s]; not indexed.");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open directory 'monitor' [%!s]; not indexed.");chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read directory 'monitor' [%!s]; not indexed.");closedir(dir);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip .. */
- /* Add all of the file names. */
- while((ent=readdir(dir)))
- add_file(ent->d_name,mode);
- closedir(dir);
- chdir("..");
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_type);
- else if(mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_domain);
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexMonitor-Head",
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(allopt || !IsNotIndexed(files[i]->url,"monitor"))
- {
- char *decurl=URLDecode(files[i]->url->name,0);
- int last,next;
- char laststr[8],nextstr[8];
- MonitorTimes(files[i]->url,&last,&next);
- if(last>=(31*24))
- sprintf(laststr,"31+");
- else
- sprintf(laststr,"%d:%d",last/24,last%24);
- if(next>=(31*24))
- sprintf(nextstr,"31+");
- else
- sprintf(nextstr,"%d:%d",next/24,next%24);
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexMonitor-Body",
- "url",files[i]->url->name,
- "link",files[i]->url->link,
- "item",decurl,
- "last",laststr,
- "next",nextstr,
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- NULL);
- free(decurl);
- nindexed++;
- }
- FreeURL(files[i]->url);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nindexed);
- sprintf(notindexed,"%d",nfiles-nindexed);
- HTMLMessageBody(fd,"IndexMonitor-Tail",
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Create an index of the pages that were got last time online.
- int fd The file descriptor to write to.
- char *name The name of the directory.
- SortMode mode The method to use to sort the names.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexLastTime(int fd,char *name,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- DIR *dir;
- struct dirent* ent;
- int i,nindexed=0;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- /* Open the lasttime subdirectory. */
- if(chdir(name))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not indexed.",name);
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- dir=opendir(".");
- if(!dir)
- {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; not indexed.",name);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip . */
- if(!ent)
- {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; not indexed.",name);closedir(dir);chdir("..");
- HTMLMessageBody(fd,"IndexError-Body",NULL);return;}
- ent=readdir(dir); /* skip .. */
- /* Add all of the file names. */
- while((ent=readdir(dir)))
- add_file(ent->d_name,mode);
- closedir(dir);
- chdir("..");
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_type);
- else if(mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_domain);
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexLastTime-Head",
- "number",name+8,
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(allopt || !IsNotIndexed(files[i]->url,"latest"))
- {
- char *decurl=URLDecode(files[i]->url->name,0);
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexLastTime-Body",
- "url",files[i]->url->name,
- "link",files[i]->url->link,
- "item",decurl,
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- NULL);
- free(decurl);
- nindexed++;
- }
- FreeURL(files[i]->url);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nindexed);
- sprintf(notindexed,"%d",nfiles-nindexed);
- HTMLMessageBody(fd,"IndexLastTime-Tail",
- "number",name+8,
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Create an index of the latest pages.
- int fd The file descriptor to write to.
- SortMode mode The method to use to sort the names.
- int delopt The option to display the delete button in the index.
- int refopt The option to display the refresh buttons in the index.
- int monopt The option to display the monitor button in the index.
- int allopt The option to show all pages including DontIndex ones.
- ++++++++++++++++++++++++++++++++++++++*/
- static void IndexLatest(int fd,SortMode mode,int delopt,int refopt, int monopt,int allopt)
- {
- int i,p,nindexed=0;
- char total[8],indexed[8],notindexed[8];
- int lastdays=0,lasthours=0;
- earliest=now-IndexLatestDays*24*3600;
- if(IndexLatestDays>0)
- {
- /* Add all of the files in the sub directories. */
- for(p=0;p<NProtocols;p++)
- {
- struct stat buf;
- if(!lstat(Protocols[p].name,&buf) && S_ISDIR(buf.st_mode))
- {
- DIR *dir2;
- struct dirent* ent2;
- if(chdir(Protocols[p].name))
- {PrintMessage(Warning,"Cannot change to directory '%s' [%!s]; not indexed.",Protocols[p].name);continue;}
- /* Open the spool directory. */
- dir2=opendir(".");
- if(!dir2)
- {PrintMessage(Warning,"Cannot open directory '%s' [%!s]; not indexed.",Protocols[p].name);chdir("..");continue;}
- ent2=readdir(dir2); /* skip . */
- if(!ent2)
- {PrintMessage(Warning,"Cannot read directory '%s' [%!s]; index failed.",Protocols[p].name);closedir(dir2);chdir("..");continue;}
- ent2=readdir(dir2); /* skip .. */
- /* Print all of the sub directories. */
- while((ent2=readdir(dir2)))
- {
- struct stat buf2;
- if(lstat(ent2->d_name,&buf2))
- PrintMessage(Inform,"Cannot stat file '%s' [%!s]; race condition?",ent2->d_name);
- else
- if(S_ISDIR(buf2.st_mode) && buf2.st_mtime>earliest)
- {
- DIR *dir3;
- struct dirent* ent3;
- struct utimbuf utbuf;
- if(chdir(ent2->d_name))
- {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s]; not indexed.",Protocols[p].name,ent2->d_name);continue;}
- dir3=opendir(".");
- if(!dir3)
- {PrintMessage(Warning,"Cannot open directory '%s/%s' [%!s]; not indexed.",Protocols[p].name,ent2->d_name);chdir("..");continue;}
- ent3=readdir(dir3); /* skip . */
- if(!ent3)
- {PrintMessage(Warning,"Cannot read directory '%s/%s' [%!s]; not indexed.",Protocols[p].name,ent2->d_name);closedir(dir3);chdir("..");continue;}
- ent3=readdir(dir3); /* skip .. */
- while((ent3=readdir(dir3)))
- add_file(ent3->d_name,mode);
- closedir(dir3);
- chdir("..");
- utbuf.actime=buf2.st_atime;
- utbuf.modtime=buf2.st_mtime;
- utime(ent2->d_name,&utbuf);
- }
- }
- closedir(dir2);
- chdir("..");
- }
- }
- /* Sort the files. */
- if(mode==MTime || mode==ATime || mode==Dated)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_time);
- else if(mode==Alpha)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_alpha);
- else if(mode==Type)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_type);
- else if(mode==Domain)
- qsort(files,nfiles,sizeof(FileIndex*),(int (*)(const void*,const void*))sort_domain);
- }
- /* Output the page. */
- sprintf(total,"%d",nfiles);
- HTMLMessageBody(fd,"IndexLatest-Head",
- "total",total,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- for(i=0;i<nfiles;i++)
- {
- if(allopt || !IsNotIndexed(files[i]->url,"latest"))
- {
- char *decurl=URLDecode(files[i]->url->name,0);
- if(mode==Dated)
- dated_separator(fd,i,&lastdays,&lasthours);
- HTMLMessageBody(fd,"IndexLatest-Body",
- "url",files[i]->url->name,
- "link",files[i]->url->link,
- "item",decurl,
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- NULL);
- free(decurl);
- nindexed++;
- }
- FreeURL(files[i]->url);
- free(files[i]);
- }
- sprintf(indexed ,"%d",nindexed);
- sprintf(notindexed,"%d",nfiles-nindexed);
- HTMLMessageBody(fd,"IndexLatest-Tail",
- "total",total,
- "indexed",indexed,
- "notindexed",notindexed,
- "sort",sorttype[mode],
- "delete" ,delopt?"&delete":"",
- "refresh",refopt?"&refresh":"",
- "monitor",monopt?"&monitor":"",
- "all" ,allopt?"&all":"",
- NULL);
- /* Tidy up and exit */
- if(files)
- free(files);
- files=NULL;
- nfiles=0;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Add a directory to the list.
- char *name The name of the directory.
- SortMode mode The sort mode.
- ++++++++++++++++++++++++++++++++++++++*/
- static void add_dir(char *name,SortMode mode)
- {
- struct stat buf;
- if(stat(name,&buf))
- {PrintMessage(Inform,"Cannot stat directory '%s' [%!s]; race condition?",name);return;}
- else if(S_ISDIR(buf.st_mode))
- {
- if(!(nfiles%16))
- {
- if(!files)
- files=(FileIndex**)malloc(16*sizeof(FileIndex*));
- else
- files=(FileIndex**)realloc(files,(nfiles+16)*sizeof(FileIndex*));
- }
- files[nfiles]=(FileIndex*)malloc(sizeof(FileIndex));
- #if defined(__CYGWIN__)
- if(strchr(name,'!'))
- {
- char *bang=strchr(name,'!');
- *bang=':';
- }
- #endif
- files[nfiles]->name=(char*)malloc(strlen(name)+1);
- strcpy(files[nfiles]->name,name);
- if(mode==Domain)
- files[nfiles]->host=files[nfiles]->name;
- else if(mode==Type)
- files[nfiles]->type="";
- else if(mode==MTime || mode==Dated)
- files[nfiles]->time=buf.st_mtime;
- else if(mode==ATime)
- files[nfiles]->time=buf.st_atime;
- nfiles++;
- }
- }
- /*++++++++++++++++++++++++++++++++++++++
- Add a file to the list.
- char *name The name of the file.
- SortMode mode The sort mode.
- ++++++++++++++++++++++++++++++++++++++*/
- static void add_file(char *name,SortMode mode)
- {
- char *url=NULL;
- struct stat buf;
- if((*name!='D' && *name!='O') || name[strlen(name)-1]=='~')
- return;
- url=FileNameToURL(name);
- if(!url)
- return;
- if(stat(name,&buf))
- {PrintMessage(Inform,"Cannot stat file '%s' [%!s]; race condition?",name);return;}
- else if(S_ISREG(buf.st_mode) && (mode!=Dated || buf.st_mtime>earliest))
- {
- if(!(nfiles%16))
- {
- if(!files)
- files=(FileIndex**)malloc(16*sizeof(FileIndex*));
- else
- files=(FileIndex**)realloc(files,(nfiles+16)*sizeof(FileIndex*));
- }
- files[nfiles]=(FileIndex*)malloc(sizeof(FileIndex));
- files[nfiles]->url=SplitURL(url);
- files[nfiles]->name=files[nfiles]->url->name;
- if(mode==Domain)
- files[nfiles]->host=files[nfiles]->url->host;
- else if(mode==Type)
- {
- char *p=files[nfiles]->url->path+strlen(files[nfiles]->url->path);
- for(;p>=files[nfiles]->url->path;p--)
- if(*p=='.')
- {files[nfiles]->type=p;break;}
- else if(*p=='/')
- {files[nfiles]->type="";break;}
- }
- else if(mode==MTime || mode==Dated)
- files[nfiles]->time=buf.st_mtime;
- else if(mode==ATime)
- files[nfiles]->time=buf.st_atime;
- nfiles++;
- }
- }
- /*++++++++++++++++++++++++++++++++++++++
- Write out a date separator for the dated format if needed.
- int fd The file to write to.
- int file The number of the file in the list.
- int *lastdays The age of the previous file in days.
- int *lasthours The age of the previous file in hours.
- ++++++++++++++++++++++++++++++++++++++*/
- static void dated_separator(int fd,int file,int *lastdays,int *lasthours)
- {
- int days=(now-files[file]->time)/(24*3600),hours=(now-files[file]->time)/3600;
- if(*lastdays<days)
- {
- char daystr[8];
- sprintf(daystr,"%d",days-*lastdays);
- HTMLMessageBody(fd,"IndexSeparator-Body",
- "days",daystr,
- NULL);
- }
- else if(file && *lasthours<(hours-1))
- {
- HTMLMessageBody(fd,"IndexSeparator-Body",
- "days","",
- NULL);
- }
- *lastdays=days;
- *lasthours=hours;
- }
- /*++++++++++++++++++++++++++++++++++++++
- Used to sort the files into alphabetical order.
- int sort_alpha Returns the comparison of the pointers to strings.
- FileIndex **a The first FileIndex.
- FileIndex **b The second FileIndex.
- ++++++++++++++++++++++++++++++++++++++*/
- static int sort_alpha(FileIndex **a,FileIndex **b)
- {
- char *an=(*a)->name;
- char *bn=(*b)->name;
- return(strcmp(an,bn));
- }
- /*++++++++++++++++++++++++++++++++++++++
- Used to sort the files into type (file extension) order.
- int sort_type Returns the comparison of the pointers to strings.
- FileIndex **a The first FileIndex.
- FileIndex **b The second FileIndex.
- ++++++++++++++++++++++++++++++++++++++*/
- static int sort_type(FileIndex **a,FileIndex **b)
- {
- char *an=(*a)->name,*at=(*a)->type;
- char *bn=(*b)->name,*bt=(*b)->type;
- int sort1=strcmp(at,bt);
- if(sort1==0)
- return(strcmp(an,bn));
- else
- return(sort1);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Used to sort the files into domain order.
- int sort_domain Returns the comparison of the pointers to strings.
- FileIndex **a The first FileIndex.
- FileIndex **b The second FileIndex.
- ++++++++++++++++++++++++++++++++++++++*/
- static int sort_domain(FileIndex **a,FileIndex **b)
- {
- char *ap=(*a)->host+strlen((*a)->host);
- char *bp=(*b)->host+strlen((*b)->host);
- int chosen=0;
- do
- {
- ap--;
- bp--;
- while(ap>(*a)->host && *ap!='.' && *ap!='/')
- ap--;
- while(bp>(*b)->host && *bp!='.' && *bp!='/')
- bp--;
- chosen=strcmp(ap,bp);
- }
- while(!chosen);
- return(chosen);
- }
- /*++++++++++++++++++++++++++++++++++++++
- Used to sort the files into time order.
- int sort_time Returns the comparison of the times.
- FileIndex **a The first FileIndex.
- FileIndex **b The second FileIndex.
- ++++++++++++++++++++++++++++++++++++++*/
- static int sort_time(FileIndex **a,FileIndex **b)
- {
- long at=(*a)->time;
- long bt=(*b)->time;
- return(bt-at);
- }