fileio.c revision 768100efd57ffbbefe9beaa33d1dd5ecc7f69173
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers This file is part of systemd.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers Copyright 2010 Lennart Poettering
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers systemd is free software; you can redistribute it and/or modify it
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers under the terms of the GNU Lesser General Public License as published by
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers the Free Software Foundation; either version 2.1 of the License, or
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers (at your option) any later version.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers systemd is distributed in the hope that it will be useful, but
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers Lesser General Public License for more details.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers You should have received a copy of the GNU Lesser General Public License
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sieversint write_string_file(const char *fn, const char *line) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sieversint write_string_file_atomic(const char *fn, const char *line) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sieversint read_one_line_file(const char *fn, char **line) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers if (!fgets(t, sizeof(t), f)) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sieversint read_full_file(const char *fn, char **contents, size_t *size) {
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers /* Safety check */
c51d84dc09476d9c06b8aac726220bf3c7d62e8dKay Sievers if (k <= 0) {
if (ferror(f))
return -errno;
return -E2BIG;
buf[l] = 0;
if (size)
*size = l;
static int parse_env_file_internal(
const char *fname,
const char *newline,
void *userdata) {
KEY,
for (p = contents; *p; p++) {
switch (state) {
case PRE_KEY:
r = -ENOMEM;
goto fail;
case KEY:
n_key = 0;
r = -ENOMEM;
goto fail;
case PRE_EQUAL:
n_key = 0;
n_key = 0;
case PRE_VALUE:
if (value)
goto fail;
n_key = 0;
r = -ENOMEM;
goto fail;
case VALUE:
if (value)
goto fail;
n_key = 0;
r = -ENOMEM;
goto fail;
case VALUE_ESCAPE:
r = -ENOMEM;
goto fail;
case SINGLE_QUOTE_VALUE:
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;
fputs(v, f);
fputc(*p, f);
fputs(p, f);
errno = 0;
STRV_FOREACH(i, l)
write_env_var(f, *i);
fflush(f);
if (ferror(f)) {
if (errno > 0)
r = -errno;
r = -EIO;
r = -errno;
unlink(p);