aboutsummaryrefslogtreecommitdiff
path: root/titan.c
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2023-07-24 09:28:01 +0000
committerOmar Polo <op@omarpolo.com>2023-07-24 09:28:01 +0000
commit9888507cd4212b360fc15e4262e5fd95844efc7c (patch)
tree077833d704cfc90cf83543bce6f122eb2c1fadb8 /titan.c
parentbf9ca5e71c334c4d96466a3e6bee8b2dbde5c884 (diff)
read from stdin if no file is given
Diffstat (limited to 'titan.c')
-rw-r--r--titan.c53
1 files changed, 47 insertions, 6 deletions
diff --git a/titan.c b/titan.c
index 7300b94..f76deeb 100644
--- a/titan.c
+++ b/titan.c
@@ -148,11 +148,53 @@ iomux(struct tls *ctx, int fd, const char *in, size_t inlen, char *out,
return 0;
}
+static FILE *
+open_input_file(int argc, char **argv)
+{
+ FILE *fp;
+ char buf[BUFSIZ];
+ char sfn[22];
+ size_t r;
+ int fd;
+
+ if (argc > 1) {
+ if ((fp = fopen(argv[1], "r")) == NULL)
+ err(1, "can't open %s", argv[1]);
+ return fp;
+ }
+
+ strlcpy(sfn, "/tmp/titan.XXXXXXXXXX", sizeof(sfn));
+ if ((fd = mkstemp(sfn)) == -1 ||
+ (fp = fdopen(fd, "w+")) == NULL) {
+ warn("%s", sfn);
+ if (fd != -1) {
+ unlink(sfn);
+ close(fd);
+ }
+ errx(1, "can't create temp file");
+ }
+ unlink(sfn);
+
+ for (;;) {
+ r = fread(buf, 1, sizeof(buf), stdin);
+ if (r == 0)
+ break;
+ fwrite(buf, 1, r, fp);
+ }
+ if (ferror(fp))
+ err(1, "I/O error");
+
+ if (fseeko(fp, 0, SEEK_SET) == -1)
+ err(1, "fseeko");
+
+ return fp;
+}
+
static void __dead
usage(void)
{
fprintf(stderr,
- "usage: %s [-C cert] [-K key] [-m mime] [-t token] url file\n",
+ "usage: %s [-C cert] [-K key] [-m mime] [-t token] url [file]\n",
getprogname());
exit(1);
}
@@ -172,7 +214,7 @@ main(int argc, char **argv)
char *req;
int sock, ch;
- if (pledge("stdio rpath inet dns", NULL) == -1)
+ if (pledge("stdio rpath tmppath inet dns", NULL) == -1)
err(1, "pledge");
while ((ch = getopt(argc, argv, "C:K:m:t:")) != -1) {
@@ -199,13 +241,12 @@ main(int argc, char **argv)
if (cert != NULL && key == NULL)
key = cert;
- if (argc != 2)
+ if (argc > 2)
usage();
- if ((in = fopen(argv[1], "r")) == NULL)
- err(1, "can't open %s", argv[1]);
+ in = open_input_file(argc, argv);
- /* drop rpath */
+ /* drop rpath tmppath */
if (pledge("stdio inet dns", NULL) == -1)
err(1, "pledge");