1 #define _POSIX_C_SOURCE 199309L
14 const int BUF_SIZE
= 1024;
16 const char PROMPT
[] = "\33[H" /* Move cursor 'home' */
17 "\33[J" /* Clear screen */
19 const char ACKNOWLEDGE
[] = "[OK]";
20 const char WAITING
[] = "[Waiting on lock...]\n";
21 const struct timespec ACKNOWLEDGE_DELAY
= {0, 300000000};
30 die("usage: tl-append [-i] [-C dir] [--no-fnctl-lock] [--no-flock-lock]");
33 conf_t
parse_command_line(int argc
, char *argv
[]) {
39 for (int i
= 1; i
< argc
; i
++) {
40 if (strcmp(argv
[i
], "-i") == 0 && isatty(2))
42 else if (strcmp(argv
[i
], "--no-fnctl-lock") == 0)
44 else if (strcmp(argv
[i
], "--no-flock-lock") == 0)
46 else if (strcmp(argv
[i
], "-C") == 0) {
48 die("-C requires a directory");
49 if (chdir(argv
[i
+ 1]) == -1)
50 die_err("Couldn't change directory");
59 static void read_line(conf_t
*conf
, char *buf
) {
60 if (conf
->interactive
)
61 if (fputs(PROMPT
, stderr
) == EOF
)
62 die("I/O error writing prompt");
63 if (fgets(buf
, BUF_SIZE
, stdin
) == NULL
) {
65 die("I/O error reading line");
67 buf
[0] = '\0'; /* Unclear if fgets does this already */
70 die("Unexpected error reading line");
74 static void write_line(const char *now
, FILE *f
, const char *line
) {
76 die_err("Error opening output file");
77 if (fputs(now
, f
) == EOF
)
78 die("Error writing to output file");
79 if (fputc(' ', f
) == EOF
)
80 die("Error writing to output file");
81 if (fputs(line
, f
) == EOF
)
82 die("Error writing to output file");
85 static void take_fcntl_lock(conf_t
*conf
, FILE *f
) {
86 if (!conf
->fcntl_lock
)
89 lock
.l_type
= F_WRLCK
;
90 lock
.l_whence
= SEEK_SET
;
95 die_err("Couldn't get file descriptor for locking");
96 if (fcntl(fd
, F_SETLK
, &lock
) == 0)
98 if (errno
!= EACCES
&& errno
!= EAGAIN
)
99 die_err("Couldn't take fcntl lock");
100 if (fputs(WAITING
, stderr
) == EOF
)
101 die("Error writing waiting message");
102 if (fcntl(fd
, F_SETLKW
, &lock
) == 0)
104 die_err("Couldn't take fcntl lock");
107 static void take_flock_lock(conf_t
*conf
, FILE *f
) {
108 if (!conf
->flock_lock
)
112 die_err("Couldn't get file descriptor for locking");
113 if (flock(fd
, LOCK_EX
| LOCK_NB
) == 0)
115 if (errno
!= EWOULDBLOCK
)
116 die_err("Couldn't take flock lock");
117 if (fputs(WAITING
, stderr
) == EOF
)
118 die("Error writing waiting message");
119 if (flock(fd
, LOCK_EX
) == 0)
121 die_err("Couldn't take flock lock");
124 static void take_lock(conf_t
*conf
, FILE *f
) {
125 take_fcntl_lock(conf
, f
);
126 take_flock_lock(conf
, f
);
129 static void write_acknowledgment(conf_t
*conf
) {
130 if (conf
->interactive
) {
131 if (fputs(ACKNOWLEDGE
, stderr
) == EOF
)
132 die("Error writing acknowledgment");
133 if (nanosleep(&ACKNOWLEDGE_DELAY
, NULL
) == -1 && errno
!= EINTR
)
134 die_err("Error sleeping");
138 static void lock_and_write_line(conf_t
*conf
, const char *line
) {
139 char *now
= encode_time(time(NULL
));
140 FILE *f
= fopen(FILENAME
, "a");
143 write_line(now
, f
, line
);
146 die_err("Error closing output file");
149 write_acknowledgment(conf
);
152 int main(int argc
, char *argv
[]) {
153 conf_t conf
= parse_command_line(argc
, argv
);
155 for (read_line(&conf
, buf
); buf
[0]; read_line(&conf
, buf
)) {
156 lock_and_write_line(&conf
, buf
);