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