diff options
author | midipix <writeonce@midipix.org> | 2015-12-19 08:22:04 -0500 |
---|---|---|
committer | midipix <writeonce@midipix.org> | 2016-11-11 00:22:29 -0500 |
commit | 14073fe30b3cb3de5bfa152083b1f24b943fba1d (patch) | |
tree | ef948d3fd176fa240ca47c073ff857e4b0d41897 /src/driver | |
parent | c19325c2855fc29fcf05c57e537e14ec342b179c (diff) | |
download | mdso-14073fe30b3cb3de5bfa152083b1f24b943fba1d.tar.bz2 mdso-14073fe30b3cb3de5bfa152083b1f24b943fba1d.tar.xz |
driver: mdso_stdin_to_tmp(): initial implementation.
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/mdso_unit_ctx.c | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/src/driver/mdso_unit_ctx.c b/src/driver/mdso_unit_ctx.c index 870e060..d955f7f 100644 --- a/src/driver/mdso_unit_ctx.c +++ b/src/driver/mdso_unit_ctx.c @@ -8,6 +8,8 @@ #include <stddef.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <errno.h> #include <sys/mman.h> #include <mdso/mdso.h> @@ -23,18 +25,62 @@ static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status) return status; } +static FILE * mdso_stdin_to_tmp(void) +{ + FILE * ftmp; + char buf[4096]; + ssize_t nread; + int ret; + + if (!(ftmp = tmpfile())) + return 0; + + nread = read(0,buf,sizeof(buf)-1); + + while (nread) { + if (nread > 0) { + buf[nread] = '\0'; + ret = fputs(buf,ftmp); + } else + ret = (errno == EINTR) ? 0 : -1; + + if (ret < 0) { + fclose(ftmp); + return 0; + } + + nread = read(0,buf,sizeof(buf)-1); + } + + return ftmp; +} + int mdso_get_unit_ctx( const struct mdso_driver_ctx * dctx, const char * path, struct mdso_unit_ctx ** pctx) { struct mdso_unit_ctx_impl * ctx; + FILE * ftmp; + int fd; if (!dctx || !(ctx = calloc(sizeof(*ctx),1))) return -1; - if (mdso_map_input(-1,path,PROT_READ,&ctx->map)) + if (strcmp(path,"-")) + fd = -1; + else if (!(ftmp = mdso_stdin_to_tmp())) + return mdso_free_unit_ctx_impl(ctx,-1); + else if ((fd = dup(fileno(ftmp))) < 0) return mdso_free_unit_ctx_impl(ctx,-1); + else + fclose(ftmp); + + if (mdso_map_input(fd,path,PROT_READ,&ctx->map)) + return mdso_free_unit_ctx_impl(ctx,-1); + + if (fd > 0) + close(fd); memcpy(&ctx->cctx,dctx->cctx, sizeof(ctx->cctx)); |