-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileio.c
48 lines (41 loc) · 878 Bytes
/
fileio.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
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "fileio.h"
/*
* The function name 'nsa' means 'None Seekable'.
* But it can be used for seekable file too. :-)
* Why did I use the name? Because the function read the
* whole file to a buffer which alloced at runtime
* so that it was usually used for noneseekable files.
*/
ssize_t readnsa(int fd, char** ptr)
{
ssize_t nread, n;
char* buffer;
buffer = 0;
nread = 0;
n = 0;
while (1) {
buffer = realloc(buffer, nread + 1024);
if (!buffer) {
errno = ENOMEM;
return -1;
}
if ((n = read(fd, buffer + nread, 1024)) < 0) {
free(buffer);
return -1;
} else if (n == 0) {
break; /* EOF */
}
nread += n;
*ptr = buffer;
}
return nread;
}