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