forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.c
More file actions
303 lines (242 loc) · 4.76 KB
/
Copy pathio.c
File metadata and controls
303 lines (242 loc) · 4.76 KB
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
/*
* io.c for mdb
* all the i/o is here
* NB: Printf()
*/
#include "mdb.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include "proto.h"
#define OUTBUFSIZE 512
#define PAGESIZE 24
PRIVATE int forceupper = FALSE;
PRIVATE int someupper = FALSE;
PRIVATE int stringcount = 0;
PRIVATE char *string_ptr = NULL; /* stringptr ambiguous at 8th char */
PRIVATE char *stringstart = NULL;
PRIVATE char outbuf[OUTBUFSIZE];
PRIVATE FILE *cmdfile = stdin;
PRIVATE FILE *outfile = stdout;
PRIVATE FILE *logfile;
PRIVATE int lineno;
_PROTOTYPE( int _doprnt, (const char *format, va_list ap, FILE *stream ));
PUBLIC char *get_cmd(cbuf, csize)
char *cbuf;
int csize;
{
char *r;
fflush(stdout);
if( cmdfile == stdin && outfile == stdout )
printf("* ");
r = fgets(cbuf, csize, cmdfile);
if ( r == NULL && cmdfile != stdin ) {
cmdfile = stdin;
return get_cmd(cbuf, csize);
}
if ( logfile != NULL ) {
fprintf( logfile, "%s", cbuf );
lineno++;
}
return r;
}
PUBLIC void openin(s)
char *s;
{
char *t;
if ((t = strchr(s,'\n')) != NULL) *t = '\0';
if ((t = strchr(s,' ')) != NULL) *t = '\0';
cmdfile = fopen(s,"r");
if (cmdfile == NULL) {
Printf("Cannot open %s for input\n",s);
cmdfile = stdin;
}
}
/* Special version of printf
* really sprintf()
* from MINIX library
* followed by outstr()
*/
PUBLIC int Printf(const char *format, ...)
{
va_list ap;
int retval;
FILE tmp_stream;
va_start(ap, format);
tmp_stream._fd = -1;
tmp_stream._flags = _IOWRITE + _IONBF + _IOWRITING;
tmp_stream._buf = (unsigned char *) outbuf;
tmp_stream._ptr = (unsigned char *) outbuf;
tmp_stream._count = 512;
retval = _doprnt(format, ap, &tmp_stream);
putc('\0',&tmp_stream);
va_end(ap);
outstr(outbuf);
return retval;
}
/*
* Set logging options
*/
PUBLIC void logging( c, name )
int c;
char *name;
{
char *t;
if ( c == 'q' && logfile != NULL ) {
fclose(logfile);
return;
}
if ((t = strchr(name,'\n')) != NULL) *t = '\0';
if ((t = strchr(name,' ' )) != NULL) *t = '\0';
if ( logfile != NULL ) fclose(logfile);
if ( strlen(name) > 0 ) {
logfile = fopen(name,"w");
if (logfile == NULL) {
Printf("Cannot open %s for output\n",name);
return;
}
/* Close standard output file for L */
if ( c == 'L' ) {
fclose(outfile);
outfile = NULL;
}
}
else
/* Reset */
{
if ( logfile != NULL ) fclose(logfile);
outfile = stdout;
outbyte('\n');
}
}
/* Output system error string */
PUBLIC void do_error(m)
char *m;
{
outstr(m);
outstr(": ");
outstr(strerror(errno));
}
PUBLIC void closestring()
{
/* close string device */
stringcount = 0;
stringstart = string_ptr = NULL;
}
PUBLIC int mytolower(ch)
int ch;
{
/* convert char to lower case */
if (ch >= 'A' && ch <= 'Z')
ch += 'a' - 'A';
return ch;
}
PUBLIC void openstring(string)
char *string;
{
/* open string device */
stringcount = 0;
stringstart = string_ptr = string;
}
PUBLIC void outbyte(byte)
int byte;
{
/* print char to currently open output devices */
if (forceupper && byte >= 'a' && byte <= 'z')
byte += 'A' - 'a';
if (string_ptr != NULL)
{
if ((*string_ptr++ = byte) == '\t')
stringcount = 8 * (stringcount / 8 + 1);
else
++stringcount;
}
else
{
if ( paging && byte == '\n' ) {
lineno++;
if ( lineno >= PAGESIZE) {
if ( cmdfile == stdin ) {
printf("\nMore...any key to continue");
fgets( outbuf, OUTBUFSIZE-1, cmdfile );
}
}
lineno = 0;
}
if ( outfile != NULL )
putc(byte,outfile);
/* Do not log CR */
if ( logfile != NULL && byte != '\r' )
putc(byte,logfile);
}
}
PUBLIC void outcomma()
{
/* print comma */
outbyte(',');
}
PRIVATE char hexdigits[] = "0123456789ABCDEF";
PUBLIC void outh4(num)
unsigned num;
{
/* print 4 bits hex */
outbyte(hexdigits[num % 16]);
}
PUBLIC void outh8(num)
unsigned num;
{
/* print 8 bits hex */
outh4(num / 16);
outh4(num);
}
PUBLIC void outh16(num)
unsigned num;
{
/* print 16 bits hex */
outh8(num / 256);
outh8(num);
}
PUBLIC void outh32(num)
unsigned num;
{
/* print 32 bits hex */
outh16((u16_t) (num >> 16));
outh16((u16_t) num);
}
PUBLIC void outspace()
{
/* print space */
outbyte(' ');
}
PUBLIC void outstr(s)
register char *s;
{
/* print string */
while (*s)
outbyte(*s++);
}
PUBLIC void outtab()
{
/* print tab */
outbyte('\t');
}
PUBLIC void outustr(s)
register char *s;
{
/* print string, perhaps converting case to upper */
forceupper = someupper;
while (*s)
outbyte(*s++);
forceupper = FALSE;
}
PUBLIC int stringpos()
{
/* return current offset of string device */
return string_ptr - stringstart;
}
PUBLIC int stringtab()
{
/* return current "tab" spot of string device */
return stringcount;
}