1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % File: PXK:UNIX-IO.C
5 % Description: Unix PSL FileDescriptors are implemented as stdio streams
6 % ("FILE *".)
7 % Author: Russell D. Fish
8 % Created: Thu Feb 16 1984
9 % Modified: 17-Jul-84 22:49:12 (RAM)
10 % Mode: Text
11 % Package:
12 % Status: Open Source: BSD License
13 %
14 % (c) Copyright 1983, Hewlett-Packard Company, see the file
15 % HP_disclaimer at the root of the PSL file tree
16 %
17 % (c) Copyright 1982, University of Utah
18 %
19 % Redistribution and use in source and binary forms, with or without
20 % modification, are permitted provided that the following conditions are met:
21 %
22 % * Redistributions of source code must retain the relevant copyright
23 % notice, this list of conditions and the following disclaimer.
24 % * Redistributions in binary form must reproduce the above copyright
25 % notice, this list of conditions and the following disclaimer in the
26 % documentation and/or other materials provided with the distribution.
27 %
28 % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 % THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
32 % CONTRIBUTORS
33 % BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 % POSSIBILITY OF SUCH DAMAGE.
40 %
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 %
43 % Revisions:
44 %
45 % 15-Sep-88 (T. Yamamoto and C. Burdorf)
46 % Moved collect out of expand_file_name so it won't be overwritten as a
47 % local before it is referenced.
48 % 20-Sep-86 (Leigh Stoller)
49 % Removed assembler alias statements because they are not portable. Instead,
50 % a sed script will be used to convert the _variables of C to VARIABLES of
51 % PSL.
52 % 17-Jul-84 22:48:32 (RAM)
53 % Added unixcd, a routine that calls expand_file_name before calling chdir.
54 % 3-Jul-84 10:45:57 (RAM)
55 % Added expand_file_name (called from unixopen) to expand shell variable
56 % references and ~ home directory syntax.
57 % 29-Jun-84 14:21:21 (RAM)
58 % Added unixputs and unixopen.
59 % 21-May-84 17:41:41 (Vicki O'Day)
60 % Added unixcleario. It is called by syscleario.
61 %
62 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63 */
64
65 #include <stdio.h>
66 #include <stdlib.h>
67
68 /* There is an assumption here that coercing addresses into ints is OK */
69 /*
70 asm(" alias _unix_stdin,UNIXSTDIN");
71 asm(" alias _unix_stdout,UNIXSTDOUT");
72 asm(" alias _unix_stderr,UNIXSTDERR");
73 asm(" alias _unix_null,UNIXNULL");
74 asm(" alias _unix_eof,UNIXEOF");
75 asm(" alias _unix_tty,UNIXTTY");
76 */
77 /* Initialize some PSL external variables with FileDescriptors for SysClearIO.
78 */
79
80 extern FILE * unixstdin, * unixstdout, * unixstderr, * unixtty;
81
82 /* Import NULL and EOF constants for error returns from stdio fns.
83 */
84 extern int unixnull[2], unixeof[2];
85
86 /* Tag( unixinitio )
87 */
unixinitio()88 unixinitio()
89 {
90 unixstdin = stdin;
91 unixstdout = stdout;
92 unixstderr = stderr;
93 unixnull[0] = (long) NULL;
94 unixnull[1] = (long) NULL;
95 unixeof[0] = EOF;
96 unixeof[1] = EOF;
97 unixtty = fopen("/dev/tty", "r");
98 }
99
100 /* Tag( unixputc )
101 * Used by kernel routines that write to the console
102 */
unixputc(c)103 unixputc(c)
104 char c;
105 {
106 fputc(c, stdout);
107 }
108
109 /* Tag( unixputs )
110 */
unixputs(str)111 unixputs(str)
112 char *str;
113 {
114 fputs(str, stdout);
115 }
116
117 /* Tag( unixputn )
118 */
unixputn(n)119 unixputn(n)
120 long long n;
121 {
122 fprintf(stdout, "%llx", n);
123 }
124
125 /* Tag( unixcleario )
126 */
unixcleario()127 unixcleario()
128 {
129 unixinitio();
130
131 #ifndef LINUX
132 /* set the stdin, stdout and stderr buffers to be empty */
133 stdin->_cnt = 0;
134 stdin->_ptr = stdin->_base;
135 stdout->_cnt = 0;
136 stdout->_ptr = stdout->_base;
137 stderr->_cnt = 0;
138 stderr->_ptr = stderr->_base;
139 #endif
140
141 }
142
143 /* The function expand_file_name is used to expand shell variable references
144 and ~ notation for directories in file names before calling fopen.
145 This eliminates the need for the HPUX-PATH and VAX-PATH kludges that were
146 once required, however, such a mechanism may still be used to override the
147 values supplied by the shell environment. The file name is first copied
148 to a temporary copy because the parsing algorithm must write into the
149 string (this may change). For now, the maximum string length supported
150 is 255 characters, but no checking is done to see if this is exceeded.
151 $ and ~ variables are expanded in one pass by calling the functions getenv,
152 getuid, getpwuid, and getpwnam. If any expansion fails, the original
153 string is returned.
154 */
155
156 #include <pwd.h>
157 struct passwd *getpwuid();
158 struct passwd *getpwnam();
159 char *getenv();
160 char collect[255], copy[255]; /* Made global so it won't be overwritten
161 Used to be local to expand_file_name */
162
163 /* Tag( expand_file_name )
164 */
expand_file_name(fname)165 char *expand_file_name(fname)
166 char *fname;
167 {
168 register char *c, *t, *e, *s, save;
169 struct passwd *p;
170 register int tilde;
171 c = copy;
172 s = fname;
173 while (*c++ = *s++);
174 s = copy;
175 c = collect;
176 *c = '\0';
177 while (*s)
178 {
179 if ((tilde = (*s == '~')) || (*s == '$'))
180 {
181 for (e = ++s; (*e != '/' && *e != '\0' && *e != '$'); e++)
182 ;
183 t = 0; /* default initialization */
184 if (e == s)
185 {
186 if (tilde) t = ((getpwuid(getuid())) -> pw_dir);
187 }
188 else
189 {
190 save = *e;
191 *e = '\0';
192 if (tilde)
193 {
194 if (p = getpwnam(s)) t = (p -> pw_dir);
195 }
196 else
197 t = getenv(s);
198 *e = save;
199 s = e;
200 }
201 if (t)
202 while (*c++ = *t++)
203 ;
204 else
205 return(fname); /* name not found, just return original fname */
206 c--;
207 }
208 for (; (*s != '\0' && *s != '$'); *c++ = *s++)
209 ;
210 *c = '\0';
211 }
212 return (collect);
213 }
214
215 extern int errno;
216
unixopen(filename,type)217 FILE* unixopen(filename, type)
218 char *filename, *type;
219 {
220 FILE* fptr;
221
222 fptr = fopen(expand_file_name(filename), type);
223 return(fptr);
224 }
225
unixcd(filename)226 unixcd(filename)
227 char *filename;
228 {
229 chdir(expand_file_name(filename));
230 }
231
unixfclose(ix)232 unixfclose (ix)
233 FILE* ix;
234
235 { fclose (ix); }
236
external_system(command)237 external_system(command)
238 char *command;
239 {
240 int value;
241 value = system(command);
242 return(value);
243 }
244
245 /* Tag( external_exit )
246 */
external_exit(status)247 external_exit(status)
248 int status;
249 {
250 exit(status);
251 }
252
253 char *static_argv[20]; /* static place to hold argv so it doesn't get gc'd */
254
copy_argv(argc,argv)255 copy_argv(argc,argv) /* copy argv into static space. */
256 int argc;
257 char *argv[];
258 {
259 int i;
260
261 for (i=0; i < argc; i++)
262 static_argv[i]=argv[i];
263
264 return((long)static_argv);
265 }
xgetw(f)266 long long xgetw (f)
267 FILE* f;
268 { long long a1,a2;
269
270 a1 = (long long) getw(f);
271 a2 = (long long) getw(f);
272 return (a2 << 32 | a1);
273 }
274
275
276