File: popen.c

package info (click to toggle)
cproto 4.7j-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 916 kB
  • sloc: ansic: 4,116; sh: 3,077; lex: 1,059; yacc: 870; makefile: 309
file content (81 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download | duplicates (14)
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
/* $Id: popen.c,v 4.2 1998/01/08 00:03:28 cthuang Exp $
 *
 * Imitate a UNIX pipe in MS-DOS.
 */
#ifdef MSDOS
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <io.h>
#include "cproto.h"

static char pipe_name[FILENAME_MAX];	/* name of the temporary file */

/* Open a pipe for reading.
 */
FILE *
popen (cmd, type)
char *cmd, *type;
{
    char *tmpdir, *argv[30], **arg, *cmdline, *s, opt[FILENAME_MAX];
    int ostdout, status;

    /* Set temporary file name. */
    if ((tmpdir = getenv("TMP")) == NULL) {
	pipe_name[0] = '\0';
    } else {
	strcpy(pipe_name, tmpdir);
	trim_path_sep(pipe_name);
	strcat(pipe_name, "/");
    }
    strcat(pipe_name, tmpnam(NULL));

    /* Split the command into an argument array. */
    cmdline = xstrdup(cmd);
    arg = argv;
    s = strtok(cmdline, " ");
    *arg++ = s;
#ifdef TURBO_CPP
    sprintf(opt, "-o%s", pipe_name);
    *arg++ = opt;
#endif
    while ((s = strtok(NULL, " ")) != NULL) {
	*arg++ = s;
    }
    *arg = NULL;
 
    /* Redirect the program's stdout. */
    ostdout = dup(fileno(stdout));
#ifdef TURBO_CPP
    freopen("nul", "w", stdout);
#else
    freopen(pipe_name, "w", stdout);
#endif
 
    /* Run the program. */
    status = spawnvp(P_WAIT, argv[0], argv);
 
    /* Restore stdout. */
    dup2(ostdout, fileno(stdout));
    free(cmdline);

    if (status != 0)
	return NULL;
 
    /* Open the intermediate file and return the stream. */
    return fopen(pipe_name, type) ;
}
 
/* Close the pipe.
 */
int
pclose (f)
FILE *f;
{
    int status;
 
    status = fclose(f);
    unlink(pipe_name);
    return status;
}
#endif	/* MSDOS */