cat.c revision 9aac0b2c19c558b853da7a6d67a0929b2d44dee4
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
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.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <getopt.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <systemd/sd-journal.h>
#include "util.h"
#include "build.h"
static char *arg_identifier = NULL;
static char arg_priority = LOG_INFO;
static bool arg_level_prefix = true;
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" -t --identifier=STRING Set syslog identifier\n"
" -p --priority=PRIORITY Set priority value (0..7)\n"
" --level-prefix=BOOL Control whether level prefix shall be parsed\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case 't':
else {
if (!arg_identifier) {
log_error("Out of memory.");
return -ENOMEM;
}
}
break;
case 'p':
if (arg_priority < 0) {
log_error("Failed to parse priority value.");
return arg_priority;
}
break;
case ARG_LEVEL_PREFIX: {
int k;
k = parse_boolean(optarg);
if (k < 0) {
log_error("Failed to parse level prefix value.");
return k;
}
arg_level_prefix = k;
break;
}
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
log_open();
if (r <= 0)
goto finish;
if (fd < 0) {
r = fd;
goto finish;
}
log_error("Failed to duplicate fd: %m");
r = -errno;
goto finish;
}
if (fd >= 3)
fd = -1;
else
r = -errno;
/* Let's try to restore a working stderr, so we can print the error message */
if (saved_stderr >= 0)
if (fd >= 0)
if (saved_stderr >= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}