]> git.ozlabs.org Git - ccan/blob - ccan/grab_file/grab_file.h
grab_file: don't use str_talloc for tests.
[ccan] / ccan / grab_file / grab_file.h
1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_GRAB_FILE_H
3 #define CCAN_GRAB_FILE_H
4 #include <stdio.h> // For size_t
5
6 /**
7  * grab_fd - read all of a file descriptor into memory
8  * @ctx: the context to tallocate from (often NULL)
9  * @fd: the file descriptor to read from
10  * @size: the (optional) size of the file
11  *
12  * This function reads from the given file descriptor until no more
13  * input is available.  The content is talloced off @ctx, and the size
14  * of the file places in @size if it's non-NULL.  For convenience, the
15  * byte after the end of the content will always be NUL.
16  *
17  * Example:
18  *      // Return the first line.
19  *      static char *read_stdin_firstline(void)
20  *      {
21  *              char *all, *nl;
22  *
23  *              all = grab_fd(NULL, 0, NULL);
24  *              if (!all)
25  *                      return NULL;
26  *              nl = strchr(all, '\n');
27  *              if (nl)
28  *                      *nl = '\0';
29  *              return all;
30  *      }
31  */
32 void *grab_fd(const void *ctx, int fd, size_t *size);
33
34 /**
35  * grab_file - read all of a file (or stdin) into memory
36  * @ctx: the context to tallocate from (often NULL)
37  * @filename: the file to read (NULL for stdin)
38  * @size: the (optional) size of the file
39  *
40  * This function reads from the given file until no more input is
41  * available.  The content is talloced off @ctx, and the size of the
42  * file places in @size if it's non-NULL.  For convenience, the byte
43  * after the end of the content will always be NUL.
44  *
45  * Example:
46  *      static char *read_file_firstline(const char *filename)
47  *      {
48  *              char *nl, *all;
49  *
50  *              all = grab_file(NULL, filename, NULL);
51  *              if (!all)
52  *                      return NULL;
53  *              nl = strchr(all, '\n');
54  *              if (nl)
55  *                      *nl = '\0';
56  *              return all;
57  *      }
58  */
59 void *grab_file(const void *ctx, const char *filename, size_t *size);
60 #endif /* CCAN_GRAB_FILE_H */