Back to index

wims  3.65+svn20090927
ftype.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* Check type of a file */
00019 
00020 /*************** Customization: change values hereafter ****************/
00021 
00022        /* limit of data buffers */
00023 #define buflim 1024*1024*16
00024 
00025 /***************** Nothing should need change hereafter *****************/
00026 
00027 #include "../wims.h"
00028 
00029 char filename[1024]="";
00030 char *filebuf;
00031 int filelen=0;
00032 
00033 void *xmalloc(size_t n)
00034 {
00035     void *p;
00036     p=malloc(n);
00037     if(p==NULL) exit(1);
00038     return p;
00039 }
00040 
00041        /* get the file */
00042 void prepare_file(void)
00043 {
00044     FILE *f;
00045     long int flen;
00046 
00047     filelen=0;
00048     f=fopen(filename,"r"); if(f==NULL) return;
00049     fseek(f,0,SEEK_END);flen=ftell(f); fseek(f,0,SEEK_SET);
00050     if(flen>buflim) return;
00051     filebuf=xmalloc(2*flen+1024);flen=fread(filebuf,1,flen,f);
00052     fclose(f);
00053     if(flen>0 && flen<buflim) filebuf[flen]=0; else flen=0;
00054     filelen=flen;
00055 }
00056 
00057 int main(int argc, char *argv[])
00058 {
00059     char *ftype="text";
00060     char *p;
00061     char *mod, *unt;
00062 
00063     unt=getenv("untrust");
00064     if(unt!=NULL && strcasecmp(unt,"yes")==0) return 1;
00065     mod=getenv("w_module");
00066     if(mod==NULL) p=argv[1]; else p=getenv("wims_exec_parm");
00067     if(p==NULL || *p==0) return 1;
00068     snprintf(filename,sizeof(filename)-128,"%s",p);
00069     prepare_file();
00070     for(p=filebuf;p<filebuf+filelen;p++) {
00071        if((*p>=0 && *p<=6) || (127&*p)<=1) 
00072            {ftype="binary"; goto fin;}
00073     }
00074     for(p=strchr(filebuf,'<'); p!=NULL; p=strchr(p,'<')) {
00075        p++;
00076        if((strncasecmp(p,"body",4)==0 && !isalnum(*(p+4))) ||
00077           (strncasecmp(p,"html",4)==0 && !isalnum(*(p+4))) ||
00078           (strncasecmp(p,"img",3)==0 && isspace(*(p+3))) ||
00079           strncasecmp(p,"p>",2)==0 ||
00080           strncasecmp(p,"/a>",3)==0) {
00081            ftype="html"; goto fin;
00082        }
00083     }
00084     if(strstr(filebuf,"\\begin{")!=NULL ||
00085        strstr(filebuf,"\\end{")!=NULL ||
00086        (strchr(filebuf,'$')!=NULL && strchr(filebuf,'\\')!=NULL &&
00087        strchr(filebuf,'{')!=NULL)) {
00088        ftype="latex"; goto fin;
00089     }
00090     fin: printf("%s",ftype);
00091     return 0;
00092 }
00093