1 /*
2  *   retail - a logfile monitoring utility
3  *
4  *   Copyright (C) Y2K (2000) A.L.Lambert
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2, or (at your option)
9  *   any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 /* Yee ole includes (I put this all in one file for my sanity) */
22 #include "include.h"
23 
24 FILE_STAT f;
25 
read_infile_llcheck()26 int read_infile_llcheck() {
27 	long int i;
28 	short int found = 0;
29 	char line[B_SIZE];
30 
31 	/* set position to last known good - 1 */
32 	for(i = 2 ; i < B_SIZE ; ++i) {
33 		/* rewind exactly one line */
34 		fseek(in_file, (f.ipos - i), SEEK_SET);
35 		if(getc(in_file) == '\n') {
36 			break; /* found our first \n, we're home */
37 		}
38 	}
39 
40 	/* get that line we just rewound to */
41 	fgets(line, sizeof(line), in_file);
42 	/* make sure the last line in the file is same as it was last time */
43 	if(strcmp(line, f.last_line) != 0) {
44 		found = 0;
45 		/* oops, file has changed; rewind and search for the last
46 		 * known line of the file in ASCII. */
47 		fseek(in_file, 0, SEEK_SET);
48 		while(fgets(line, sizeof(line), in_file) != NULL) {
49 			/* ahh... we found it, set postion here, and continue */
50 			if(strcmp(line, f.last_line) == 0) {
51 				if(debug) fprintf(stderr, "file changed: new position found: resetting\n");
52 				f.ipos = ftell(in_file);
53 				found = 1;
54 				break;
55 			}
56 		}
57 		if(found != 1) {
58 			if(debug) fprintf(stderr, "file changed: resetting position to 0\n");
59 			f.ipos = 0;
60 		}
61 	}
62 	return retval;
63 }
64 
read_infile()65 int read_infile() {
66 	char line[B_SIZE];  /* where we stick our line */
67 	short int new_stuff = 0;	/* anything new found?	*/
68 	/* double check to see the file's not shorter than it used to be */
69 	fseek(in_file, 0, SEEK_END);
70 	if(ftell(in_file) < f.ipos) {
71 		/* reset that to 0 for where to start */
72 		f.ipos = 0;
73 	}
74 
75 	/* check the last line against the know value */
76 	read_infile_llcheck();
77 
78 	/* move to the last known position in the file */
79 	fseek(in_file, f.ipos, SEEK_SET);
80 	/* loop through the file for any new lines since last we looked */
81 	while(fgets(line, sizeof(line), in_file) != NULL) {
82 		new_stuff = 1;
83 		/* print the line as-is; no editing whatsoever */
84 		printf("%s", line); /* no \n at the end; should already be there */
85 	}
86 	if(new_stuff == 0) return retval;
87 
88 	/* set the int position of the file to the current file position */
89 	f.ipos = ftell(in_file);
90 	/* free the last_line variable if need be */
91 	if(f.last_line != NULL) free(f.last_line);
92 	/* malloc it to the appropriate size */
93 	f.last_line = malloc(strlen(line) + 1);
94 	/* strcpy the new value into place */
95 	strcpy(f.last_line, line);
96 	/* all set, now get out of here */
97 	return retval;
98 }
99 /* read our stats file.  Call this BEFORE read_infile()!!! you have been WARNED! */
read_stfile()100 int read_stfile() {
101 	char *line;
102 	long int f_size;
103 	short int fc;
104 
105 	/* find  out how much we need to malloc for line */
106 	fseek(in_stat, 0, SEEK_END);	/* jump to end of file		*/
107 	f_size = ftell(in_stat);	/* how many bytes in file?	*/
108 	fseek(in_stat, 0, SEEK_SET);	/* rewind to beginning of file	*/
109 	line = malloc(f_size);		/* malloc line to stuff this in */
110 	f.last_line = malloc(f_size);	/* malloc same for last line	*/
111 	if(line == NULL || f.last_line == NULL) { /* error check the malloc 	*/
112 		retval = RV_FATAL;
113 		perror("malloc");
114 		exit(retval);
115 	}
116 
117 	/* rip the first line from the file in question */
118 	fgets(line, f_size, in_stat);
119 	/* reset position to beginning of file */
120 	fseek(in_stat, 0, SEEK_SET);
121 	fc = sscanf(line, "%[^:]:%[^\n]", f.apos, f.last_line);
122 	if(fc != 2) {
123 		retval = RV_OPEN;
124 		return retval;
125 	}
126 	/* can't strcmp later if we don't do this (yuck!) */
127 	strcat(f.last_line, "\n");
128 	/* convert ascii position to integer position */
129 	f.ipos = atoi(f.apos);
130 	if(debug) fprintf(stderr, "in_stat:%li:%s\n", f.ipos, f.last_line);
131 	return retval;
132 }
133