run_gzip.cxx 1.74 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"
21 22
#include "fs/io/GzipOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
23
#include "system/Error.hxx"
24
#include "Log.hxx"
25 26 27 28 29

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

30 31
static void
Copy(OutputStream &dest, int src)
32 33 34 35 36
{
	while (true) {
		char buffer[4096];
		ssize_t nbytes = read(src, buffer, sizeof(buffer));
		if (nbytes <= 0) {
37 38 39 40
			if (nbytes < 0)
				throw MakeErrno("read() failed");

			return;
41 42
		}

43
		dest.Write(buffer, nbytes);
44 45 46
	}
}

47 48
static void
CopyGzip(OutputStream &_dest, int src)
49
{
50
	GzipOutputStream dest(_dest);
51
	Copy(dest, src);
52
	dest.Flush();
53 54
}

55 56
static void
CopyGzip(FILE *_dest, int src)
57 58
{
	StdioOutputStream dest(_dest);
59
	CopyGzip(dest, src);
60 61 62 63 64 65 66 67 68 69
}

int
main(int argc, gcc_unused char **argv)
{
	if (argc != 1) {
		fprintf(stderr, "Usage: run_gzip\n");
		return EXIT_FAILURE;
	}

70
	try {
71
		CopyGzip(stdout, STDIN_FILENO);
72 73 74
		return EXIT_SUCCESS;
	} catch (const std::exception &e) {
		LogError(e);
75 76 77
		return EXIT_FAILURE;
	}
}