File: os9.c

package info (click to toggle)
gnuplot 3.5beta6.340-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 4,792 kB
  • ctags: 4,205
  • sloc: ansic: 41,878; asm: 539; makefile: 498; objc: 379; csh: 297; sh: 277; pascal: 194; perl: 138; lisp: 88
file content (181 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download
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
/*
 * os9.c
 *
 * Some Unix like functions that gnuplot uses.
 * Original sources from the blars lib.
 *
 */

#include <stdio.h>
#include <modes.h>
#include <direct.h>
#include <sgstat.h>

#ifdef PIPES

/* Original version by Robert A. Larson */
/* Adapted by M.N. Schipper */

#include <string.h>
#include <module.h>

extern char *_environ;
extern int os9fork();
extern mh_com *modlink();
extern mh_com *modloadp();

static int proc[_NFILE];
static mh_com *loadmods[_NFILE];

FILE *popen(command, mode)
char *command;
char *mode;
{
    int temp, fd;
    FILE *pipe;
    char *argv[4];
    register char *cp;
    mh_com *mod;
    int linked = 0;

    if(mode[1]!='\0' || (*mode!='r' && *mode!='w')) return (FILE *)NULL;
    fd = (*mode=='r');
    if((temp = dup(fd)) <= 0) return (FILE *)NULL;
    if((pipe = fopen("/pipe", "r+")) == NULL) {
    	close(temp);
    	return (FILE *)NULL;
    }
    close(fd);
    dup(fileno(pipe));

    if (strrchr (command, '/') == NULL)
		mod = modlink (command, 0);
	else
		mod = (mh_com *) -1;
	if (mod == (mh_com *) -1)
		loadmods[fileno(pipe)] = mod = modloadp (command, 0, NULL);
	else
	{
		linked = 1;
		loadmods[fileno(pipe)] = (mh_com *) -1;
	}
		
    argv[0] = "shell";
    if (mod != (mh_com *) -1)
    {
	    argv[1] = "ex";
	    argv[2] = command;
    	argv[3] = (char *)NULL;
    }
    else
    {
    	argv[1] = command;
	    argv[2] = (char *)NULL;
	}
    if((proc[fileno(pipe)] = os9exec(os9fork, argv[0], argv, _environ, 0, 0)) < 0)
    {
		fclose(pipe);
		pipe = NULL;
	}
    close(fd);
    dup(temp);
    close(temp);
	if (linked && mod != (mh_com *) -1)
		munlink (mod);
    return pipe;
}

int pclose(pipe)
FILE *pipe;
{
    int p, stat, w;

    if((p = proc[fileno(pipe)]) <= 0) return -1;
    proc[fileno(pipe)] = 0;
    fflush(pipe);
	if (loadmods[fileno(pipe)] != (mh_com *) -1)
		munlink (loadmods[fileno(pipe)]);
    fclose(pipe);
    while((w=wait(&stat)) != -1 && w!=p) {}
    return w==-1 ? -1 : stat;
}

#endif	/* PIPES */


int isatty(f)
int f;
{
    struct sgbuf sgbuf;

    if(_gs_opt(f, &sgbuf) < 0) return -1;
    return sgbuf.sg_class == 0;
}


char *getwd(p)
char *p;
{
    register char *cp;
    register struct dirent *dp;
    register int l, olddot = 0, i, d, dot, dotdot;
    struct dirent db[8];
    char buf[1024];

    cp = &buf[1024-1];
    *cp = '\0';
    for(;;) {
	if((d = open(".", S_IREAD | S_IFDIR)) < 0) {
	    if(*cp) chdir(cp+1);
	    return NULL;
	}
	if((i = read(d, (char *)db, sizeof(db))) == 0) {
	    if(*cp) chdir(cp+1);
	    close(d);
	    return NULL;
	}
	dotdot = db[0].dir_addr;
	dot = db[1].dir_addr;
	if(olddot) {
	    i -= 2 * sizeof(struct dirent);
	    dp = &db[2];
	    for(;;) {
	        if(i <= 0) {
		    if((i = read(d, (char *)db, sizeof(db))) == 0) {
			if(*cp) chdir(cp+1);
			close(d);
			return NULL;
		    }
		    dp = &db[0];
		}
		if(olddot == dp->dir_addr) {
		    l = strlen(dp->dir_name);
		    /* last character has parity bit set... */
		    *--cp = dp->dir_name[--l] & 0x7f;
		    while(l) *--cp = dp->dir_name[--l];
		    *--cp = '/';
		    break;
		}
		i -= sizeof(struct dirent);
		dp++;
	    }
	}
	if(dot==dotdot) {
	    if(*cp) chdir(cp+1);
	    *p = '/';
	    if(_gs_devn(d, p+1) < 0) {
	        close(d);
		return NULL;
	    }
	    close(d);
	    strcat(p, cp);
	    return p;
	}
	close(d);
	if(chdir("..") != 0) {
	    if(*cp) chdir(cp+1);
	    return NULL;
	}
	olddot = dot;
    }
}