-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathluc.cc
More file actions
106 lines (87 loc) · 1.82 KB
/
luc.cc
File metadata and controls
106 lines (87 loc) · 1.82 KB
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
extern "C" {
void luc_io_init (void);
void luc_snapin (const char*, void*, off_t);
void luc_snapout (const char*, void*, off_t);
void luc_stat (const char*, off_t*);
}
using namespace std;
void
luc_io_init (void)
{
}
static void
read_in (const char *fname, void* buf, off_t sz)
{
char * tmpbuf = (char *) buf;
int fd;
fd = open (fname, O_RDONLY);
if (fd < 0) {
fprintf (stderr, "Failed to open %s for reading: %s\n",
fname, strerror (errno));
abort ();
}
off_t bytes_read = 0;
while (bytes_read < sz) {
int last = read(fd, tmpbuf, sz);
bytes_read += last;
tmpbuf += last;
}
if (bytes_read != sz)
{
fprintf (stderr, "Failed to read %ld octets from %s: %s\n",
(long)sz, fname, strerror (errno));
abort ();
}
close (fd);
}
void
luc_snapin (const char *fname, void* buf, off_t sz)
{
read_in (fname, buf, sz);
}
static void
write_out (const char *fname, void* buf, off_t sz)
{
int fd;
fd = open (fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (fd < 0) {
fprintf (stderr, "Failed to open %s for writing: %s\n",
fname, strerror (errno));
abort ();
}
if (write (fd, buf, sz) < sz) {
fprintf (stderr, "Failed to write %ld octets to %s: %s\n",
(long)sz, fname, strerror (errno));
abort ();
}
close (fd);
}
void
luc_snapout (const char *fname, void* buf, off_t sz)
{
write_out (fname, buf, sz);
}
static void
stat_fname (const char *fname, off_t *sz)
{
struct stat st;
if (stat (fname, &st)) {
fprintf (stderr, "Failed to stat %s: %s\n",
fname, strerror (errno));
abort ();
}
*sz = st.st_size;
}
void
luc_stat (const char *fname, off_t *sz)
{
stat_fname (fname, sz);
}