Subversion Repositories wimsdev

Rev

Rev 8149 | 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. #include "texgif.h"
  21.  
  22. void texgif_error(char *s)
  23. {
  24.     fprintf(stderr,"%s: %s\n",progname, s);
  25.     exit(1);
  26. }
  27.  
  28. unsigned long int texint(void *bp, int l)
  29. {
  30.     unsigned long int o, t;
  31.     unsigned char c, *p;
  32.     int i;
  33.     o=t=0; p=bp;
  34.     for(i=0;i<l;i++) {
  35.         c=p[i]; t=c; o<<=8; o+=t;
  36.     }
  37.     return o;
  38. }
  39.  
  40. long int texintsigned(void *bp, int l)
  41. {
  42.     long int o, t;
  43.     signed char c, *p;
  44.     int i;
  45.     if(l<=0) return 0;
  46.     p=bp; o=*p;
  47.     for(i=1;i<l;i++) {
  48.         c=p[i]; t=c; o<<=8; o|=t&255;
  49.     }
  50.     return o;
  51. }
  52.  
  53. /* get the content of a file, and store in buf. */
  54. int getfile(char *fname, unsigned char **buf)
  55. {
  56.     FILE *f;
  57.     long l, l2;
  58.  
  59.     f=fopen(fname,"r"); if(f==NULL) return -1;
  60.     fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
  61.     if(l>FILE_LENGTH_LIMIT || l<=0) {
  62.         fclose(f); return -1;
  63.     }
  64.     *buf=xmalloc(l+16);
  65.     l2=fread(*buf,1,l,f); fclose(f);
  66.     if(l!=l2) {
  67.         free(*buf); return -1;
  68.     }
  69.     return l;
  70. }
  71.  
  72. int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
  73. {
  74.     pid_t pid;
  75.     int status;
  76.  
  77.     fflush(NULL);       /* flush all output streams before forking
  78.                          * otherwise they will be doubled */
  79.     pid=fork(); if(pid==-1) return -1;
  80.     if(!pid) {  /* child */
  81.         if(inf!=NULL && freopen(inf,"r",stdin) == NULL)
  82.           texgif_error("freopen failure");
  83.         if(outf!=NULL && freopen(outf,"w",stdout))
  84.           texgif_error("freopen failure");
  85.         if(errf!=NULL && freopen(errf,"w",stderr))
  86.           texgif_error("freopen failure");
  87.         if(wrapexec) {
  88.             setreuid(getuid(),getuid());setregid(getgid(),getgid());
  89.         }
  90.         if(strchr(cmdf,'/')) execv(cmdf,arg);
  91.         else execvp(cmdf,arg);
  92.         exit(127);
  93.     }
  94.     else {      /* parent */
  95.         status=0; waitpid(pid,&status,0);
  96.         return WEXITSTATUS(status);
  97.     }
  98. }
  99.  
  100. /* system(), but with variable parms
  101.  * Uses sh to execute command.
  102.  */
  103. int call_sh(char *s,...)
  104. {
  105.     va_list vp;
  106.     char buf[MAX_LINELEN+1];
  107.     char *parmbuf[4];
  108.     int t;
  109.  
  110.     va_start(vp,s);
  111.     vsnprintf(buf,sizeof(buf),s,vp);
  112.     va_end(vp);
  113.     parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
  114.     wrapexec=0;
  115.     t=execredirected("sh",NULL,NULL,NULL,parmbuf);
  116.     sync(); return t;
  117. }
  118.  
  119.