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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/* $Id: xtail.c,v 2.5 2000/06/04 09:09:03 chip Exp $ */
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#define INTERN
#include "xtail.h"
#include <string.h>
int sigcaught = 0;
RETSIGTYPE sigcatcher(int sig);
int main(int argc, char *argv[])
{
int open_files_only, already_open, iteration, i;
struct entry_descrip *entryp;
struct stat sbuf, path_sbuf;
/*
* Initialize.
*/
Progname = basename(argv[0]);
List_file = new_entry_list(ENTRY_LIST_CHUNK);
List_dir = new_entry_list(ENTRY_LIST_CHUNK);
List_zap = new_entry_list(ENTRY_LIST_CHUNK);
Sorted = FALSE;
Reset_status = FALSE;
Debug = FALSE;
sigcatcher(0);
/*
* Place all of the entries onto lists.
*/
for (i = 1 ; i < argc ; ++i) {
if (i == 1 && strcmp(argv[i], "-D") == 0) {
Debug = TRUE;
continue;
}
/*
* Temporarily throw this entry onto the end of the zapped list.
*/
entryp = new_entry(List_zap, argv[i]);
/*
* Stat the file and get it to its proper place.
*/
switch (stat_entry(List_zap, last_entry(List_zap), &sbuf)) {
case ENTRY_FILE: /* move entry to file list */
move_entry(List_file, List_zap, last_entry(List_zap));
entryp->size = sbuf.st_size;
entryp->mtime = sbuf.st_mtime;
entryp->dev = sbuf.st_dev;
entryp->ino = sbuf.st_ino;
break;
case ENTRY_DIR: /* move entry to dir list */
move_entry(List_dir, List_zap, last_entry(List_zap));
entryp->size = sbuf.st_size;
entryp->mtime = sbuf.st_mtime;
if (scan_directory(entryp->name) != 0) {
message(MSSG_OPEN, entryp);
rmv_entry(List_dir, last_entry(List_dir));
}
break;
case ENTRY_ZAP: /* keep entry on zap list */
break;
case ENTRY_SPECIAL: /* entry is a special file */
message(MSSG_NOTAFIL, entryp);
rmv_entry(List_zap, last_entry(List_zap));
break;
default: /* stat error */
message(MSSG_STAT, entryp);
rmv_entry(List_zap, last_entry(List_zap));
break;
}
}
/*
* Make sure we are watching something reasonable.
*/
if (List_file->num_entries == 0) {
if (List_dir->num_entries == 0 && List_zap->num_entries == 0) {
(void) fprintf(stderr, "%s: no valid entries specified\n", Progname);
(void) exit(1);
}
(void) puts("\n*** warning - no files are being watched ***");
}
/*
* From this point on we want to reset the status of an entry any
* time we move it around to another list.
*/
Reset_status = TRUE;
/*
* Force a check of everything first time through the loop.
*/
iteration = CHECK_COUNT;
/*
* Loop forever.
*/
for (;;) {
/*
* Once every CHECK_COUNT iterations check everything.
* All other times only look at the opened files.
*/
open_files_only = (++iteration < CHECK_COUNT);
if (!open_files_only)
iteration = 0;
/*
* Make sure that the most recently modified files are open.
*/
if (!Sorted)
fixup_open_files();
/*
* Display what we are watching if a SIGINT was caught.
*/
if (sigcaught) {
show_status();
sigcatcher(0);
}
/*
* Go through all of the files looking for changes.
*/
Dprintf(stderr, ">>> checking files list (%s)\n",
(open_files_only ? "open files only" : "all files"));
for (i = 0 ; i < List_file->num_entries ; ++i) {
entryp = List_file->list[i];
already_open = (entryp->fd > 0) ;
/*
* Ignore closed files except every CHECK_COUNT iterations.
*/
if (!already_open && open_files_only)
continue;
/*
* Get the status of this file.
*/
switch (stat_entry(List_file, i, &sbuf)) {
case ENTRY_FILE: /* got status OK */
break;
case ENTRY_DIR: /* huh??? it's now a dir */
move_entry(List_dir, List_file, i--);
continue;
case ENTRY_ZAP: /* entry has been deleted */
message(MSSG_ZAPPED, entryp);
move_entry(List_zap, List_file, i--);
continue;
case ENTRY_SPECIAL: /* entry is a special file */
message(MSSG_NOTAFIL, entryp);
rmv_entry(List_file, i--);
continue;
default: /* stat error */
message(MSSG_STAT, entryp);
rmv_entry(List_file, i--);
continue;
}
/*
* See if an opened file has been deleted.
*/
if (already_open && sbuf.st_nlink == 0) {
message(MSSG_ZAPPED, entryp);
move_entry(List_zap, List_file, i--);
continue;
}
/*
* See if an open file has been renamed. Occasionally do
* an extra stat on the file's name, and compare that to the
* info which fstat() returned.
*/
if (already_open && !open_files_only
&& (stat(entryp->name, &path_sbuf) != 0
|| path_sbuf.st_dev != entryp->dev
|| path_sbuf.st_ino != entryp->ino)) {
message(MSSG_RENAMED, entryp);
move_entry(List_zap, List_file, i--);
continue;
}
/*
* If nothing has changed then continue on.
*/
if (entryp->size==sbuf.st_size && entryp->mtime==sbuf.st_mtime)
continue;
/*
* If the file isn't already open, then do so.
* Note -- it is important that we call "fixup_open_files()"
* at the end of the loop to make sure too many files don't
* stay opened.
*/
if (!already_open && open_entry(List_file, i) != 0) {
--i;
continue;
}
/*
* See if the file has been truncated.
*/
if (sbuf.st_size < entryp->size) {
message(MSSG_TRUNC, entryp);
entryp->size = 0;
}
/*
* Seek to where the changes begin.
*/
if (lseek(entryp->fd, entryp->size, 0) < 0) {
message(MSSG_SEEK, entryp);
rmv_entry(List_file, i--);
continue;
}
/*
* Dump the recently added info.
*/
{
int nb;
static char buf[BUFSIZ];
message(MSSG_BANNER, entryp);
while ((nb = read(entryp->fd, buf, sizeof(buf))) > 0) {
(void) fwrite(buf, sizeof(char), (unsigned) nb, stdout);
entryp->size += nb;
}
if (nb < 0) {
message(MSSG_READ, entryp);
rmv_entry(List_file, i--);
continue;
}
}
/*
* Update the other saved info about this entry.
*/
entryp->mtime = sbuf.st_mtime;
entryp->dev = sbuf.st_dev;
entryp->ino = sbuf.st_ino;
/*
* Since we've changed the mtime, the list might no longer be
* sorted. However if this entry is already at the top of the
* list then it's OK.
*/
if (i != 0)
Sorted = FALSE;
/*
* If we've just opened the file then force a resort now to
* prevent too many files from being opened.
*/
if (!already_open)
fixup_open_files();
}
/*
* Go through list of nonexistent entries to see if any have appeared.
* This is done only once every CHECK_COUNT iterations.
*/
if (!open_files_only) {
Dprintf(stderr, ">>> checking zapped list\n");
for (i = 0 ; i < List_zap->num_entries ; ++i) {
entryp = List_zap->list[i];
switch (stat_entry(List_zap, i, &sbuf)) {
case ENTRY_FILE: /* entry has appeared as a file */
message(MSSG_CREATED, entryp);
move_entry(List_file, List_zap, i--);
break;
case ENTRY_DIR: /* entry has appeared as a dir */
message(MSSG_CREATED, entryp);
move_entry(List_dir, List_zap, i--);
break;
case ENTRY_ZAP: /* entry still doesn't exist */
break;
case ENTRY_SPECIAL: /* entry is a special file */
message(MSSG_NOTAFIL, entryp);
rmv_entry(List_zap, i--);
break;
default: /* error - entry removed */
message(MSSG_STAT, entryp);
rmv_entry(List_zap, i--);
break;
}
}
}
/*
* Go through the list of dirs to see if any new files were created.
* This is done only once every CHECK_COUNT iterations.
*/
if (!open_files_only) {
Dprintf(stderr, ">>> checking directory list\n");
for (i = 0 ; !open_files_only && i < List_dir->num_entries ; ++i) {
entryp = List_dir->list[i];
switch (stat_entry(List_dir, i, &sbuf)) {
case ENTRY_DIR: /* got status OK */
break;
case ENTRY_FILE: /* huh??? it's now a reg file */
move_entry(List_file, List_dir, i--);
continue;
case ENTRY_ZAP: /* entry has been deleted */
message(MSSG_ZAPPED, entryp);
move_entry(List_zap, List_dir, i--);
continue;
case ENTRY_SPECIAL: /* entry is a special file */
message(MSSG_NOTAFIL, entryp);
rmv_entry(List_dir, i--);
continue;
default: /* stat error */
message(MSSG_STAT, entryp);
rmv_entry(List_dir, i--);
continue;
}
if (entryp->mtime == sbuf.st_mtime)
continue;
if (scan_directory(entryp->name) != 0) {
message(MSSG_OPEN, entryp);
rmv_entry(List_dir, i--);
}
entryp->mtime = sbuf.st_mtime;
}
}
/*
* End of checking loop.
*/
{
extern unsigned sleep(unsigned int);
(void) fflush(stdout);
(void) sleep(SLEEP_TIME);
}
}
/*NOTREACHED*/
}
RETSIGTYPE sigcatcher(int sig)
{
if (sig == SIGQUIT)
(void) exit(0);
sigcaught = sig;
#ifdef STATUS_ENAB
(void) signal(SIGINT, sigcatcher);
(void) signal(SIGQUIT, sigcatcher);
#endif
}
|