Subversion Repositories wimsdev

Rev

Rev 8100 | Rev 8149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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