From: Thadeu Lima de Souza Cascardo Date: Sat, 10 Dec 2016 13:22:09 +0000 (-0200) Subject: dumpfile: joga o conteúdo de um arquivo em um fd X-Git-Url: http://git.cascardo.eti.br/?p=cascardo%2Fdeclara.git;a=commitdiff_plain;h=d2700053d6a0da8bf20a5fe6c4090e041b832b71 dumpfile: joga o conteúdo de um arquivo em um fd 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. --- 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