aboutsummaryrefslogtreecommitdiff
path: root/unix/boot/generic.new/chario.c
blob: 09b46e400014cf7bb8add55161d4f9dcfd6321d0 (plain) (blame)
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
/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


/*
 * OS Character I/O.  This set of routines are provided as a workaround in
 * the event that the host system cannot execute FTELL/FSEEK reliably (VMS/C
 * could not).  The idea here is to keep track of the character offset from
 * the beginning of the file.  K_FTELL returns the character offset.  K_FSEEK
 * rewinds the file and reads characters forward to the indicated offset.
 * K_GETC keeps a count of the file position.  (the k_ stands for kludge).
 */

extern	int debug;

struct context {
	FILE	*fp;			/* file descriptor	*/
	long	fpos;			/* saved file pointer	*/
	char	fname[512];		/* file being scanned	*/
};

FILE *
k_fopen (fname, mode)
char	*fname;
char	*mode;
{
	register struct context *cx;
	register FILE	*fp;

	if ((fp = fopen (fname, mode)) == NULL)
	    return (NULL);

	cx = (struct context *) malloc (sizeof(struct context));
	strcpy (cx->fname, fname);
	cx->fpos = 0;
	cx->fp = fp;

	return ((FILE *)cx);
}


int
k_fclose (cx_i)
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	int	status;

	status = fclose (cx->fp);
	free (cx);

	return (status);
}

#ifdef vms

int
k_getc (cx_i)
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	register int	ch;

	cx->fpos++;
	if (debug > 3) {
	    if ((ch = getc (cx->fp)) > 0)
		printf ("%5d %03o %c\n", cx->fpos, ch, ch > 040 ? ch : 040);
	    return (ch);
	} else
	    return (getc (cx->fp));
}

char *
k_fgets (obuf, maxch, cx_i)
char	*obuf;
int	maxch;
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	register int	ch, n;
	register char	*op;

	for (op=obuf, n=maxch;  --n >= 0;  )
	    if ((ch = k_getc(cx)) < 0)
		return (NULL);
	    else {
		*op++ = ch;
		if (ch == '\n')
		    break;
	    }

	return (obuf);
}

seek
k_fseek (cx_i, offset, type)
FILE	*cx_i;
long	offset;
int	type;
{
	register struct context *cx = (struct context *)cx_i;
	register FILE	*fp = cx->fp;
	register int	ch;

	if (debug > 1)
	    printf ("seek (%s, %ld, %d)\n", cx->fname, offset, type);
	    
	if (type == 0) {
	    fseek (fp, 0L, 0);
	    cx->fpos = 0;

	    while (cx->fpos < offset && (ch = getc(fp)) != EOF) {
		if (debug > 1)
		    fputc (ch, stdout);
		cx->fpos++;
	    }

	    if (debug > 1)
		printf ("[]\n");

	    return (0);
	}

	if (fseek (fp, offset, type) == -1)
	    return (-1);
	else {
	    cx->fpos = ftell (fp);
	    return (0);
	}
}

long
k_ftell (cx_i)
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;

	if (debug > 1) {
	    printf ("ftell returns %d\n", cx->fpos);
	    fflush (stdout);
	}

	return (cx->fpos);
}

#else

int
k_getc (cx_i)
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	return (getc (cx->fp));
}

char *
k_fgets (op, maxch, cx_i)
char	*op;
int	maxch;
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	return (fgets (op, maxch, cx->fp));
}

int
k_fseek (cx_i, offset, type)
FILE	*cx_i;
long	offset;
int	type;
{
	register struct context *cx = (struct context *)cx_i;
	return (fseek (cx->fp, offset, type));
}

int
k_ftell (cx_i)
FILE	*cx_i;
{
	register struct context *cx = (struct context *)cx_i;
	return (ftell (cx->fp));
}

#endif