Skip to content

Instantly share code, notes, and snippets.

@philshafer
Created April 2, 2019 02:03
Show Gist options
  • Save philshafer/95fc4ab814091529fc0a132a49130d90 to your computer and use it in GitHub Desktop.
Save philshafer/95fc4ab814091529fc0a132a49130d90 to your computer and use it in GitHub Desktop.
huge-string.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <libxo/xo.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int id[CTL_MAXNAME];
size_t idlevel= CTL_MAXNAME;
void *value;
size_t valuelen;
int fd, rc;
atexit(xo_finish_atexit);
xo_set_flags(NULL, XOF_FLUSH);
argc = xo_parse_args(argc, argv);
if (argc < 0)
exit(EXIT_FAILURE);
if (argc < 1)
xo_err(1, "missing name");
fd = open(argv[1], O_RDONLY);
if (fd < 0)
xo_err(1, "could not open: %s", argv[1]);
struct stat st;
if (fstat(fd, &st) < 0)
xo_err(1, "stat failed: %m");
valuelen = st.st_size;
value = malloc(valuelen);
if (value == NULL)
xo_err(1, "malloc error");
rc = read(fd, value, valuelen);
if (rc < 0)
xo_err(1, "read failed");
xo_emit("{:valuelen/%lu}", valuelen);
xo_emit("{:value/%s}", (char *) value);
xo_emit("{:valuelen/%lu}", valuelen);
xo_emit("{L:\n}");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment