forked from jleffler/scc-snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterio.c
87 lines (74 loc) · 1.98 KB
/
filterio.c
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
/*
@(#)File: $RCSfile: filterio.c,v $
@(#)Version: $Revision: 2019.2 $
@(#)Last changed: $Date: 2019/08/16 05:47:20 $
@(#)Purpose: Perform standardized I/O error check for filter programs
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 2003-2019
@(#)Product: SCC Version 8.0.3 (2022-05-30)
*/
/*TABSTOP=4*/
#include "filter.h"
#include "stderr.h"
static int numfiles = 0;
#ifndef lint
/* Prevent over-aggressive optimizers from eliminating ID string */
extern const char jlss_id_filterio_c[];
const char jlss_id_filterio_c[] = "@(#)$Id: filterio.c,v 2019.2 2019/08/16 05:47:20 jleffler Exp $";
#endif /* lint */
/* filter_numfiles() - return number of files to be processed */
int filter_numfiles(void)
{
return numfiles;
}
/* filter_setnumfiles() - set number of files to be processed */
/*
** Only set number of files if not overridden by prior
** filter_setnumfiles(1) or filter_setnumfiles(2)
*/
void filter_setnumfiles(int num)
{
if (numfiles == 0)
numfiles = num;
}
/* filter_io_check() - did an input or output error occur? */
int filter_io_check(FILE *ifp, const char *ifn, FILE *ofp)
{
int rc = 0;
if (ferror(ifp))
{
err_sysrem("read error on file %s\n", ifn);
rc = -1;
}
if (ferror(ofp))
{
err_sysrem("write error on output file\n");
rc = -1;
}
return(rc);
}
#ifdef TEST
#include <stdlib.h>
static int cat(FILE *ifp, const char *ifn, FILE *ofp)
{
char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), ifp)) != 0)
{
if (fwrite(buffer, sizeof(char), nbytes, ofp) != nbytes)
{
err_syserr("write failed: ");
/*NOTREACHED*/
}
}
return(filter_io_check(ifp, ifn, ofp));
}
int main(int argc, char **argv)
{
int rc = EXIT_SUCCESS;
err_setarg0(argv[0]);
if (filter_output(argc, argv, 1, cat) != 0)
rc = EXIT_FAILURE;
return(rc);
}
#endif /* TEST */