Subversion Repositories wimsdev

Rev

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