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