From d2700053d6a0da8bf20a5fe6c4090e041b832b71 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 10 Dec 2016 11:22:09 -0200 Subject: [PATCH] =?utf8?q?dumpfile:=20joga=20o=20conte=C3=BAdo=20de=20um?= =?utf8?q?=20arquivo=20em=20um=20fd?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit A função dumpfile lê o conteúdo de um arquivo e o escreve em um descritor, que pode ser a saída padrão, por exemplo. --- lib/util.c | 20 +++++++++++++++++++- lib/util.h | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/util.c b/lib/util.c index 90ca426..eb2ee8e 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Thadeu Lima de Souza Cascardo + * Copyright (C) 2015-2016 Thadeu Lima de Souza Cascardo * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,9 @@ #include #include +#include +#include + int set_llong(char *str, long long *val) { char *end = NULL; @@ -56,3 +59,18 @@ int set_string(char **args, int argc, char **str) return -errno; return 0; } + +int dumpfile(int fd, char *filename) +{ + int inp; + char buffer[256]; + int r; + inp = open(filename, O_RDONLY); + if (inp < 0) + return -errno; + while ((r = read(inp, buffer, sizeof(buffer))) > 0) { + write(fd, buffer, r); + } + close(inp); + return r; +} diff --git a/lib/util.h b/lib/util.h index 754f5de..255a329 100644 --- a/lib/util.h +++ b/lib/util.h @@ -74,4 +74,6 @@ static inline int centavos(long long val) #define FMT_R "R$ %lld,%02d" #define R(l) reais(l), centavos(l) +int dumpfile(int fd, char *filename); + #endif -- 2.20.1