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