aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c45
1 files changed, 44 insertions, 1 deletions
diff --git a/src/util.c b/src/util.c
index bada5fd..ecd3173 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1,5 +1,5 @@
/* This file is part of Eclat.
- Copyright (C) 2012-2014 Sergey Poznyakoff.
+ Copyright (C) 2012-2015 Sergey Poznyakoff.
Eclat is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -18,6 +18,7 @@
#include "json.h"
#include <termios.h>
#include <sys/ioctl.h>
+#include <sys/stat.h>
int translation_enabled;
char *custom_map;
@@ -367,6 +368,48 @@ canonattrname(char **attrs, const char *arg, char *delim, size_t *plen)
}
return NULL;
}
+
+char *
+read_file(const char *file)
+{
+ char *buf = NULL;
+
+ if (strcmp(file, "-") == 0) {
+ struct grecs_txtacc *acc = grecs_txtacc_create();
+ char inbuf[4096];
+ size_t n;
+
+ while (n = fread(inbuf, 1, sizeof(inbuf), stdin))
+ grecs_txtacc_grow(acc, inbuf, n);
+ grecs_txtacc_grow_char(acc, 0);
+ if (ferror(stdin))
+ die(EX_NOINPUT, "read error");
+ grecs_txtacc_grow_char(acc, 0);
+ buf = grecs_txtacc_finish(acc, 1);
+ grecs_txtacc_free(acc);
+ } else {
+ struct stat st;
+ FILE *fp;
+
+ if (stat(file, &st))
+ die(EX_USAGE, "cannot stat file %s: %s", file,
+ strerror(errno));
+
+ /* FIXME: Use limits.h to check st.st_size */
+ buf = grecs_malloc(st.st_size+1);
+ fp = fopen(file, "r");
+ if (!fp)
+ die(EX_NOINPUT, "cannot open file %s: %s", file,
+ strerror(errno));
+ if (fread(buf, st.st_size, 1, fp) != 1)
+ die(EX_NOINPUT, "error reading from %s: %s", file,
+ strerror(errno));
+ fclose(fp);
+ buf[st.st_size] = 0;
+ }
+
+ return buf;
+}
char *instance_store_base_url = "http://169.254.169.254/latest";
unsigned short instance_store_port;

Return to:

Send suggestions and report system problems to the System administrator.