File: eperl_sys.c

package info (click to toggle)
eperl 2.2.14-21
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,304 kB
  • ctags: 738
  • sloc: ansic: 4,694; perl: 584; sh: 556; makefile: 353
file content (450 lines) | stat: -rw-r--r-- 10,464 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
**        ____           _ 
**    ___|  _ \ ___ _ __| |
**   / _ \ |_) / _ \ '__| |
**  |  __/  __/  __/ |  | |
**   \___|_|   \___|_|  |_|
** 
**  ePerl -- Embedded Perl 5 Language
**
**  ePerl interprets an ASCII file bristled with Perl 5 program statements
**  by evaluating the Perl 5 code while passing through the plain ASCII
**  data. It can operate both as a standard Unix filter for general file
**  generation tasks and as a powerful Webserver scripting language for
**  dynamic HTML page programming. 
**
**  ======================================================================
**
**  Copyright (c) 1996,1997,1998,1999 Ralf S. Engelschall <rse@engelschall.com>
**
**  This program is free software; it may be redistributed and/or modified
**  only under the terms of either the Artistic License or the GNU General
**  Public License, which may be found in the ePerl source distribution.
**  Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
**  a built-in copy of both license files.
**
**  This program is distributed in the hope that it will be useful, but
**  WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
**  Artistic License or the GNU General Public License for more details.
**
**  ======================================================================
**
**  eperl_sys.c -- ePerl system functions
*/

#include "eperl_config.h"
#include "eperl_global.h"
#include "eperl_proto.h"

char * getenv ();

/*
**
**  own setenv() function which works with Perl
**
*/

extern char **environ;

char **mysetenv(char **env, char *var, char *str, ...)
{
    va_list ap;
    char ca[1024];
    char ca2[1024];
    char *cp;
    int i;
    char **envN;
    static int stillcalled = FALSE;
    int replaced = FALSE;

    /*  create the key=val string  */
    va_start(ap, str);
    vsnprintf(ca, sizeof(ca), str, ap);
    ca[sizeof(ca)-1] = NUL;
    snprintf(ca2, sizeof(ca2), "%s=%s", var, ca);
    ca2[sizeof(ca2)-1] = NUL;
    cp = strdup(ca2);

    /*  now duplicate the old structure  */
    for (i = 0; env[i] != NULL; i++)
        ;
    envN = (char **)malloc(sizeof(char *) * (i+2));
    for (i = 0; env[i] != NULL; i++) {
        if (strncmp(env[i], var, strlen(var)) == 0) {
            envN[i] = cp;
            replaced = TRUE;
        }
        else
            envN[i] = env[i];
    }

    /*  add the new entry if not replaced */
    if (!replaced) {
        envN[i++] = cp;
    }
    envN[i] = NULL;

    /*  set the libc/exec variable which Perl uses */
    if (stillcalled) 
        free(environ);
    stillcalled = TRUE;
    environ = envN;

    va_end(ap);
    return envN;
}


/*
**
**  I/O handle redirection
**
*/

#define HANDLE_STDIN  0
#define HANDLE_STDOUT 1
#define HANDLE_STDERR 2

static int IO_redirected_stdin  = FALSE;
static int IO_redirected_stdout = FALSE;
static int IO_redirected_stderr = FALSE;

static int IO_handle_store_stdin;
static int IO_handle_store_stdout;
static int IO_handle_store_stderr;

void IO_redirect_stdin(FILE *fp)
{
    if (IO_redirected_stdin)
        IO_restore_stdin();

    /* first copy the current stdin to the store handle */
    IO_handle_store_stdin = dup(HANDLE_STDIN);

    /* then copy the new handle to stdin */
    (void)dup2(fileno(fp), HANDLE_STDIN);

    /* and remember the fact */
    IO_redirected_stdin = TRUE;
}

void IO_redirect_stdout(FILE *fp)
{
    if (IO_redirected_stdout)
        IO_restore_stdout();

    /* first copy the current stdout to the store handle */
    IO_handle_store_stdout = dup(HANDLE_STDOUT);

    /* then copy the new handle to stdout */
    (void)dup2(fileno(fp), HANDLE_STDOUT);

    /* and remember the fact */
    IO_redirected_stdout = TRUE;
}

