From df3657b2a99ef20da99ac3c6c02f43cc23e70fca Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Fri, 20 Jan 2017 22:19:09 +0100 Subject: Moving towards a server/clients structure. --- src/storage/storage.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/storage/storage.c (limited to 'src/storage/storage.c') diff --git a/src/storage/storage.c b/src/storage/storage.c new file mode 100644 index 0000000..22c5c49 --- /dev/null +++ b/src/storage/storage.c @@ -0,0 +1,69 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include +#include +#include /* defines SIZE_MAX */ +#include + +#include "../pipe/pipe.h" + +#include "storage.h" + +int ZoO_storage_write_line +( + const char filename [const restrict static 1], + char line [const restrict static 1], + size_t const line_size, + const struct ZoO_pipe io [const restrict static 1] +) +{ + const int old_errno = errno; + FILE * file; + + file = fopen(filename, "a"); + + if (file == (FILE *) NULL) + { + ZoO_ERROR + ( + io, + "Could not open file '%s' in appending mode.", + filename + ); + + return -1; + } + + line[line_size - 1] = '\n'; + + if + ( + fwrite + ( + (const void *) line, + sizeof(char), + line_size, + file + ) < line_size + ) + { + line[line_size - 1] = '\0'; + + ZoO_ERROR + ( + io, + "Could not store line '%s' in %s.", + line, + filename + ); + + fclose(file); + + return -1; + } + + fclose(file); + + return 0; +} -- cgit v1.2.3-70-g09d2