]> git.scottworley.com Git - tl-append/blame - tl-append.c
Release 1.0.1
[tl-append] / tl-append.c
CommitLineData
ef591a4f
SW
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
e81b7a5f
SW
18#define _POSIX_C_SOURCE 199309L
19
20#include <errno.h>
b56e29cb 21#include <fcntl.h>
b3d5ed96
SW
22#include <stdio.h>
23#include <stdlib.h>
e81b7a5f 24#include <string.h>
d212cd99 25#include <sys/file.h>
e81b7a5f
SW
26#include <time.h>
27#include <unistd.h>
b3d5ed96 28
d522116b 29#include "common.h"
b3d5ed96 30
4b4feda2 31const int BUF_SIZE = 1024;
b3d5ed96 32
e81b7a5f
SW
33const char PROMPT[] = "\33[H" /* Move cursor 'home' */
34 "\33[J" /* Clear screen */
35 "> ";
36const char ACKNOWLEDGE[] = "[OK]";
b56e29cb 37const char WAITING[] = "[Waiting on lock...]\n";
e81b7a5f
SW
38const struct timespec ACKNOWLEDGE_DELAY = {0, 300000000};
39
40typedef struct {
41 int interactive;
675caf99 42 int fcntl_lock;
d212cd99 43 int flock_lock;
e81b7a5f
SW
44} conf_t;
45
7521e30b 46void usage() {
ff3c08a3 47 die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
7521e30b
SW
48}
49
e81b7a5f 50conf_t parse_command_line(int argc, char *argv[]) {
d19cfb47
SW
51 conf_t conf;
52 conf.interactive = 0;
675caf99 53 conf.fcntl_lock = 1;
d212cd99 54 conf.flock_lock = 1;
e81b7a5f 55
d19cfb47
SW
56 for (int i = 1; i < argc; i++) {
57 if (strcmp(argv[i], "-i") == 0 && isatty(2))
58 conf.interactive = 1;
7521e30b 59 else if (strcmp(argv[i], "--no-fnctl-lock") == 0)
675caf99 60 conf.fcntl_lock = 0;
7521e30b 61 else if (strcmp(argv[i], "--no-flock-lock") == 0)
d212cd99 62 conf.flock_lock = 0;
ff3c08a3
SW
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
7521e30b 70 usage();
d19cfb47 71 }
e81b7a5f
SW
72
73 return conf;
74}
75
88eed985
SW
76static void read_line(conf_t *conf, char *buf) {
77 if (conf->interactive)
e81b7a5f
SW
78 if (fputs(PROMPT, stderr) == EOF)
79 die("I/O error writing prompt");
b3d5ed96
SW
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
b56e29cb 91static void write_line(const char *now, FILE *f, const char *line) {
b3d5ed96
SW
92 if (f == NULL)
93 die_err("Error opening output file");
2998af81
SW
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");
b3d5ed96
SW
98 if (fputs(line, f) == EOF)
99 die("Error writing to output file");
b56e29cb
SW
100}
101
d212cd99 102static void take_fcntl_lock(conf_t *conf, FILE *f) {
675caf99
SW
103 if (!conf->fcntl_lock)
104 return;
b56e29cb
SW
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)
d212cd99 116 die_err("Couldn't take fcntl lock");
b56e29cb
SW
117 if (fputs(WAITING, stderr) == EOF)
118 die("Error writing waiting message");
119 if (fcntl(fd, F_SETLKW, &lock) == 0)
120 return;
d212cd99
SW
121 die_err("Couldn't take fcntl lock");
122}
123
124static 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
141static void take_lock(conf_t *conf, FILE *f) {
142 take_fcntl_lock(conf, f);
143 take_flock_lock(conf, f);
b56e29cb
SW
144}
145
88eed985
SW
146static void write_acknowledgment(conf_t *conf) {
147 if (conf->interactive) {
e81b7a5f
SW
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 }
b3d5ed96
SW
153}
154
88eed985 155static void lock_and_write_line(conf_t *conf, const char *line) {
9b76e9d2 156 char *now = encode_time(time(NULL));
b56e29cb
SW
157 FILE *f = fopen(FILENAME, "a");
158
675caf99 159 take_lock(conf, f);
b56e29cb
SW
160 write_line(now, f, line);
161
162 if (fclose(f) != 0)
163 die_err("Error closing output file");
164
9b76e9d2 165 free(now);
b56e29cb
SW
166 write_acknowledgment(conf);
167}
168
e81b7a5f
SW
169int main(int argc, char *argv[]) {
170 conf_t conf = parse_command_line(argc, argv);
b3d5ed96 171 char buf[BUF_SIZE];
88eed985
SW
172 for (read_line(&conf, buf); buf[0]; read_line(&conf, buf)) {
173 lock_and_write_line(&conf, buf);
b3d5ed96
SW
174 }
175 return 0;
176}