]> git.scottworley.com Git - tl-append/blob - tl-append.c
Release 1.0.1
[tl-append] / tl-append.c
1 /*
2 * tl-append: time-logger appending shell
3 * Copyright (C) 2023 Scott Worley <scottworley@scottworley.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #define _POSIX_C_SOURCE 199309L
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/file.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include "common.h"
30
31 const int BUF_SIZE = 1024;
32
33 const char PROMPT[] = "\33[H" /* Move cursor 'home' */
34 "\33[J" /* Clear screen */
35 "> ";
36 const char ACKNOWLEDGE[] = "[OK]";
37 const char WAITING[] = "[Waiting on lock...]\n";
38 const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
39
40 typedef struct {
41 int interactive;
42 int fcntl_lock;
43 int flock_lock;
44 } conf_t;
45
46 void usage() {
47 die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
48 }
49
50 conf_t parse_command_line(int argc, char *argv[]) {
51 conf_t conf;
52 conf.interactive = 0;
53 conf.fcntl_lock = 1;
54 conf.flock_lock = 1;
55
56 for (int i = 1; i < argc; i++) {
57 if (strcmp(argv[i], "-i") == 0 && isatty(2))
58 conf.interactive = 1;
59 else if (strcmp(argv[i], "--no-fnctl-lock") == 0)
60 conf.fcntl_lock = 0;
61 else if (strcmp(argv[i], "--no-flock-lock") == 0)
62 conf.flock_lock = 0;
63 else if (strcmp(argv[i], "-C") == 0) {
64 if (i + 1 >= argc)
65 die("-C requires a directory");
66 if (chdir(argv[i + 1]) == -1)
67 die_err("Couldn't change directory");
68 i++;
69 } else
70 usage();
71 }
72
73 return conf;
74 }
75
76 static void read_line(conf_t *conf, char *buf) {
77 if (conf->interactive)
78 if (fputs(PROMPT, stderr) == EOF)
79 die("I/O error writing prompt");
80 if (fgets(buf, BUF_SIZE, stdin) == NULL) {
81 if (ferror(stdin))
82 die("I/O error reading line");
83 if (feof(stdin)) {
84 buf[0] = '\0'; /* Unclear if fgets does this already */
85 return;
86 }
87 die("Unexpected error reading line");
88 }
89 }
90
91 static void write_line(const char *now, FILE *f, const char *line) {
92 if (f == NULL)
93 die_err("Error opening output file");
94 if (fputs(now, f) == EOF)
95 die("Error writing to output file");
96 if (fputc(' ', f) == EOF)
97 die("Error writing to output file");
98 if (fputs(line, f) == EOF)
99 die("Error writing to output file");
100 }
101
102 static void take_fcntl_lock(conf_t *conf, FILE *f) {
103 if (!conf->fcntl_lock)
104 return;
105 struct flock lock;
106 lock.l_type = F_WRLCK;
107 lock.l_whence = SEEK_SET;
108 lock.l_start = 0;
109 lock.l_len = 0;
110 int fd = fileno(f);
111 if (fd == -1)
112 die_err("Couldn't get file descriptor for locking");
113 if (fcntl(fd, F_SETLK, &lock) == 0)
114 return;
115 if (errno != EACCES && errno != EAGAIN)
116 die_err("Couldn't take fcntl lock");
117 if (fputs(WAITING, stderr) == EOF)
118 die("Error writing waiting message");
119 if (fcntl(fd, F_SETLKW, &lock) == 0)
120 return;
121 die_err("Couldn't take fcntl lock");
122 }
123
124 static void take_flock_lock(conf_t *conf, FILE *f) {
125 if (!conf->flock_lock)
126 return;
127 int fd = fileno(f);
128 if (fd == -1)
129 die_err("Couldn't get file descriptor for locking");
130 if (flock(fd, LOCK_EX | LOCK_NB) == 0)
131 return;
132 if (errno != EWOULDBLOCK)
133 die_err("Couldn't take flock lock");
134 if (fputs(WAITING, stderr) == EOF)
135 die("Error writing waiting message");
136 if (flock(fd, LOCK_EX) == 0)
137 return;
138 die_err("Couldn't take flock lock");
139 }
140
141 static void take_lock(conf_t *conf, FILE *f) {
142 take_fcntl_lock(conf, f);
143 take_flock_lock(conf, f);
144 }
145
146 static void write_acknowledgment(conf_t *conf) {
147 if (conf->interactive) {
148 if (fputs(ACKNOWLEDGE, stderr) == EOF)
149 die("Error writing acknowledgment");
150 if (nanosleep(&ACKNOWLEDGE_DELAY, NULL) == -1 && errno != EINTR)
151 die_err("Error sleeping");
152 }
153 }
154
155 static void lock_and_write_line(conf_t *conf, const char *line) {
156 char *now = encode_time(time(NULL));
157 FILE *f = fopen(FILENAME, "a");
158
159 take_lock(conf, f);
160 write_line(now, f, line);
161
162 if (fclose(f) != 0)
163 die_err("Error closing output file");
164
165 free(now);
166 write_acknowledgment(conf);
167 }
168
169 int main(int argc, char *argv[]) {
170 conf_t conf = parse_command_line(argc, argv);
171 char buf[BUF_SIZE];
172 for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
173 lock_and_write_line(&conf, buf);
174 }
175 return 0;
176 }