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] [--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)
53 static void read_line(conf_t
*conf
, char *buf
) {
54 if (conf
->interactive
)
55 if (fputs(PROMPT
, stderr
) == EOF
)
56 die("I/O error writing prompt");
57 if (fgets(buf
, BUF_SIZE
, stdin
) == NULL
) {
59 die("I/O error reading line");
61 buf
[0] = '\0'; /* Unclear if fgets does this already */
64 die("Unexpected error reading line");
68 static void write_line(const char *now
, FILE *f
, const char *line
) {
70 die_err("Error opening output file");
71 if (fputs(now
, f
) == EOF
)
72 die("Error writing to output file");
73 if (fputc(' ', f
) == EOF
)
74 die("Error writing to output file");
75 if (fputs(line
, f
) == EOF
)
76 die("Error writing to output file");
79 static void take_fcntl_lock(conf_t
*conf
, FILE *f
) {
80 if (!conf
->fcntl_lock
)
83 lock
.l_type
= F_WRLCK
;
84 lock
.l_whence
= SEEK_SET
;
89 die_err("Couldn't get file descriptor for locking");
90 if (fcntl(fd
, F_SETLK
, &lock
) == 0)
92 if (errno
!= EACCES
&& errno
!= EAGAIN
)
93 die_err("Couldn't take fcntl lock");
94 if (fputs(WAITING
, stderr
) == EOF
)
95 die("Error writing waiting message");
96 if (fcntl(fd
, F_SETLKW
, &lock
) == 0)
98 die_err("Couldn't take fcntl lock");
101 static void take_flock_lock(conf_t
*conf
, FILE *f
) {
102 if (!conf
->flock_lock
)
106 die_err("Couldn't get file descriptor for locking");
107 if (flock(fd
, LOCK_EX
| LOCK_NB
) == 0)
109 if (errno
!= EWOULDBLOCK
)
110 die_err("Couldn't take flock lock");
111 if (fputs(WAITING
, stderr
) == EOF
)
112 die("Error writing waiting message");
113 if (flock(fd
, LOCK_EX
) == 0)
115 die_err("Couldn't take flock lock");
118 static void take_lock(conf_t
*conf
, FILE *f
) {
119 take_fcntl_lock(conf
, f
);
120 take_flock_lock(conf
, f
);
123 static void write_acknowledgment(conf_t
*conf
) {
124 if (conf
->interactive
) {
125 if (fputs(ACKNOWLEDGE
, stderr
) == EOF
)
126 die("Error writing acknowledgment");
127 if (nanosleep(&ACKNOWLEDGE_DELAY
, NULL
) == -1 && errno
!= EINTR
)
128 die_err("Error sleeping");
132 static void lock_and_write_line(conf_t
*conf
, const char *line
) {
133 const char *now
= encode_time(time(NULL
));
134 FILE *f
= fopen(FILENAME
, "a");
137 write_line(now
, f
, line
);
140 die_err("Error closing output file");
142 write_acknowledgment(conf
);
145 int main(int argc
, char *argv
[]) {
146 conf_t conf
= parse_command_line(argc
, argv
);
148 for (read_line(&conf
, buf
); buf
[0]; read_line(&conf
, buf
)) {
149 lock_and_write_line(&conf
, buf
);