void IO_redirect_stderr(FILE *fp)
{
    if (IO_redirected_stderr)
        IO_restore_stderr();

    /* first copy the current stderr to the store handle */
    IO_handle_store_stderr = dup(HANDLE_STDERR);

    /* then copy the new handle to stderr */
    (void)dup2(fileno(fp), HANDLE_STDERR);

    /* and remember the fact */
    IO_redirected_stderr = TRUE;
}

int IO_is_stdin_redirected(void)
{
    return IO_redirected_stdin;
}

int IO_is_stdout_redirected(void)
{
    return IO_redirected_stdout;
}

int IO_is_stderr_redirected(void)
{
    return IO_redirected_stderr;
}

void IO_restore_stdin(void)
{
    if (IO_redirected_stdin) {
        dup2(IO_handle_store_stdin, HANDLE_STDIN); 
        IO_redirected_stdin = FALSE;
    }
}

void IO_restore_stdout(void)
{
    if (IO_redirected_stdout) {
        dup2(IO_handle_store_stdout, HANDLE_STDOUT); 
        IO_redirected_stdout = FALSE;
    }
}

void IO_restore_stderr(void)
{
    if (IO_redirected_stderr) {
      dup2(IO_handle_store_stderr, HANDLE_STDERR); 
        IO_redirected_stderr = FALSE;
    }
}


/*
**
**  Temporary filename support  
**
*/

static char *mytmpfiles[100] = { NULL };
static int mytmpfilecnt = 0;

char *mytmpfile(char *id)
{
    char ca[1024];
    char *cp, *tmpdir;
    int i;

    tmpdir = getenv ("TMPDIR");
    if (tmpdir == (char *) NULL)
        tmpdir="/tmp";

    snprintf(ca, sizeof(ca), "%s/%s.%d.tmp%d", tmpdir, id, (int)getpid(), mytmpfilecnt++);
    ca[sizeof(ca)-1] = NUL;
    cp = strdup(ca);
    for (i = 0; mytmpfiles[i] != NULL; i++)
        ;
    mytmpfiles[i++] = cp;
    mytmpfiles[i] = NULL;
    return cp;
}

void remove_mytmpfiles(void)
{
    int i;

    for (i = 0; mytmpfiles[i] != NULL; i++) {
        unlink(mytmpfiles[i]);
    }
}


/*
**
**  ISO time
**
*/

char *isotime(time_t *t)
{
    struct tm *tm;
    char timestr[128];

    tm = localtime(t);
    sprintf(timestr, "%02d-%02d-%04d %02d:%02d",
                      tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900,
                      tm->tm_hour, tm->tm_min);
    return strdup(timestr);
}


/*
**
**  read source file into internal buffer 
**
*/
char *ePerl_ReadSourceFile(char *filename, char **cpBufC, int *nBufC)
{
    char *rc;
    FILE *fp = NULL;
    char *cpBuf = NULL;
    int nBuf;
    char tmpfile[256], *ptr_tmpfile;
    int usetmp = 0;
    int c;

    if (stringEQ(filename, "-")) {
        /* file is given on stdin */
        ptr_tmpfile = mytmpfile("ePerl.source");
        sprintf(tmpfile, "%s", ptr_tmpfile);
        if ((fp = fopen(tmpfile, "w")) == NULL) {
            ePerl_SetError("Cannot open temporary source file %s for writing", tmpfile);
            CU(NULL);
        }
        nBuf = 0;
        while ((c = fgetc(stdin)) != EOF) {
            fprintf(fp, "%c", c);
        }
        fclose(fp);
        fp = NULL;
        filename = tmpfile;
        usetmp = 1;
    }

    if ((fp = fopen(filename, "r")) == NULL) {
        ePerl_SetError("Cannot open source file %s for reading", filename);
        CU(NULL);
    }
    fseek(fp, 0, SEEK_END);
    nBuf = ftell(fp);
    if (nBuf == 0) {
        cpBuf = (char *)malloc(sizeof(char) * 1);
        *cpBuf = NUL;
    }
    else {
        if ((cpBuf = (char *)malloc(sizeof(char) * nBuf+1)) == NULL) {
            ePerl_SetError("Cannot allocate %d bytes of memory", nBuf);
            CU(NULL);
        }
        fseek(fp, 0, SEEK_SET);
        if (fread(cpBuf, nBuf, 1, fp) == 0) {
            ePerl_SetError("Cannot read from file %s", filename);
            CU(NULL);
        }
        cpBuf[nBuf] = '\0';
    }
    *cpBufC = cpBuf;
    *nBufC  = nBuf;
    RETURN_WVAL(cpBuf);

    CUS:
    if (cpBuf)
        free(cpBuf);
    if (fp)
        fclose(fp);
    if (usetmp)
        unlink(tmpfile);
    RETURN_EXRC;
}

