-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend-file-slice.c
64 lines (55 loc) · 1.32 KB
/
send-file-slice.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
/*
* Synopsis:
* Inclusively slice a chunk of a seekable file onto standard output.
* Usage:
* slice-file <start-offset> <end-offset> <file-path>
* Exit Status:
* 0 ok, wrote <stop-offset> - <start-offset> bytes to stdout
* 1 file length < stop-offset, no bytes written
* 5 unexpected error.
* Note:
* An empty stream exits 0. Why?
*/
#include <sys/errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "jmscott/libjmscott.h"
extern int errno;
char *jmscott_progname = "send-file-slice";
static void
die(char *msg)
{
jmscott_die(5, msg);
}
static void
die2(char *msg1, char *msg2)
{
jmscott_die2(5, msg1, msg2);
}
int main(int argc, char **argv)
{
size_t start, stop;
char *err;
argc--;
if (argc != 3)
die("wrong count of CLI args: expected 3");
if ((err = jmscott_a2size_t(argv[1], &start)))
die2("a2size_t(start) failed", err);
if ((err = jmscott_a2size_t(argv[2], &stop)))
die2("a2size_t(stop) failed", err);
if (start > stop)
die("start > stop");
if (!argv[1][0])
die("empty file path");
int in;
in = jmscott_open(argv[3], O_RDONLY, 0);
if (in < 0)
die2("open(input) failed", strerror(errno));
err = jmscott_send_file(in, 1, (long long *)0);
if (err)
die2("sendfile() failed", err);
if (jmscott_close(in))
die2("close(in) failed", strerror(errno));
_exit(0);
}