-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexp_binary_pipe.c
92 lines (79 loc) · 1.91 KB
/
sexp_binary_pipe.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
88
89
90
91
92
#include <stdio.h>
#include <stdint.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#ifdef __BORLANDC__
#define _setmode setmode
#endif
#include <sexp.h>
#include <sexp_binary_read.h>
#include <sexp_binary_write.h>
#ifndef _WIN32
static void *binary_stdio(void)
{
if (isatty(fileno(stdin))) {
return "standard input is a terminal";
}
if (isatty(fileno(stdout))) {
return "standard output is a terminal";
}
return 0;
}
#endif
#ifdef _WIN32
static void *binary_stdio(void)
{
if (_isatty(_fileno(stdin))) {
return "standard input is a terminal";
}
if (_isatty(_fileno(stdout))) {
return "standard output is a terminal";
}
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stdout), _O_BINARY);
return 0;
}
#endif
static void *read_from_file(void *file_void, void *bytes, size_t nbyte)
{
FILE *file = file_void;
if (fread(bytes, 1, nbyte, file) == nbyte) {
return 0;
}
return ferror(file) ? "read error" : "did not read enough data";
}
static void *write_to_file(void *file_void, void *bytes, size_t nbyte)
{
FILE *file = file_void;
if (fwrite(bytes, 1, nbyte, file) == nbyte) {
return 0;
}
return ferror(file) ? "write error" : "did not write enough data";
}
static void *flush_file(void *file)
{
fflush((FILE *)file);
return ferror(file) ? "write error" : 0;
}
void *sexp_binary_pipe(struct sexp_binary_read **out_rd,
struct sexp_binary_write **out_wr)
{
void *error;
if ((error = binary_stdio())) {
return error;
}
if (!(*out_rd = sexp_binary_read_new(read_from_file, stdin))) {
return "out of memory";
}
if (!(*out_wr =
sexp_binary_write_new(write_to_file, flush_file, stdout))) {
sexp_binary_read_free(*out_rd);
return "out of memory";
}
return 0;
}