File: os9.c

package info (click to toggle)
gnuplot 3.5beta6.347-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 5,032 kB
  • ctags: 4,235
  • sloc: ansic: 42,086; makefile: 561; asm: 539; sh: 386; objc: 379; csh: 297; pascal: 194; perl: 138; lisp: 88
file content (213 lines) | stat: -rw-r--r-- 4,967 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
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
/* $Id: os9.c,v 1.2 1998/03/22 22:31:54 drd Exp $ */

/* GNUPLOT - os9.c */

/*[
 * Copyright 1986 - 1993, 1998   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/


/*
 * 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;
    }
}