-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtape-image.c
186 lines (160 loc) · 3.75 KB
/
tape-image.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
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
/* Copyright (C) 2022, 2023 Lars Brinkhoff <lars@nocrew.org>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "tape-image.h"
static int marks;
int
read_tape (const char *file)
{
int fd = open (file, O_RDONLY);
marks = 0;
return fd;
}
int
write_tape (const char *file)
{
int fd = open (file, O_WRONLY | O_CREAT, 0600);
marks = 0;
return fd;
}
int
rw_tape (const char *file)
{
int fd = open (file, O_RDWR | O_CREAT, 0600);
marks = 0;
return fd;
}
static size_t
read_reclen (int fd)
{
unsigned char size[4];
ssize_t n;
size_t m;
n = read (fd, size, 4);
if (n == -1) {
fprintf (stderr, "Read error: %s\n", strerror (errno));
return RECORD_ERR | errno;
} else if (n == 0) {
return RECORD_EOM;
} else if (n < 4) {
return RECORD_ERR;
}
m = size[0];
m |= (size_t)size[1] << 8;
m |= (size_t)size[2] << 16;
m |= (size_t)size[3] << 24;
return m;
}
size_t
read_record (int fd, void *buffer, size_t n)
{
char zero;
size_t n1, n3;
ssize_t n2;
n1 = read_reclen(fd);
if (n1 & RECORD_ERR)
return n1;
if (n1 == RECORD_MARK)
return n1;
if (n1 > n)
n1 = n;
n2 = read (fd, buffer, n1);
if (n2 == -1) {
fprintf (stderr, "Read error: %s\n", strerror (errno));
return RECORD_ERR | errno;
}
if (n1 & 1) {
n2 = read (fd, &zero, 1);
if (n2 == -1) {
fprintf (stderr, "Read error: %s\n", strerror (errno));
return RECORD_ERR | errno;
}
}
n3 = read_reclen(fd);
if (n3 & RECORD_ERR)
return n3;
if (n1 != n3)
return RECORD_ERR;
return n3;
}
static void
write_reclen (int fd, size_t n)
{
unsigned char size[4];
ssize_t m;
size[0] = n & 0377;
size[1] = (n >> 8) & 0377;
size[2] = (n >> 16) & 0377;
size[3] = (n >> 24) & 0377;
m = write (fd, size, 4);
if (m == -1)
fprintf (stderr, "Write error: %s\n", strerror (errno));
}
void
write_mark (int fd)
{
marks++;
write_reclen (fd, RECORD_MARK);
}
void
write_record (int fd, const void *buffer, size_t n)
{
char zero = 0;
int m;
n &= RECORD_LMASK;
if (n == 0)
{
fprintf (stderr, "Can't write empty record.\n");
return;
}
marks = 0;
write_reclen (fd, n);
m = write (fd, buffer, n);
if (m == -1)
fprintf (stderr, "Write error: %s\n", strerror (errno));
if (n & 1) {
m = write (fd, &zero, 1);
if (m == -1)
fprintf (stderr, "Write error: %s\n", strerror (errno));
}
write_reclen (fd, n);
}
void
write_eot (int fd)
{
int i;
for (i = marks; i < 2; i++)
write_mark (fd);
}
void
write_eom (int fd)
{
write_reclen (fd, RECORD_EOM);
}
void
write_error (int fd, unsigned error)
{
error &= RECORD_EMASK;
write_reclen (fd, error | RECORD_ERR);
}