Re: more pipe questions



On 2001.02.15 05:29:41 +0100 Helmethead wrote:
> On Thu, Feb 15, 2001 at 02:53:02AM +0100, Ronald Bultje wrote:
> > I remember this question being asked before, but deleted it, I think...
> > 
> > If I write data to a file (continuously, a logfile kinda thing), how
> can I
> > check this file for "being updated"? I now use a read-pipe and
> popen("cat
> > file", "r"); but there's probably a better way.
> > 
> > Or is it maybe possible to open this logfile "continuously" so that any
> > data written to it can be caught at once?
> > 
> > Besides that, if I put this popen("cat file", "r"); in a while-loop, my
> > tasklist shows a [cat <defunc>] and the program stops there...... Any
> clue
> > why?
> > 
> 
> Why it's getting zombies (defunct processes) would be because you are not
> calling pclose() on each file handle. And if you want to use popen to do
> this, call tail -0f not cat. any updates to the file will be immediately
> printed with that command.
> 
> eg.
> 
> FILE *fp;
> if (!(fp = popen ("tail -0f filename", "r")))
> {
>     perror ("popen() failed");
>     exit (1);
> }
> printf ("Watching filename for new data\n\n");
> for (;;) /* infinite loop */
> {
>     char buff[80];
>     if (fgets (buff, 79, fp) == NULL)
>     {
> 	if (feof (fp))
> 	    fprintf (stderr, "tail closed\n");
> 	if (ferror (fp))
> 	    perror ("fgets() failed");
> 	exit (1);
>     }
>     printf ("line appended to file filename: %s\n", buff);
> }
> 
> yeah, gtk-ize all that.
> 
> > Ronald
> > 
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
> 
> 

My Gtk-ized code:

void lavreclog_input(gpointer data, gint source, GdkInputCondition
condition)
{
	char *p;
	char buf[129];
	int len, numLines=0;

	while ((p = fgets( buf, 128, studio_lavrec_log)) != NULL)
	{
		len = strlen(buf);
		if (len > 0)
			buf[len-1] = '\0';
		g_print("LAVREC: %s\n", buf);
		numLines++;
		return;
	}

	if ( numLines == 0 )
	{
		g_print("Lavreclog completed\n");
		pclose(studio_lavrec_log);
		studio_lavrec_log = NULL;
		gdk_input_remove(studio_lavreclog_gint);
		studio_lavreclog_int = -1;
		studio_lavreclog_gint = 0;
	}
}

void read_lav_log()
{
	char cmd[100];

	sprintf(cmd,"tail -0f %s/%s/%s",getenv("HOME"),".studio",
"lavrec.log");

	if (studio_lavrec_log != NULL)
	{
		pclose(studio_lavrec_log);
		studio_lavrec_pipe = NULL;
	}
	studio_lavrec_log = popen(cmd, "r");
	if (studio_lavrec_log == NULL)
	{
		g_print("Null pipe data - exiting...\n");
		pclose(studio_lavrec_log);
		return;
	}
	studio_lavreclog_int = fileno(studio_lavrec_log);
	studio_lavreclog_gint = gdk_input_add(studio_lavreclog_int,
		GDK_INPUT_READ, lavreclog_input," " );
}

But I get no input anymore in the function lavreclog_input()...I guess I
need to set it nonblocking/flushing. How do I do that? Or is it something
else? I thought fcntl, but that didn't work, I think - still no input...

Ronald

-- 
---------------------------------------------------.
--   .-.    | Ronald Bultje                        |
--   /V\    | Running: Linux 2.4.1 and OpenBSD 2.8 |
--  // \\   | E-mail : rbultje ronald bitfreak net |
-- /(   )\  | WWW    : http://ronald.bitfreak.net/ |
--  ^^-^^   |    *** Warning: Unix Addicted ***    |
---------------------------------------------------'





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]