cat.c revision e4603df5cf80bbd7a7d51fc66fa6c60e042423bc
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sd-journal.h"
#include "util.h"
static char *arg_identifier = NULL;
static int arg_priority = LOG_INFO;
static bool arg_level_prefix = true;
static void 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"
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case 't':
else {
if (!arg_identifier)
return log_oom();
}
break;
case 'p':
if (arg_priority < 0) {
log_error("Failed to parse priority value.");
return -EINVAL;
}
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;
}
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
log_open();
if (r <= 0)
goto finish;
if (fd < 0) {
r = fd;
goto finish;
}
goto finish;
}
if (fd >= 3)
safe_close(fd);
fd = -1;
else
r = -errno;
/* Let's try to restore a working stderr, so we can print the error message */
if (saved_stderr >= 0)
log_error_errno(r, "Failed to execute process: %m");
safe_close(fd);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}