]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/get_file_lines.c
2f27a012dca5431a44c240a90c16307127ce9cbf
[ccan] / tools / ccanlint / get_file_lines.c
1 #include "get_file_lines.h"
2 #include <talloc/talloc.h>
3 #include <string/string.h>
4 #include <noerr/noerr.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <err.h>
10 #include <dirent.h>
11
12 static void *grab_fd(const void *ctx, int fd)
13 {
14         int ret;
15         unsigned int max = 16384, size = 0;
16         char *buffer;
17
18         buffer = talloc_array(ctx, char, max+1);
19         while ((ret = read(fd, buffer + size, max - size)) > 0) {
20                 size += ret;
21                 if (size == max)
22                         buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
23         }
24         if (ret < 0) {
25                 talloc_free(buffer);
26                 buffer = NULL;
27         } else
28                 buffer[size] = '\0';
29
30         return buffer;
31 }
32
33 /* This version adds one byte (for nul term) */
34 static void *grab_file(const void *ctx, const char *filename)
35 {
36         int fd;
37         char *buffer;
38
39         if (streq(filename, "-"))
40                 fd = dup(STDIN_FILENO);
41         else
42                 fd = open(filename, O_RDONLY, 0);
43
44         if (fd < 0)
45                 return NULL;
46
47         buffer = grab_fd(ctx, fd);
48         close_noerr(fd);
49         return buffer;
50 }
51
52 char **get_file_lines(void *ctx, const char *name, unsigned int *num_lines)
53 {
54         char *buffer = grab_file(ctx, name);
55
56         if (!buffer)
57                 err(1, "Getting file %s", name);
58
59         return strsplit(buffer, buffer, "\n", num_lines);
60 }