blob: 26c3cd0590826f8644b38f6ba97ca9f71c9cf3e9 (
plain) (
blame)
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
|
/*
* server/file.c
*/
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "file.h"
int file_open(char *filename)
{
int fd = open(filename, O_RDONLY);
if (fd == -1)
return -1;
struct stat st;
if (fstat(fd, &st) == -1)
return -1;
return fd;
}
int file_getsize(int fd, off_t *size)
{
*size = lseek64(fd, 0, SEEK_END);
if (*size == -1)
return -1;
return 0;
}
int file_read(int fd, void *buf, size_t size, off_t pos)
{
off_t newpos = lseek(fd, pos, SEEK_SET);
if (newpos == -1)
return -1;
size_t nleft = size;
ssize_t nread;
char *ptr = buf;
while (nleft > 0) {
if ((nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
continue;
return -1;
}
if (nread == 0) {
break;
}
nleft -= nread;
ptr += nread;
}
return 0;
}
|