예제 #1
0
파일: utmpdump.c 프로젝트: Kynde/util-linux
static FILE *dump(FILE *in, const char *filename, int follow, FILE *out)
{
	struct utmp ut;

	if (follow)
		ignore_result( fseek(in, -10 * sizeof(ut), SEEK_END) );

	while (fread(&ut, sizeof(ut), 1, in) == 1)
		print_utline(ut, out);

	if (!follow)
		return in;

#ifdef HAVE_INOTIFY_INIT
	if (follow_by_inotify(in, filename, out) == 0)
		return NULL;				/* file already closed */
	else
#endif
		/* fallback for systems without inotify or with non-free
		 * inotify instances */
		for (;;) {
			while (fread(&ut, sizeof(ut), 1, in) == 1)
				print_utline(ut, out);
			sleep(1);
		}

	return in;
}
예제 #2
0
파일: utmpdump.c 프로젝트: Kynde/util-linux
static void roll_file(const char *filename, off_t *size, FILE *out)
{
	FILE *in;
	struct stat st;
	struct utmp ut;
	off_t pos;

	if (!(in = fopen(filename, "r")))
		err(EXIT_FAILURE, _("cannot open %s"), filename);

	if (fstat(fileno(in), &st) == -1)
		err(EXIT_FAILURE, _("%s: stat failed"), filename);

	if (st.st_size == *size)
		goto done;

	if (fseek(in, *size, SEEK_SET) != (off_t) -1) {
		while (fread(&ut, sizeof(ut), 1, in) == 1)
			print_utline(ut, out);
	}

	pos = ftello(in);
	/* If we've successfully read something, use the file position, this
	 * avoids data duplication.  If we read nothing or hit an error,
	 * reset to the reported size, this handles truncated files.
	 */
	*size = (pos != -1 && pos != *size) ? pos : st.st_size;

done:
	fclose(in);
}
예제 #3
0
void
dump(FILE *fp, int forever, int oldfmt)
{
	struct utmp ut;
	struct oldutmp uto;

	if (forever)
		fseek(fp, -10 * (oldfmt ? sizeof uto : sizeof ut), SEEK_END);

	do {
		if (oldfmt)
			while (fread(&uto, sizeof uto, 1, fp) == 1)
				print_utline(oldtonew(uto));
		else
			while (fread(&ut, sizeof ut, 1, fp) == 1)
				print_utline(ut);
		if (forever) sleep(1);
	} while (forever);
}