]> git.scottworley.com Git - tl-append/blame - tl-append.c
Hold a lock while appending
[tl-append] / tl-append.c
CommitLineData
e81b7a5f
SW
1#define _POSIX_C_SOURCE 199309L
2
3#include <errno.h>
b56e29cb 4#include <fcntl.h>
b3d5ed96
SW
5#include <stdio.h>
6#include <stdlib.h>
e81b7a5f
SW
7#include <string.h>
8#include <time.h>
9#include <unistd.h>
b3d5ed96 10
d522116b 11#include "common.h"
b3d5ed96 12
4b4feda2 13const int BUF_SIZE = 1024;
b3d5ed96 14
e81b7a5f
SW
15const char PROMPT[] = "\33[H" /* Move cursor 'home' */
16 "\33[J" /* Clear screen */
17 "> ";
18const char ACKNOWLEDGE[] = "[OK]";
b56e29cb 19const char WAITING[] = "[Waiting on lock...]\n";
e81b7a5f
SW
20const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
21
22typedef struct {
23 int interactive;
24} conf_t;
25
26conf_t parse_command_line(int argc, char *argv[]) {
27 conf_t conf = {0};
28
29 if (argc == 2 && strcmp(argv[1], "-i") == 0 && isatty(2))
30 conf.interactive = 1;
31
32 return conf;
33}
34
35static void read_line(conf_t conf, char *buf) {
36 if (conf.interactive)
37 if (fputs(PROMPT, stderr) == EOF)
38 die("I/O error writing prompt");
b3d5ed96
SW
39 if (fgets(buf, BUF_SIZE, stdin) == NULL) {
40 if (ferror(stdin))
41 die("I/O error reading line");
42 if (feof(stdin)) {
43 buf[0] = '\0'; /* Unclear if fgets does this already */
44 return;
45 }
46 die("Unexpected error reading line");
47 }
48}
49
b56e29cb 50static void write_line(const char *now, FILE *f, const char *line) {
b3d5ed96
SW
51 if (f == NULL)
52 die_err("Error opening output file");
2998af81
SW
53 if (fputs(now, f) == EOF)
54 die("Error writing to output file");
55 if (fputc(' ', f) == EOF)
56 die("Error writing to output file");
b3d5ed96
SW
57 if (fputs(line, f) == EOF)
58 die("Error writing to output file");
b56e29cb
SW
59}
60
61static void take_lock(FILE *f) {
62 struct flock lock;
63 lock.l_type = F_WRLCK;
64 lock.l_whence = SEEK_SET;
65 lock.l_start = 0;
66 lock.l_len = 0;
67 int fd = fileno(f);
68 if (fd == -1)
69 die_err("Couldn't get file descriptor for locking");
70 if (fcntl(fd, F_SETLK, &lock) == 0)
71 return;
72 if (errno != EACCES && errno != EAGAIN)
73 die_err("Couldn't take lock");
74 if (fputs(WAITING, stderr) == EOF)
75 die("Error writing waiting message");
76 if (fcntl(fd, F_SETLKW, &lock) == 0)
77 return;
78 die_err("Couldn't take lock");
79}
80
81static void write_acknowledgment(conf_t conf) {
e81b7a5f
SW
82 if (conf.interactive) {
83 if (fputs(ACKNOWLEDGE, stderr) == EOF)
84 die("Error writing acknowledgment");
85 if (nanosleep(&ACKNOWLEDGE_DELAY, NULL) == -1 && errno != EINTR)
86 die_err("Error sleeping");
87 }
b3d5ed96
SW
88}
89
b56e29cb
SW
90static void lock_and_write_line(conf_t conf, const char *line) {
91 const char *now = encode_time(time(NULL));
92 FILE *f = fopen(FILENAME, "a");
93
94 take_lock(f);
95 write_line(now, f, line);
96
97 if (fclose(f) != 0)
98 die_err("Error closing output file");
99
100 write_acknowledgment(conf);
101}
102
e81b7a5f
SW
103int main(int argc, char *argv[]) {
104 conf_t conf = parse_command_line(argc, argv);
b3d5ed96 105 char buf[BUF_SIZE];
e81b7a5f 106 for (read_line(conf, buf); buf[0]; read_line(conf, buf)) {
b56e29cb 107 lock_and_write_line(conf, buf);
b3d5ed96
SW
108 }
109 return 0;
110}