summaryrefslogtreecommitdiff
path: root/dviware/quicspool/src/getc.c
blob: cf935e18bb342a71c86af04b652785e51d602845 (plain)
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
#ifndef lint
static char	*rcs = "$Header: getc.c,v 1.1 88/01/15 13:04:03 simpson Rel $";
#endif
/*
$Log:	getc.c,v $
 * Revision 1.1  88/01/15  13:04:03  simpson
 * initial release
 * 
 * Revision 0.1  87/12/11  18:30:55  simpson
 * beta test
 * 
*/

/* Secure getc(3) routines.  These routines provide a set of standard I/O
 * functions for reading data from files.  The routines are essentially
 * identical to their standard I/O equivalents except that these routines
 * check for EOF.  If EOF is encountered, a function determined by the user is
 * called.  This is useful if you are reading from a file, such as a TeX DVI
 * file or troff file, and you want to check to see if the file ends
 * prematurely without putting checks in every time you call getc(), gets(),
 * etc.  When reading a DVI file for example, you know when a file should end
 * by the context, so if it does end prematurely, you can call an error
 * routine.  The following functions are provided:
 *
 *	void seteoffunction(f)
 *	void (*f)();
 *
 * Sets the function to call if EOF is reached when reading with any of the
 * routines.  The function is assumed not to return although returning is
 * allowed.
 *
 *	int cgetc(f)
 *	FILE	*f;
 *
 * Just like getc(3) but it checks for EOF.  If EOF is encountered, the
 * function set in seteoffunction() is called.  (It is really more like
 * fgetc(3) since it is not a macro).
 *
 *	int cgetchar()
 *
 * Equivalent to cgetc(stdin).  It's not a macro since we would need an
 * include file.
 * 
 *	int cgetw(f)
 *	FILE *f;
 *
 * Just like getw(3) but is checks for EOF.  If EOF is encountered, the
 * function set in seteoffunction() is called.
 *
 *	char *cgets(s)
 *	char *s;
 *
 * Just like gets(3) except it checks for EOF.  This is a little lie.
 * Actually it only calls the function set in seteoffunction if it
 * encounters EOF on the first character read.  The rationale behind this is
 * that you probably want the last line of a file even if it doesn't end with
 * a newline.
 *
 *	char *cfgets(s, n, stream)
 *	char *s;
 *	int  n;
 *	FILE *stream;
 *
 * Just like fgets(3) except it checks for EOF.  Again, it only calls the
 * eof function if the first character read returns EOF.
 *
 *      int cfread(ptr, size, nitems, stream)
 *      char	*ptr;
 *	int	size;
 *	int	nitems;
 *	FILE	*stream;
 *
 * Just like fread(3) except it checks for EOF.  The size read must be the
 * size expected.
 */

#include <stdio.h>
#include <local/standard.h>

static void (*EOFFunction)();

void seteoffunction(f)
void (*f)();
{
    EOFFunction = f;
}

int cgetc(f)
FILE	*f;
{
    int c;

    if ((c = getc(f)) == EOF) {
	if (EOFFunction)
	    (*EOFFunction)();
    }
    else 
	return c;
    return EOF;	    /* Only if EOFFunction returns or is NULL */
}

int cgetchar()
{
    return cgetc(stdin);
}

int cgetw(f)
FILE	*f;
{
    int w;

    w = getw(f);
    if (feof(f)) {
	if (EOFFunction)
	    (*EOFFunction)();
    } else
	return w;
    return EOF;		/* Only if EOFFunction returns or is NULL */
}

char *cgets(s)
char *s;
{
    int	    c;
    char    *p = s;

    for (c = getchar(); c != '\n' && c != EOF; c = getchar())
	*p++ = c;
    if (c == EOF && p == s) {
	if (EOFFunction)
	    (*EOFFunction)();
    } else {
	*p = '\0';
	return s;
    }
    return NULL;	/* Only if EOFFunction returns or is NULL */
}

char *cfgets(s, n, stream)
char *s;
int  n;
FILE *stream;
{
    int	    c = '\0';
    char    *p = s, *strcat();

    while (p - s < n - 1 && (c = getc(stream)) != '\n' && c != EOF)
	*p++ = c;
    if (c == EOF && p == s) {
	if (EOFFunction)
	    (*EOFFunction)();
    }
    else {
	*p = '\0';
	if (c == '\n')
	    (void)strcat(s, "\n");
	return s;
    }
    return NULL;	/* Only if EOFFunction returns or is NULL */
}

int cfread(ptr, size, nitems, stream)
char	*ptr;
int	size;
int	nitems;
FILE	*stream;
{
    if (fread(ptr, size, nitems, stream) != nitems)
    	if (EOFFunction)
	    (*EOFFunction)();
    return nitems;
}