#include <time.h>
#include <unistd.h>
-#define BUFSIZE 4095
+#define BUFSIZE 4096
-static char buf[BUFSIZE + 1];
-static size_t offset = 0;
+static char BUF[BUFSIZE];
static uint64_t get_timestamp(sd_journal *j) {
uint64_t timestamp;
if (!length) {
return;
}
- size_t string_offset = 0;
- size_t remaining = length;
- while (offset + remaining > BUFSIZE) {
- strncpy(buf + offset, string + string_offset, BUFSIZE - offset);
- string_offset += BUFSIZE - offset;
- remaining = length - string_offset;
- if (write (1, buf, BUFSIZE) <= 0) {
- perror("write to stdout failed");
- exit(1);
- }
- offset = 0;
+
+ size_t r = fwrite_unlocked(string, 1, length, stdout);
+ if (r < length) {
+ fprintf(stderr, "Failed to write\n");
+ exit(1);
}
- strncpy(buf + offset, string + string_offset, remaining);
- offset += remaining;
}
static void print_cursor(sd_journal *j) {
usage("unkown, or to many arguments");
}
+ // setup stdout buffer
+ if (setvbuf(stdout, BUF, _IOFBF, BUFSIZE)) {
+ fprintf(stderr, "Failed to set buffer for stdout: %s\n", strerror(errno));
+ return 1;
+ }
+
// to prevent calling it everytime we generate a timestamp
tzset();
sd_journal_close(j);
// print remaining buffer
- if (write (1, buf, offset) <= 0) {
- perror("write to stdout failed");
- return 1;
- }
+ fflush_unlocked(stdout);
return 0;
}