-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftail-f.pl
executable file
·161 lines (87 loc) · 3.85 KB
/
ftail-f.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#Copyright (C)2001-2003 Altera Corporation
#Any megafunction design, and related net list (encrypted or decrypted),
#support information, device programming or simulation file, and any other
#associated documentation or information provided by Altera or a partner
#under Altera's Megafunction Partnership Program may be used only to
#program PLD devices (but not masked PLD devices) from Altera. Any other
#use of such megafunction design, net list, support information, device
#programming or simulation file, or any other related documentation or
#information is prohibited for any other purpose, including, but not
#limited to modification, reverse engineering, de-compiling, or use with
#any other silicon devices, unless such use is explicitly licensed under
#a separate agreement with Altera or a megafunction partner. Title to
#the intellectual property, including patents, copyrights, trademarks,
#trade secrets, or maskworks, embodied in any such megafunction design,
#net list, support information, device programming or simulation file, or
#any other related documentation or information provided by Altera or a
#megafunction partner, remains with Altera, the megafunction partner, or
#their respective licensors. No other licenses, including any licenses
#needed under any third party's intellectual property, are provided herein.
#Copying or modifying any file, or portion thereof, to which this notice
#is attached violates this copyright.
@prog = split (/\/|\\/,$0) ; $pname = $prog[$#prog] ;
select(STDIN); $| = 1; # make stdin unbufferred
select(STDOUT); $| = 1; # make stdout unbufferred
$SIG{'INT'} = 'quit'; # run &quit on ^C
&usage if @ARGV < 1; # die without 1 (file) arg
$file = $ARGV[0];
$old_size = -1;
$size = 0;
$some_bytes = 0;
die if (&args_test($file) && sleep 5);
while (1) {
if (($size == 0) && ($size != $old_size)) {
$formatted_time = &get_formatted_time();
syswrite (STDOUT,
"\n$pname: $file size is zero at time $formatted_time.\n".
"------------------------------------------------------------------------\n");
$old_size = $size;
}
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,
$size,$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);
if ($size > $old_size) {
sysopen (LOG, $file, O_RDONLY) or &ktb(); # ktb == KickTheBucket
sysseek (LOG, $old_size, 0);
sysread (LOG, $some_bytes, $size - $old_size);
close(LOG);
syswrite (STDOUT, $some_bytes);
$old_size = $size;
}
select(undef, undef, undef, 0.1);
} # end main loop [while TRUE]
sub usage {
die "usage: $pname logfile\n";
} # usage
sub quit { # interrupt handler for exit
local ($sig) = @_;
print STDERR "Caught SIG$sig; exiting...\n";
close (LOG);
sleep 1;
exit;
} # quit
sub ktb { # kick the bucket if file disappears
local ($file) = @_;
print STDERR "$pname: $file is gone!; exiting...\n";
sleep 1;
exit 1;
} # quit
sub args_test { # return 0 for success; return 1 for fail
local ($file) = @_;
local $code = " ";
local $die = 0;
if (defined (open (LOG, $file))) {
close(LOG); # success
} else {
$code = "$!\n"; # remember fail reason
}
if ($code ne " ") {
print STDERR "$pname: Cannot open '$file': $code";
$die = 1; # remember any failure
}
return $die; # return any failure status.
} # args_test
sub get_formatted_time { # create a timestring without using packages
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
return (sprintf ("%.2d:%.2d:%.2d %.2d/%.2d/%.4d", $hour,$min,$sec,($mon + 1),$mday,($year + 1900)));
} # get_formatted_time