Back to index

wims  3.65+svn20090927
basic.c
Go to the documentation of this file.
00001 /*    Copyright (C) 2002-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 /* dvi 2 gif driver, tex standard */
00019 
00020 void error(char *s)
00021 {
00022     fprintf(stderr,"%s: %s\n",progname, s);
00023     exit(1);
00024 }
00025 
00026 void *xmalloc(size_t n)
00027 {
00028     void *p;
00029     p=malloc(n);
00030     if(p==NULL) error("Malloc failure.");
00031     return p;
00032 }
00033 
00034 unsigned long int texint(void *bp, int l)
00035 {
00036     unsigned long int o, t;
00037     unsigned char c, *p;
00038     int i;
00039     o=t=0; p=bp;
00040     for(i=0;i<l;i++) {
00041        c=p[i]; t=c; o<<=8; o+=t;
00042     }
00043     return o;
00044 }
00045 
00046 long int texintsigned(void *bp, int l)
00047 {
00048     long int o, t;
00049     signed char c, *p;
00050     int i;
00051     if(l<=0) return 0;
00052     p=bp; o=*p;
00053     for(i=1;i<l;i++) {
00054        c=p[i]; t=c; o<<=8; o|=t&255;
00055     }
00056     return o;
00057 }
00058 
00059        /* Strips leading spaces */
00060 char *find_word_start(char *p)
00061 {
00062     int i;
00063     for(i=0; isspace(*p); p++,i++);
00064     return p;
00065 }
00066 
00067        /* Points to the end of the word */
00068 char *find_word_end(char *p)
00069 {
00070     int i;
00071     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
00072     return p;
00073 }
00074 
00075        /* searches a list. Returns index if found, -1 if nomatch. 
00076         * Uses binary search, list must be sorted. */
00077 int search_list(void *list, int items, size_t item_size, const char *str)
00078 {
00079     int i1,i2,j,k;
00080     char **p;
00081     char c;
00082     
00083     if(items<=0) return -1;
00084     j=0; c=*str;
00085     p=list;
00086     k=**p-c; if(k==0) k=strcmp(*p,str);
00087     if(k==0) return k; if(k>0) return -1;
00088     p=list+(items-1)*item_size; 
00089     k=**p-c; if(k==0) k=strcmp(*p,str);
00090     if(k==0) return items-1; if(k<0) return -1;
00091     for(i1=0,i2=items-1;i2>i1+1;) {
00092        j=i1+(i2-i1)/2;
00093        p=list+(j*item_size);
00094        k=**p-c; if(k==0) k=strcmp(*p,str);
00095        if(k==0) return j;
00096        if(k>0) {i2=j; continue;}
00097        if(k<0) {i1=j; continue;}   
00098     }
00099     return -1;
00100 }
00101 
00102        /* get the content of a file, and store in buf. */
00103 int getfile(char *fname, unsigned char **buf)
00104 {
00105     FILE *f;
00106     long l, l2;
00107     
00108     f=fopen(fname,"r"); if(f==NULL) return -1;
00109     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
00110     if(l>FILE_LENGTH_LIMIT || l<=0) {
00111        fclose(f); return -1;
00112     }
00113     *buf=xmalloc(l+16);
00114     l2=fread(*buf,1,l,f); fclose(f);
00115     if(l!=l2) {
00116        free(*buf); return -1;
00117     }
00118     return l;
00119 }
00120 
00121 int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
00122 {
00123     pid_t pid;
00124     int status;
00125 
00126     fflush(NULL);    /* flush all output streams before forking
00127                       * otherwise they will be doubled */
00128     pid=fork(); if(pid==-1) return -1;
00129     if(!pid) {       /* child */
00130        if(inf!=NULL) freopen(inf,"r",stdin);
00131        if(outf!=NULL) freopen(outf,"w",stdout);
00132        if(errf!=NULL) freopen(errf,"w",stderr);
00133        if(wrapexec) {
00134            setreuid(getuid(),getuid());setregid(getgid(),getgid());
00135        }
00136        if(strchr(cmdf,'/')) execv(cmdf,arg);
00137        else execvp(cmdf,arg);
00138        exit(127);
00139     }
00140     else {    /* parent */
00141        status=0; waitpid(pid,&status,0);
00142        return WEXITSTATUS(status);
00143     }
00144 }
00145 
00146        /* system(), but with variable parms
00147         * Uses sh to execute command. */
00148 int call_sh(char *s,...)
00149 {
00150     va_list vp;
00151     char buf[MAX_LINELEN+1];
00152     char *parmbuf[4];
00153     int t;
00154 
00155     va_start(vp,s);
00156     vsnprintf(buf,sizeof(buf),s,vp);
00157     va_end(vp);
00158     parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
00159     wrapexec=0;
00160     t=execredirected("sh",NULL,NULL,NULL,parmbuf);
00161     sync(); return t;
00162 }
00163 
00164        /* recursively generate a directory structure */
00165 void mkdirs(char *s)
00166 {
00167     struct stat st;
00168     char *buf;
00169     if(stat(s,&st)==-1) {
00170        if(strrchr(s,'/')!=NULL) {
00171            buf=xmalloc(strlen(s)+1);
00172            strcpy(buf,s); *strrchr(buf,'/')=0;
00173            mkdirs(buf); free(buf);
00174        }
00175        mkdir(s,-1);
00176     }
00177 }
00178