/*
**
**  read an error file to internal buffer and substitute the filename
**
*/
char *ePerl_ReadErrorFile(char *filename, char *scriptfile, char *scripturl)
{
    char *rc;
    FILE *fp = NULL;
    char *cpBuf = NULL;
    int nBuf;
    char *cp;

    if ((fp = fopen(filename, "r")) == NULL) {
        ePerl_SetError("Cannot open error file %s for reading", filename);
        CU(NULL);
    }
    fseek(fp, 0, SEEK_END);
    nBuf = ftell(fp);
    if ((cpBuf = (char *)malloc(sizeof(char) * nBuf * 2)) == NULL) {
        ePerl_SetError("Cannot allocate %d bytes of memory", nBuf * 2);
        CU(NULL);
    }
    fseek(fp, 0, SEEK_SET);
    if (fread(cpBuf, nBuf, 1, fp) == 0) {
        ePerl_SetError("Cannot read from file %s", filename);
        CU(NULL);
    }
    cpBuf[nBuf] = '\0';
    for (cp = cpBuf; cp < cpBuf+nBuf; ) {
        if ((cp = strstr(cp, scriptfile)) != NULL) {
#ifdef HAVE_MEMMOVE
            (void)memmove(cp+strlen(scripturl), cp+strlen(scriptfile), strlen(cp+strlen(scriptfile))+1);
#else
            (void)bcopy(cp+strlen(scriptfile), cp+strlen(scripturl), strlen(cp+strlen(scriptfile))+1);
#endif
            (void)memcpy(cp, scripturl, strlen(scripturl));
            cp += strlen(scripturl);
            continue;
        }
        break;
    }
    RETURN_WVAL(cpBuf);

    CUS:
    if (cpBuf)
        free(cpBuf);
    if (fp)
        fclose(fp);
    RETURN_EXRC;
}

/*
**
**  path support
**
*/

char *filename(char *path) 
{
    static char file[MAXPATHLEN];
    char *cp;

    if (path[strlen(path)-1] == '/')
        return "";
    else {
        for (cp = path+strlen(path); cp > path && *(cp-1) != '/'; cp--)
            ;
        if (cp == path+1)
            cp--;
        strncpy(file, cp, sizeof(file));
        file[sizeof(file)-1] = '\0';
        return file;
    }
}

char *dirname(char *path) 
{
    static char dir[MAXPATHLEN];
    char *cp;

    if (path[strlen(path)-1] == '/')
        return path;
    else {
        strncpy(dir, path, sizeof(dir));
        dir[sizeof(dir)-1] = '\0';
        for (cp = dir+strlen(dir); cp > dir && *(cp-1) != '/'; cp--)
            ;
        *cp = NUL;
        return dir;
    }
}

char *abspath(char *path) 
{
    static char apath[MAXPATHLEN];
    static char cwd[MAXPATHLEN];
    char *cp;

    if (path[0] == '/')
        return path;
    else {
        /* remember current working dir */
        getcwd(cwd, MAXPATHLEN);
        /* determine dir of path */
        cp = dirname(path);
        chdir(cp);
        getcwd(apath, MAXPATHLEN);
        /* restore cwd */
        chdir(cwd);
        /* add file part again */
        if (apath[strlen(apath)-1] != '/') {
            strncpy(apath+strlen(apath), "/", sizeof(apath)-strlen(apath));
            apath[sizeof(apath)-1] = '\0';
        }
        strncpy(apath+strlen(apath), path, sizeof(apath)-strlen(apath));
        apath[sizeof(apath)-1] = '\0';
        return apath;
    }
}

/*EOF*/