[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

problemer med gammel kode.



hejsa
jeg sag od skulle bruge noget gammel kode jeg tidligere har rodet med. 
Desværre er jeg stødt på en del problemer:
koden består af 2 filer (header og main) og ser således ud:
HEADER FIL
............................
#define _LOGFILE_H


#include <stdio.h>
#include <sys/stat.h>

typedef struct {
	char *name;           
	struct stat info;     
	off_t offset;         
	FILE *fp;             
	int moins_f;          
} GLogFile;

GLogFile *log_file_new(char *logfile) ;
void log_file_free(GLogFile * self) ;
int log_file_tail(GLogFile * log, char **buf) ;

-------------------------------------------------

MAIN FIL
................................................

#include "tailf.h"
#include <unistd.h>
#include <glib.h>

#ifdef _DEBUG_
#define begin_fun g_print("<%s>\n",__PRETTY_FUNCTION__) 
#define end_fun   g_print("<%s> Ok\n",__PRETTY_FUNCTION__) 
#else
#define begin_fun 
#define end_fun 
#endif

static int 
_log_file_tail(GLogFile *self,int nb_line,char *buf,int len) ;


GLogFile *log_file_new(char *logfile)
{
	GLogFile *self;
	int res;

	begin_fun ;

	g_return_val_if_fail(logfile != NULL, NULL);

	self = g_new0(GLogFile, 1);

	self->name = g_strdup(logfile);
	self->fp = fopen(logfile, "r");

	if (!self->fp) {
		g_warning("impossible d'ouvrir %s", logfile);
		g_free(self->name);
		g_free(self);
		return NULL;
	}

	res = fstat(fileno(self->fp), &self->info);

	if (res) {
		g_warning("fejl ved brug af %s", logfile);
		fclose(self->fp);
		g_free(self->name);
		g_free(self);
		return NULL;
	}

	end_fun ;

	return self ;
}

void log_file_free(GLogFile * self)
{
	g_return_if_fail(self != NULL);

	fclose(self->fp);
	g_free(self->name);
	g_free(self);
}

	static int 
_log_file_tail(GLogFile *self,int nb_line,char *buf,int len) 
{
	long position ;
	char *tmp ;
	char *ptr, *last_n ;

	int n ;
	begin_fun ;

	g_return_val_if_fail(self != NULL,1) ;
	g_return_val_if_fail(buf != NULL,1) ;

	if(len == 0 || nb_line == 0)
		return 0 ;

	tmp = g_new(char,len+1) ;

	position = self->info.st_size - len ;

	if(position<0) position = 0 ;

	fseek(self->fp,position,SEEK_SET) ;

	n = fread(tmp,1,len,self->fp) ;
	tmp[n] = '\0' ;


	for(ptr = tmp+n,last_n = tmp+n-1 ; ptr != tmp && nb_line ; ptr--)
		if(*ptr == '\n') {
			last_n = ptr+1 ; 
			nb_line-- ;
		}

	strcpy(buf,last_n) ;

	self->offset = self->info.st_size ;
	self->moins_f = 1 ;

	g_free(tmp) ;   

	end_fun ;

	return 0 ;
}

int log_file_tail(GLogFile * log, char **buf)
{
	int res_name, res_fp,n,ret ;
	off_t dif ; 
	struct stat statfp ;
	struct stat statname ;

	begin_fun ;

	g_return_val_if_fail(log != NULL, 1);
	g_return_val_if_fail(buf != NULL, 1);


	res_fp = fstat(fileno(log->fp),&statfp) ;
	res_name = stat(log->name,&statname) ;


	if ((res_fp == 0 && statfp.st_nlink == 0)
			|| (res_name == 0
				&& log->info.st_ino != statname.st_ino)) {
		fclose(log->fp) ;



		if ((log->fp = fopen(log->name, "r")) == NULL) {
			perror(log->name) ;
			return 1 ;
		}
		fstat(fileno(log->fp), &log->info);
		statfp = log->info ;
		log->offset = 0 ;
		log->moins_f = 0 ;
	}


	if(log->moins_f == 0) { 
		char tmp[20*128] ;

		ret = _log_file_tail(log,20,tmp,20*128) ;
		if(!ret)
			*buf = g_strdup(tmp) ;
		else
			*buf = NULL ;
		return ret ;
	}


	dif = statfp.st_size - log->offset ;
	log->moins_f = 1 ;


	if(dif>0) {

		*buf = g_new(char,dif+1) ;

		fseek(log->fp,log->offset,SEEK_SET) ;
		n=fread(*buf,1,dif,log->fp) ;

		log->offset+=n ;
		(*buf)[n] = '\0' ;

		while(n>=0 && (*buf)[n] != '\n') 
			n-- ;           

		(*buf)[n+1] = '\0' ; 

	} else if(dif == 0) {

		*buf = NULL ;
	} else { /* Erreur dif < 0 */

		log->offset = 0 ;
		return 1 ;
	}

	end_fun ;

	return 0;

}
#ifdef never
int main() 
{
	GLogFile *log ;
	char *buf=NULL ;

	int res ;

	log = log_file_new("/tmp/toto") ;
	while(1) {

		res = log_file_tail(log,&buf) ;
		if(buf) {
			g_print("%s",buf) ;
			g_free(buf) ;
		}

		sleep(2) ;

	}

	log_file_free(log) ;
	return 0 ;

}
#endif
-----------------------------------------------


Den kommer med et hav af fejl når jeg kompiler

sslug@sslug kim]$ gcc -o tailf kode-til-f-tail.c `gtk-config --libs
--cflags`
kode-til-f-tail.c:41: parse error before `off_t'
kode-til-f-tail.c:41: warning: no semicolon at end of struct or union
kode-til-f-tail.c:44: parse error before `}'
kode-til-f-tail.c:44: warning: data definition has no type or storage
class
kode-til-f-tail.c:46: parse error before `*'
kode-til-f-tail.c:46: warning: data definition has no type or storage
class
kode-til-f-tail.c:47: parse error before `*'
kode-til-f-tail.c:48: parse error before `*'
kode-til-f-tail.c:96: parse error before `*'
kode-til-f-tail.c:99: parse error before `*'
kode-til-f-tail.c: In function `log_file_new':
kode-til-f-tail.c:101: `self' undeclared (first use in this function)
kode-til-f-tail.c:101: (Each undeclared identifier is reported only once
kode-til-f-tail.c:101: for each function it appears in.)
kode-til-f-tail.c:102: parse error before `int'
kode-til-f-tail.c:108: parse error before `)'
kode-til-f-tail.c:120: `res' undeclared (first use in this function)
kode-til-f-tail.c: At top level:
kode-til-f-tail.c:135: parse error before `*'
kode-til-f-tail.c: In function `log_file_free':

osv....


Nogen der kan gennemskue hvad der er galt? programmet er en form for
tail -f 


-- 
Kim Schulz - Freelance Development    | Q: How many Harvard MBA's does
www.schulz.dk  - En nørds bekendelser | it take to screw in a
www.linuxia.dk - hverdagens små hacks | lightbulb? A: Just one.  He


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *