Subversion Repositories wimsdev

Rev

Rev 3718 | Rev 8082 | 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. #include "../Lib/basicstr.c"
  20.  
  21. void error(char *s)
  22. {
  23.     fprintf(stderr,"%s: %s\n",progname, s);
  24.     exit(1);
  25. }
  26.  
  27. void *xmalloc(size_t n)
  28. {
  29.     void *p;
  30.     p=malloc(n);
  31.     if(p==NULL) error("Malloc failure.");
  32.     return p;
  33. }
  34.  
  35. unsigned long int texint(void *bp, int l)
  36. {
  37.     unsigned long int o, t;
  38.     unsigned char c, *p;
  39.     int i;
  40.     o=t=0; p=bp;
  41.     for(i=0;i<l;i++) {
  42.         c=p[i]; t=c; o<<=8; o+=t;
  43.     }
  44.     return o;
  45. }
  46.  
  47. long int texintsigned(void *bp, int l)
  48. {
  49.     long int o, t;
  50.     signed char c, *p;
  51.     int i;
  52.     if(l<=0) return 0;
  53.     p=bp; o=*p;
  54.     for(i=1;i<l;i++) {
  55.         c=p[i]; t=c; o<<=8; o|=t&255;
  56.     }
  57.     return o;
  58. }
  59.  
  60.         /* Strips leading spaces */
  61. char *find_word_start(char *p)
  62. {
  63.     int i;
  64.     for(i=0; isspace(*p); p++,i++);
  65.     return p;
  66. }
  67.  
  68.         /* Points to the end of the word */
  69. char *find_word_end(char *p)
  70. {
  71.     int i;
  72.     for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++);
  73.     return p;
  74. }
  75.  
  76.         /* searches a list. Returns index if found, -1 if nomatch.
  77.          * Uses binary search, list must be sorted. */
  78. int search_list(void *list, int items, size_t item_size, const char *str)
  79. {
  80.     int i1,i2,j,k;
  81.     char **p;
  82.     char c;
  83.    
  84.     if(items<=0) return -1;
  85.     j=0; c=*str;
  86.     p=list;
  87.     k=**p-c; if(k==0) k=strcmp(*p,str);
  88.     if(k==0) return k; if(k>0) return -1;
  89.     p=list+(items-1)*item_size;
  90.     k=**p-c; if(k==0) k=strcmp(*p,str);
  91.     if(k==0) return items-1; if(k<0) return -1;
  92.     for(i1=0,i2=items-1;i2>i1+1;) {
  93.         j=i1+(i2-i1)/2;
  94.         p=list+(j*item_size);
  95.         k=**p-c; if(k==0) k=strcmp(*p,str);
  96.         if(k==0) return j;
  97.         if(k>0) {i2=j; continue;}
  98.         if(k<0) {i1=j; continue;}      
  99.     }
  100.     return -1;
  101. }
  102.  
  103.         /* get the content of a file, and store in buf. */
  104. int getfile(char *fname, unsigned char **buf)
  105. {
  106.     FILE *f;
  107.     long l, l2;
  108.    
  109.     f=fopen(fname,"r"); if(f==NULL) return -1;
  110.     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  111.     if(l>FILE_LENGTH_LIMIT || l<=0) {
  112.         fclose(f); return -1;
  113.     }
  114.     *buf=xmalloc(l+16);
  115.     l2=fread(*buf,1,l,f); fclose(f);
  116.     if(l!=l2) {
  117.         free(*buf); return -1;
  118.     }
  119.     return l;
  120. }
  121.  
  122. int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
  123. {
  124.     pid_t pid;
  125.     int status;
  126.  
  127.     fflush(NULL);       /* flush all output streams before forking
  128.                          * otherwise they will be doubled */
  129.     pid=fork(); if(pid==-1) return -1;
  130.     if(!pid) {  /* child */
  131.         if(inf!=NULL && freopen(inf,"r",stdin) == NULL)
  132.           error("freopen failure");
  133.         if(outf!=NULL && freopen(outf,"w",stdout))
  134.           error("freopen failure");
  135.         if(errf!=NULL && freopen(errf,"w",stderr))
  136.           error("freopen failure");
  137.         if(wrapexec) {
  138.             setreuid(getuid(),getuid());setregid(getgid(),getgid());
  139.         }
  140.         if(strchr(cmdf,'/')) execv(cmdf,arg);
  141.         else execvp(cmdf,arg);
  142.         exit(127);
  143.     }
  144.     else {      /* parent */
  145.         status=0; waitpid(pid,&status,0);
  146.         return WEXITSTATUS(status);
  147.     }
  148. }
  149.  
  150.         /* system(), but with variable parms
  151.          * Uses sh to execute command. */
  152. int call_sh(char *s,...)
  153. {
  154.     va_list vp;
  155.     char buf[MAX_LINELEN+1];
  156.     char *parmbuf[4];
  157.     int t;
  158.  
  159.     va_start(vp,s);
  160.     vsnprintf(buf,sizeof(buf),s,vp);
  161.     va_end(vp);
  162.     parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
  163.     wrapexec=0;
  164.     t=execredirected("sh",NULL,NULL,NULL,parmbuf);
  165.     sync(); return t;
  166. }
  167.  
  168.         /* recursively generate a directory structure */
  169. void mkdirs(char *s)
  170. {
  171.     struct stat st;
  172.     char *buf;
  173.     if(stat(s,&st)==-1) {
  174.         if(strrchr(s,'/')!=NULL) {
  175.             buf=xmalloc(strlen(s)+1);
  176.             ovlstrcpy(buf,s); *strrchr(buf,'/')=0;
  177.             mkdirs(buf); free(buf);
  178.         }
  179.         mkdir(s,-1);
  180.     }
  181. }
  182.  
  183.