Subversion Repositories wimsdev

Rev

Rev 3835 | Rev 8100 | 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
 
8082 bpr 76
/* searches a list. Returns index if found, (-1-index of insertion) if nomatch.
77
 * Uses binary search, list must be sorted. */
78
 
10 reyssat 79
int search_list(void *list, int items, size_t item_size, const char *str)
80
{
8082 bpr 81
 int i = 0;
82
 while (items > 0)
83
   {
84
     int m = items / 2, j = i + m;
85
     int k = strcmp(*(char **)(list + j * item_size), str);
86
     if (k == 0) return j;
87
     if (k > 0) items = m; else {i = j + 1; items -= (m + 1);}
88
   }
89
 return ~i;
10 reyssat 90
}
91
 
92
        /* get the content of a file, and store in buf. */
93
int getfile(char *fname, unsigned char **buf)
94
{
95
    FILE *f;
96
    long l, l2;
8082 bpr 97
 
10 reyssat 98
    f=fopen(fname,"r"); if(f==NULL) return -1;
99
    fseek(f,0,SEEK_END); l=ftell(f); fseek(f,0,SEEK_SET);
100
    if(l>FILE_LENGTH_LIMIT || l<=0) {
101
        fclose(f); return -1;
102
    }
103
    *buf=xmalloc(l+16);
104
    l2=fread(*buf,1,l,f); fclose(f);
105
    if(l!=l2) {
106
        free(*buf); return -1;
107
    }
108
    return l;
109
}
110
 
111
int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[])
112
{
113
    pid_t pid;
114
    int status;
115
 
116
    fflush(NULL);       /* flush all output streams before forking
117
                         * otherwise they will be doubled */
118
    pid=fork(); if(pid==-1) return -1;
119
    if(!pid) {  /* child */
3835 kbelabas 120
        if(inf!=NULL && freopen(inf,"r",stdin) == NULL)
121
          error("freopen failure");
122
        if(outf!=NULL && freopen(outf,"w",stdout))
123
          error("freopen failure");
124
        if(errf!=NULL && freopen(errf,"w",stderr))
125
          error("freopen failure");
10 reyssat 126
        if(wrapexec) {
127
            setreuid(getuid(),getuid());setregid(getgid(),getgid());
128
        }
129
        if(strchr(cmdf,'/')) execv(cmdf,arg);
130
        else execvp(cmdf,arg);
131
        exit(127);
132
    }
133
    else {      /* parent */
134
        status=0; waitpid(pid,&status,0);
135
        return WEXITSTATUS(status);
136
    }
137
}
138
 
139
        /* system(), but with variable parms
140
         * Uses sh to execute command. */
141
int call_sh(char *s,...)
142
{
143
    va_list vp;
144
    char buf[MAX_LINELEN+1];
145
    char *parmbuf[4];
146
    int t;
147
 
148
    va_start(vp,s);
149
    vsnprintf(buf,sizeof(buf),s,vp);
150
    va_end(vp);
151
    parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL;
152
    wrapexec=0;
153
    t=execredirected("sh",NULL,NULL,NULL,parmbuf);
154
    sync(); return t;
155
}
156
 
157
        /* recursively generate a directory structure */
158
void mkdirs(char *s)
159
{
160
    struct stat st;
161
    char *buf;
162
    if(stat(s,&st)==-1) {
163
        if(strrchr(s,'/')!=NULL) {
164
            buf=xmalloc(strlen(s)+1);
3718 reyssat 165
            ovlstrcpy(buf,s); *strrchr(buf,'/')=0;
10 reyssat 166
            mkdirs(buf); free(buf);
167
        }
168
        mkdir(s,-1);
169
    }
170
}
171