Subversion Repositories wimsdev

Rev

Rev 10 | Rev 3835 | 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 */
3718 reyssat 19
#include "../Lib/basicstr.c"
10 reyssat 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);
132
        if(outf!=NULL) freopen(outf,"w",stdout);
133
        if(errf!=NULL) freopen(errf,"w",stderr);
134
        if(wrapexec) {
135
            setreuid(getuid(),getuid());setregid(getgid(),getgid());
136
        }
137
        if(strchr(cmdf,'/')) execv(cmdf,arg);
138
        else execvp(cmdf,arg);
139
        exit(127);
140
    }
141
    else {      /* parent */
142
        status=0; waitpid(pid,&status,0);
143
        return WEXITSTATUS(status);
144
    }
145
}
146
 
147
        /* system(), but with variable parms
148
         * Uses sh to execute command. */
149
int call_sh(char *s,...)
150
{
151
    va_list vp;
152
    char buf[MAX_LINELEN+1];
153
    char *parmbuf[4];
154
    int t;
155
 
156
    va_start(vp,s);
157
    vsnprintf(buf,sizeof(buf),s,vp);
158
    va_end(vp);
159
    parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
160
    wrapexec=0;
161
    t=execredirected("sh",NULL,NULL,NULL,parmbuf);
162
    sync(); return t;
163
}
164
 
165
        /* recursively generate a directory structure */
166
void mkdirs(char *s)
167
{
168
    struct stat st;
169
    char *buf;
170
    if(stat(s,&st)==-1) {
171
        if(strrchr(s,'/')!=NULL) {
172
            buf=xmalloc(strlen(s)+1);
3718 reyssat 173
            ovlstrcpy(buf,s); *strrchr(buf,'/')=0;
10 reyssat 174
            mkdirs(buf); free(buf);
175
        }
176
        mkdir(s,-1);
177
    }
178
}
179