Subversion Repositories wimsdev

Rev

Rev 3718 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* dvi 2 gif driver, tex standard */
  19.  
  20. void error(char *s)
  21. {
  22.     fprintf(stderr,"%s: %s\n",progname, s);
  23.     exit(1);
  24. }
  25.  
  26. void *xmalloc(size_t n)
  27. {
  28.     void *p;
  29.     p=malloc(n);
  30.     if(p==NULL) error("Malloc failure.");
  31.     return p;
  32. }
  33.  
  34. unsigned long int texint(void *bp, int l)
  35. {
  36.     unsigned long int o, t;
  37.     unsigned char c, *p;
  38.     int i;
  39.     o=t=0; p=bp;
  40.     for(i=0;i<l;i++) {
  41.         c=p[i]; t=c; o<<=8; o+=t;
  42.     }
  43.     return o;
  44. }
  45.  
  46. long int texintsigned(void *bp, int l)
  47. {
  48.     long int o, t;
  49.     signed char c, *p;
  50.     int i;
  51.     if(l<=0) return 0;
  52.     p=bp; o=*p;
  53.     for(i=1;i<l;i++) {
  54.         c=p[i]; t=c; o<<=8; o|=t&255;
  55.     }
  56.     return o;
  57. }
  58.  
  59.         /* Strips leading spaces */
  60. char *find_word_start(char *p)
  61. {
  62.     int i;
  63.     for(i=0; isspace(*p); p++,i++);
  64.     return p;
  65. }
  66.  
  67.         /* Points to the end of the word */
  68. char *find_word_end(char *p)
  69. {
  70.     int i;
  71.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  72.     return p;
  73. }
  74.  
  75.         /* searches a list. Returns index if found, -1 if nomatch.
  76.          * Uses binary search, list must be sorted. */
  77. int search_list(void *list, int items, size_t item_size, const char *str)
  78. {
  79.     int i1,i2,j,k;
  80.     char **p;
  81.     char c;
  82.    
  83.     if(items<=0) return -1;
  84.     j=0; c=*str;
  85.     p=list;
  86.     k=**p-c; if(k==0) k=strcmp(*p,str);
  87.     if(k==0) return k; if(k>0) return -1;
  88.     p=list+(items-1)*item_size;
  89.     k=**p-c; if(k==0) k=strcmp(*p,str);
  90.     if(k==0) return items-1; if(k<0) return -1;
  91.     for(i1=0,i2=items-1;i2>i1+1;) {
  92.         j=i1+(i2-i1)/2;
  93.         p=list+(j*item_size);
  94.         k=**p-c; if(k==0) k=strcmp(*p,str);
  95.         if(k==0) return j;
  96.         if(k>0) {i2=j; continue;}
  97.         if(k<0) {i1=j; continue;}      
  98.     }
  99.     return -1;
  100. }
  101.  
  102.         /* get the content of a file, and store in buf. */
  103. int getfile(char *fname, unsigned char **buf)
  104. {
  105.     FILE *f;
  106.     long l, l2;
  107.    
  108.     f=fopen(fname,"r"); if(f==NULL) return -1;
  109.     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  110.     if(l>FILE_LENGTH_LIMIT || l<=0) {
  111.         fclose(f); return -1;
  112.     }
  113.     *buf=xmalloc(l+16);
  114.     l2=fread(*buf,1,l,f); fclose(f);
  115.     if(l!=l2) {
  116.         free(*buf); return -1;
  117.     }
  118.     return l;
  119. }
  120.  
  121. int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
  122. {
  123.     pid_t pid;
  124.     int status;
  125.  
  126.     fflush(NULL);       /* flush all output streams before forking
  127.                          * otherwise they will be doubled */
  128.     pid=fork(); if(pid==-1) return -1;
  129.     if(!pid) {  /* child */
  130.         if(inf!=NULL) freopen(inf,"r",stdin);
  131.         if(outf!=NULL) freopen(outf,"w",stdout);
  132.         if(errf!=NULL) freopen(errf,"w",stderr);
  133.         if(wrapexec) {
  134.             setreuid(getuid(),getuid());setregid(getgid(),getgid());
  135.         }
  136.         if(strchr(cmdf,'/')) execv(cmdf,arg);
  137.         else execvp(cmdf,arg);
  138.         exit(127);
  139.     }
  140.     else {      /* parent */
  141.         status=0; waitpid(pid,&status,0);
  142.         return WEXITSTATUS(status);
  143.     }
  144. }
  145.  
  146.         /* system(), but with variable parms
  147.          * Uses sh to execute command. */
  148. int call_sh(char *s,...)
  149. {
  150.     va_list vp;
  151.     char buf[MAX_LINELEN+1];
  152.     char *parmbuf[4];
  153.     int t;
  154.  
  155.     va_start(vp,s);
  156.     vsnprintf(buf,sizeof(buf),s,vp);
  157.     va_end(vp);
  158.     parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
  159.     wrapexec=0;
  160.     t=execredirected("sh",NULL,NULL,NULL,parmbuf);
  161.     sync(); return t;
  162. }
  163.  
  164.         /* recursively generate a directory structure */
  165. void mkdirs(char *s)
  166. {
  167.     struct stat st;
  168.     char *buf;
  169.     if(stat(s,&st)==-1) {
  170.         if(strrchr(s,'/')!=NULL) {
  171.             buf=xmalloc(strlen(s)+1);
  172.             strcpy(buf,s); *strrchr(buf,'/')=0;
  173.             mkdirs(buf); free(buf);
  174.         }
  175.         mkdir(s,-1);
  176.     }
  177. }
  178.  
  179.