fileio.c revision 8333c77edf8fd1654cd96f3f6ee0f078dd64b58b
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 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 <unistd.h>
#include "fileio.h"
#include "util.h"
#include "strv.h"
if (!f)
return -errno;
errno = 0;
fputc('\n', f);
fflush(f);
if (ferror(f))
return 0;
}
_cleanup_free_ char *p = NULL;
int r;
r = fopen_temporary(fn, &f, &p);
if (r < 0)
return r;
errno = 0;
r = -errno;
goto finish;
}
fputc('\n', f);
fflush(f);
if (ferror(f))
else {
r = -errno;
else
r = 0;
}
if (r < 0)
unlink(p);
return r;
}
char t[LINE_MAX], *c;
if (!f)
return -errno;
if (!fgets(t, sizeof(t), f)) {
if (ferror(f))
t[0] = 0;
}
c = strdup(t);
if (!c)
return -ENOMEM;
truncate_nl(c);
*line = c;
return 0;
}
size_t n, l;
if (!f)
return -errno;
return -errno;
/* Safety check */
return -E2BIG;
l = 0;
for (;;) {
char *t;
size_t k;
if (!t)
return -ENOMEM;
buf = t;
if (k <= 0) {
if (ferror(f))
return -errno;
break;
}
l += k;
n *= 2;
/* Safety check */
if (n > 4*1024*1024)
return -E2BIG;
}
buf[l] = 0;
if (size)
*size = l;
return 0;
}
int parse_env_file(
const char *fname,
const char *separator, ...) {
int r = 0;
if (r < 0)
return r;
p = contents;
for (;;) {
p += strspn(p, WHITESPACE);
if (!*p)
break;
char **value;
size_t n;
char *v;
p[n] != '=')
continue;
p += n + 1;
if (n >= 2 &&
p[n-1] == p[0])
else
v = strndup(p, n);
if (!v) {
r = -ENOMEM;
goto fail;
}
if (v[0] == '\0') {
/* return empty value strings as NULL */
free(v);
v = NULL;
}
*value = v;
p += n;
r ++;
break;
}
}
if (!key)
}
fail:
return r;
}
_cleanup_fclose_ FILE *f;
_cleanup_strv_free_ char **m = NULL;
_cleanup_free_ char *c = NULL;
/* This reads an environment file, but will not complain about
* any invalid assignments, that needs to be done by the
* caller */
if (!f)
return -errno;
while (!feof(f)) {
if (!fgets(l, sizeof(l), f)) {
if (ferror(f))
return -errno;
/* The previous line was a continuation line?
* Let's process it now, before we leave the
* loop */
if (c)
goto process;
break;
}
/* Is this a continuation line? If so, just append
* this to c, and go to next line right-away */
if (cs) {
*cs = '\0';
b = strappend(c, l);
if (!b)
return -ENOMEM;
free(c);
c = b;
continue;
}
/* If the previous line was a continuation line,
* append the current line to it */
if (c) {
b = strappend(c, l);
if (!b)
return -ENOMEM;
free(c);
c = b;
}
p = strstrip(c ? c : l);
_cleanup_free_ char *u;
int k;
u = normalize_env_assignment(p);
if (!u)
return -ENOMEM;
k = strv_extend(&m, u);
if (k < 0)
return -ENOMEM;
}
free(c);
c = NULL;
}
*rl = m;
m = NULL;
return 0;
}
int write_env_file(const char *fname, char **l) {
char **i;
char _cleanup_free_ *p = NULL;
int r;
r = fopen_temporary(fname, &f, &p);
if (r < 0)
return r;
errno = 0;
STRV_FOREACH(i, l) {
fputs(*i, f);
fputc('\n', f);
}
fflush(f);
if (ferror(f)) {
if (errno > 0)
r = -errno;
else
r = -EIO;
} else {
r = -errno;
else
r = 0;
}
if (r < 0)
unlink(p);
return r;
}