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