summaryrefslogtreecommitdiff
blob: 796d3d03e9ed2d4786f82bfcfbf8e5c047a651ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h> /* defines SIZE_MAX */
#include <stdio.h>

#include "error.h"

#include "data_output.h"

int ZoO_data_output_write_line
(
   const char filename [const restrict static 1],
   char line [const restrict static 1],
   size_t const line_size
)
{
   const int old_errno = errno;
   FILE * file;

   file = fopen(filename, "a");

   if (file == (FILE *) NULL)
   {
      ZoO_ERROR
      (
         "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
      (
         "Could not store line '%s' in %s.",
         line,
         filename
      );

      fclose(file);

      return -1;
   }

   fclose(file);

   return 0;
}