fileio.c revision f73141d7657b3f60b8669bc8386413d8a8a372c6
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering This file is part of systemd.
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering Copyright 2010 Lennart Poettering
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering (at your option) any later version.
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering systemd is distributed in the hope that it will be useful, but
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering Lesser General Public License for more details.
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1c7dd82563ff2e71a067aea20d2acb2d0553644bLennart Poetteringint write_string_file(const char *fn, const char *line) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poetteringint write_string_file_atomic(const char *fn, const char *line) {
1c7dd82563ff2e71a067aea20d2acb2d0553644bLennart Poetteringint read_one_line_file(const char *fn, char **line) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!fgets(t, sizeof(t), f)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poetteringint read_full_file(const char *fn, char **contents, size_t *size) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering /* Safety check */
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering n = st.st_size > 0 ? st.st_size : LINE_MAX;
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering /* Safety check */
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering int (*push) (const char *key, char *value, void *userdata),
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering _cleanup_free_ char *contents = NULL, *key = NULL;
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering size_t key_alloc = 0, n_key = 0, value_alloc = 0, n_value = 0, last_whitespace = (size_t) -1;
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering r = read_full_file(fname, &contents, NULL);
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering for (p = contents; *p; p++) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering else if (c == '=')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!greedy_realloc((void**) &key, &key_alloc, n_key+2)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering } else if (c == '=')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering } else if (c == '\'')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering else if (c == '\"')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering else if (c == '\\')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering /* Chomp off trailing whitespace */
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering } else if (c == '\\') {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering /* Escaped newlines we eat up entirely */
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (!greedy_realloc((void**) &value, &value_alloc, n_value+2)) {
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering if (c == '\'')
edce2aed3aa93b84f7b4c70412bdb665da2977b0Lennart Poettering else if (c == '\\')
r = -ENOMEM;
goto fail;
r = -ENOMEM;
goto fail;
case DOUBLE_QUOTE_VALUE:
r = -ENOMEM;
goto fail;
r = -ENOMEM;
goto fail;
case COMMENT:
case COMMENT_ESCAPE:
if (value)
goto fail;
fail:
free(*v);
*v = value;
int parse_env_file(
const char *fname,
const char *newline, ...) {
if (!newline)
char ***m = userdata;
return -ENOMEM;
r = strv_push(m, p);
free(p);
char **m = NULL;
if (!newline)
strv_free(m);
*rl = m;
errno = 0;
STRV_FOREACH(i, l) {
fputs(*i, f);
fflush(f);
if (ferror(f)) {
if (errno > 0)
r = -errno;
r = -EIO;
r = -errno;
unlink(p);