author | viric <viriketo@gmail.com> |
Tue, 11 Oct 2011 19:50:10 +0200 | |
changeset 303 | 2edd42e77392 |
parent 277 | 1ee3c4ef9402 |
child 313 | 19f22c06343b |
permissions | -rw-r--r-- |
183
95d49e8a8cec
Updating 'help' and some other related files to 0.5
viric@llimona
parents:
179
diff
changeset
|
1 |
/* |
95d49e8a8cec
Updating 'help' and some other related files to 0.5
viric@llimona
parents:
179
diff
changeset
|
2 |
Task Spooler - a task queue system for the unix user |
267 | 3 |
Copyright (C) 2007-2009 LluĂs Batlle i Rossell |
183
95d49e8a8cec
Updating 'help' and some other related files to 0.5
viric@llimona
parents:
179
diff
changeset
|
4 |
|
95d49e8a8cec
Updating 'help' and some other related files to 0.5
viric@llimona
parents:
179
diff
changeset
|
5 |
Please find the license in the provided COPYING file. |
95d49e8a8cec
Updating 'help' and some other related files to 0.5
viric@llimona
parents:
179
diff
changeset
|
6 |
*/ |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
7 |
#include <unistd.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
8 |
#include <fcntl.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
9 |
#include <sys/types.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
10 |
#include <sys/stat.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
11 |
#include <stdio.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
12 |
#include <errno.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
13 |
#include <string.h> |
274
62048132f95f
Making changes so it looks like the code in ts-0.6.4, which I can't find
viric@mandarina
parents:
267
diff
changeset
|
14 |
#include <sys/select.h> |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
15 |
#include <stdlib.h> |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
16 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
17 |
#include <sys/time.h> /* Dep de main.h */ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
18 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
19 |
#include "main.h" |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
20 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
21 |
enum { BSIZE=1024 }; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
22 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
23 |
static int min(int a, int b) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
24 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
25 |
if (a < b) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
26 |
return a; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
27 |
return b; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
28 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
29 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
30 |
static int max(int a, int b) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
31 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
32 |
if (a > b) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
33 |
return a; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
34 |
return b; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
35 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
36 |
|
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
37 |
static void tail_error(const char *str) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
38 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
39 |
fprintf(stderr, "%s", str); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
40 |
fprintf(stderr, ". errno: %i (%s)\n", |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
41 |
errno, strerror(errno)); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
42 |
exit(-1); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
43 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
44 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
45 |
static void seek_at_last_lines(int fd, int lines) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
46 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
47 |
char buf[BSIZE]; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
48 |
int lines_found = 0; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
49 |
int last_lseek = BSIZE; |
211
51938376376e
Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents:
203
diff
changeset
|
50 |
int last_read = 0; /* Only to catch not doing any loop */ |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
51 |
int move_offset; |
211
51938376376e
Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents:
203
diff
changeset
|
52 |
int i = -1; /* Only to catch not doing any loop */ |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
53 |
|
187 | 54 |
last_lseek = lseek(fd, 0, SEEK_END); |
55 |
||
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
56 |
do |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
57 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
58 |
int next_read; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
59 |
next_read = min(last_lseek, BSIZE); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
60 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
61 |
/* we should end looping if last_lseek == 0 |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
62 |
* This means we already read all the file. */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
63 |
if (next_read <= 0) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
64 |
break; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
65 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
66 |
/* last_lseek will be -1 at the beginning of the file, |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
67 |
* if we wanted to go farer than it. */ |
228 | 68 |
last_lseek = lseek(fd, -next_read, SEEK_CUR); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
69 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
70 |
if (last_lseek == -1) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
71 |
last_lseek = lseek(fd, 0, SEEK_SET); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
72 |
|
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
73 |
last_read = read(fd, buf, next_read); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
74 |
if (last_read == -1) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
75 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
76 |
if (errno == EINTR) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
77 |
continue; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
78 |
tail_error("Error reading"); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
79 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
80 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
81 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
82 |
for(i = last_read-1; i >= 0; --i) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
83 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
84 |
if (buf[i] == '\n') |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
85 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
86 |
++lines_found; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
87 |
if (lines_found > lines) |
228 | 88 |
/* We will use 'i' to calculate where to |
89 |
* put the file cursor, before return. */ |
|
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
90 |
break; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
91 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
92 |
} |
187 | 93 |
|
94 |
/* Go back the read bytes */ |
|
95 |
last_lseek = lseek(fd, -last_read, SEEK_CUR); |
|
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
96 |
} while(lines_found < lines); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
97 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
98 |
/* Calculate the position */ |
228 | 99 |
move_offset = i+1; |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
100 |
lseek(fd, move_offset, SEEK_CUR); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
101 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
102 |
|
187 | 103 |
static void set_non_blocking(int fd) |
104 |
{ |
|
105 |
long arg; |
|
106 |
||
107 |
arg = O_RDONLY | O_NONBLOCK; |
|
108 |
fcntl(fd, F_SETFL, arg); |
|
109 |
} |
|
110 |
||
203
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
189
diff
changeset
|
111 |
/* if last_lines == -1, go on from the start of the file */ |
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
189
diff
changeset
|
112 |
int tail_file(const char *fname, int last_lines) |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
113 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
114 |
int fd; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
115 |
int res; |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
116 |
int waiting_end = 1; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
117 |
int end_res = 0; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
118 |
int endfile_reached = 0; |
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
119 |
int could_write = 1; |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
120 |
|
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
121 |
fd_set readset, errorset; |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
122 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
123 |
fd = open(fname, O_RDONLY); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
124 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
125 |
if (fd == -1) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
126 |
tail_error("Error: Cannot open the outut file"); |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
127 |
|
203
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
189
diff
changeset
|
128 |
if (last_lines >= 0) |
664044b1de73
Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents:
189
diff
changeset
|
129 |
seek_at_last_lines(fd, last_lines); |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
130 |
|
187 | 131 |
/* we don't want the next read calls to block. */ |
132 |
set_non_blocking(fd); |
|
133 |
||
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
134 |
do |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
135 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
136 |
char buf[BSIZE]; |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
137 |
int maxfd; |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
138 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
139 |
FD_ZERO(&readset); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
140 |
maxfd = -1; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
141 |
if (!endfile_reached) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
142 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
143 |
FD_SET(fd, &readset); |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
144 |
maxfd = fd; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
145 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
146 |
if (waiting_end) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
147 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
148 |
FD_SET(server_socket, &readset); |
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
149 |
maxfd = max(maxfd, server_socket); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
150 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
151 |
|
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
152 |
FD_ZERO(&errorset); |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
153 |
FD_SET(1, &errorset); |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
154 |
maxfd = max(maxfd, server_socket); |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
155 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
156 |
/* If we don't have fd's to wait for, let's sleep */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
157 |
if (maxfd == -1) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
158 |
{ |
179
60e972139b56
Moving from nanosleep to usleep, because I don't know in what standard nanosleep is defined.
lbatlle@npdl268.bpo.hp.com
parents:
174
diff
changeset
|
159 |
usleep(1 /* sec */* 1000000); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
160 |
} else |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
161 |
{ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
162 |
/* Otherwise, do a normal select */ |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
163 |
struct timeval tv = {1 /*sec*/, 0 }; |
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
164 |
res = select(maxfd + 1, &readset, 0, &errorset, &tv); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
165 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
166 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
167 |
if (FD_ISSET(server_socket, &readset)) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
168 |
{ |
258
825b9ed3f836
Fixing a bug on tail/cat, waiting for the job end.
viric@mandarina
parents:
231
diff
changeset
|
169 |
end_res = c_wait_job_recv(); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
170 |
waiting_end = 0; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
171 |
} |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
172 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
173 |
/* We always read when select awakes */ |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
174 |
res = read(fd, buf, BSIZE); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
175 |
if (res == -1) |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
176 |
{ |
187 | 177 |
if (errno == EINTR || errno == EAGAIN) |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
178 |
{ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
179 |
res = 1; /* Hack for the while condition */ |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
180 |
continue; |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
181 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
182 |
tail_error("Error reading"); |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
183 |
} |
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
184 |
|
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
185 |
if (res == 0) |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
186 |
endfile_reached = 1; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
187 |
else |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
188 |
endfile_reached = 0; |
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
189 |
|
277
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
190 |
if (!FD_ISSET(1, &errorset)) |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
191 |
{ |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
192 |
while(res > 0) |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
193 |
{ |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
194 |
int wres; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
195 |
wres = write(1, buf, res); |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
196 |
/* Maybe the listener doesn't want to receive more */ |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
197 |
if (wres < 0) |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
198 |
{ |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
199 |
could_write = 0; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
200 |
break; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
201 |
} |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
202 |
res -= wres; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
203 |
} |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
204 |
} |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
205 |
else |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
206 |
{ |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
207 |
could_write = 0; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
208 |
break; |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
209 |
} |
1ee3c4ef9402
Fixing a bug on -c and -t that remained open after killing 'less' on "ts -c | less"
viric@mandarina
parents:
274
diff
changeset
|
210 |
} while((!endfile_reached || waiting_end) && could_write); |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
211 |
|
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
212 |
close(fd); |
174
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
213 |
|
c112f67965fb
Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents:
173
diff
changeset
|
214 |
return end_res; |
173
b572fdd206f4
Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff
changeset
|
215 |
} |