RE: just not getting it...



here is the source code i attached in the previous email:

/*
 * forkapp.c
 * compile with [gcc forkapp.c -o app `gnome-config --cflags --libs
gnomeui`]
 * Shane Clancy
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <gnome.h>
#include <sys/wait.h>
#include <linux/sys.h>

GtkWidget* create_forkapp (void);

void
on_execute_clicked                     (GtkButton       *button,
                                        gpointer         user_data);

void
on_bye_clicked                        (GtkButton       *button,
                                        gpointer         user_data);

void
catchall							   (int
signum);

  GtkWidget *forkapp;
  GtkWidget *dock1;
  GtkWidget *vpaned1;
  GtkWidget *hbox1;
  GtkWidget *execute;
  GtkWidget *bye;
  GtkWidget *vpaned2;
  GtkWidget *frame1;
  GtkWidget *entry1;
  GtkWidget *vpaned3;
  GtkWidget *frame2;
  GtkWidget *entry2;
  GtkWidget *vpaned4;
  GtkWidget *frame3;
  GtkWidget *entry3;
  GtkWidget *scrolledwindow1;
  GtkWidget *text1;
  GtkWidget *appbar1;
  
  int	kidpid[2];
  int	kidsdone = 0;
  int    dad;
  int	c;
  FILE  *fp;
  FILE  *xfer;
  FILE  *last;
  char  *path;
  char   crossover[255];
  char   filename[255];
  char   laststr[255];
  char   spacer[] = {"\n-------\n"};
  char   buffer[255];

int
main (int argc, char *argv[])
{
  GtkWidget *forkapp;

#ifdef ENABLE_NLS
  bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
  textdomain (PACKAGE);
#endif

  gnome_init ("forkapp", "1.0", argc, argv);

  forkapp = create_forkapp ();
  gtk_widget_show (forkapp);

  gtk_main ();
  return 0;
}

GtkWidget*
create_forkapp (void)
{
  forkapp = gnome_app_new ("Forkapp", _("forkapp"));
  gtk_object_set_data (GTK_OBJECT (forkapp), "forkapp", forkapp);
  gtk_window_set_default_size (GTK_WINDOW (forkapp), 350, 350);

  dock1 = GNOME_APP (forkapp)->dock;
  gtk_widget_ref (dock1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "dock1", dock1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (dock1);

  vpaned1 = gtk_vpaned_new ();
  gtk_widget_ref (vpaned1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "vpaned1", vpaned1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vpaned1);
  gnome_app_set_contents (GNOME_APP (forkapp), vpaned1);

  hbox1 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "hbox1", hbox1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox1);
  gtk_paned_pack1 (GTK_PANED (vpaned1), hbox1, FALSE, TRUE);

  execute = gnome_stock_button (GNOME_STOCK_BUTTON_APPLY);
  gtk_widget_ref (execute);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "execute", execute,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (execute);
  gtk_box_pack_start (GTK_BOX (hbox1), execute, FALSE, FALSE, 0);

  bye = gnome_stock_button (GNOME_STOCK_BUTTON_CLOSE);
  gtk_widget_ref (bye);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "bye", bye,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (bye);
  gtk_box_pack_start (GTK_BOX (hbox1), bye, FALSE, FALSE, 0);

  vpaned2 = gtk_vpaned_new ();
  gtk_widget_ref (vpaned2);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "vpaned2", vpaned2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vpaned2);
  gtk_paned_pack2 (GTK_PANED (vpaned1), vpaned2, TRUE, TRUE);

  frame1 = gtk_frame_new (_("Path to working directory"));
  gtk_widget_ref (frame1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "frame1", frame1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (frame1);
  gtk_paned_pack1 (GTK_PANED (vpaned2), frame1, FALSE, TRUE);

  entry1 = gtk_entry_new ();
  gtk_widget_ref (entry1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "entry1", entry1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (entry1);
  gtk_container_add (GTK_CONTAINER (frame1), entry1);

  vpaned3 = gtk_vpaned_new ();
  gtk_widget_ref (vpaned3);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "vpaned3", vpaned3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vpaned3);
  gtk_paned_pack2 (GTK_PANED (vpaned2), vpaned3, TRUE, TRUE);

  frame2 = gtk_frame_new (_("Content for first file"));
  gtk_widget_ref (frame2);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "frame2", frame2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (frame2);
  gtk_paned_pack1 (GTK_PANED (vpaned3), frame2, FALSE, TRUE);

  entry2 = gtk_entry_new ();
  gtk_widget_ref (entry2);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "entry2", entry2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (entry2);
  gtk_container_add (GTK_CONTAINER (frame2), entry2);

  vpaned4 = gtk_vpaned_new ();
  gtk_widget_ref (vpaned4);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "vpaned4", vpaned4,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vpaned4);
  gtk_paned_pack2 (GTK_PANED (vpaned3), vpaned4, TRUE, TRUE);

  frame3 = gtk_frame_new (_("Content for second file"));
  gtk_widget_ref (frame3);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "frame3", frame3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (frame3);
  gtk_paned_pack1 (GTK_PANED (vpaned4), frame3, FALSE, TRUE);

  entry3 = gtk_entry_new ();
  gtk_widget_ref (entry3);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "entry3", entry3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (entry3);
  gtk_container_add (GTK_CONTAINER (frame3), entry3);

  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_ref (scrolledwindow1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "scrolledwindow1",
scrolledwindow1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (scrolledwindow1);
  gtk_paned_pack2 (GTK_PANED (vpaned4), scrolledwindow1, TRUE, TRUE);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1),
GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

  text1 = gtk_text_new (NULL, NULL);
  gtk_widget_ref (text1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "text1", text1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (text1);
  gtk_container_add (GTK_CONTAINER (scrolledwindow1), text1);
  gtk_text_set_editable (GTK_TEXT (text1), TRUE);

  appbar1 = gnome_appbar_new (TRUE, TRUE, GNOME_PREFERENCES_NEVER);
  gtk_widget_ref (appbar1);
  gtk_object_set_data_full (GTK_OBJECT (forkapp), "appbar1", appbar1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (appbar1);
  gnome_app_set_statusbar (GNOME_APP (forkapp), appbar1);

  gtk_signal_connect (GTK_OBJECT (execute), "clicked",
                      GTK_SIGNAL_FUNC (on_execute_clicked),
                      NULL);
  gtk_signal_connect (GTK_OBJECT (bye), "clicked",
                      GTK_SIGNAL_FUNC (on_bye_clicked),
                      NULL);

  return forkapp;
}


void
on_execute_clicked                     (GtkButton       *button,
                                        gpointer         user_data)
{
	path = gtk_entry_get_text((GtkEntry *) entry1);
	
	signal (SIGCHLD, (void *) catchall);
	
	printf("here's the path we're using [%s].\n", path);
	
	if ((kidpid[0] = fork()) == 0)
		{
			//char  data[] = {"data for first file.\n"};
			char	*data;
			
			data = gtk_entry_get_text((GtkEntry *) entry2);
			printf("the first child is off and running.\n");
			sprintf(filename, "%sfile%d", path, 0);
			if ((fp = (fopen(filename, "w"))) == NULL)
				{
					printf("hmm... couldn't write the
data file for child %d.\n",1);
					printf("here's where we tried to
write to [%s].\n", filename);
					exit (0);
				}
			fputs (data, fp);
			fclose(fp);
			printf("child %d is done.\n", 1);
			exit(0);
		}
	if ((kidpid[1] = fork()) == 0)
		{
			//char  data[] = {"data for second file.\n"};
			char *data;
			
			data = gtk_entry_get_text((GtkEntry *) entry3);
			sprintf(filename, "%sfile%d", path, 1);
			if ((fp = (fopen(filename, "w"))) == NULL)
				{
					printf("hmm... couldn't write the
data file for child %d.\n",2);
					printf("here's where we tried to
write to [%s].\n", filename);
					exit (0);
				}
			fputs (data, fp);
			fclose(fp);
			printf("child %d is done.\n", 2);
			exit(0);
		}
	while (kidsdone == 0)
		{
			sleep (2);
		}
	printf("past the while loop, about to look at the kids' data
files.\n");
	for (dad = 0; dad < 2; dad++)
		{
			if (kidpid[dad] == 1)
				{
					sprintf(crossover, "%sfile%d", path,
dad);
					printf("currently looking at %s.\n",
crossover);
					sprintf(filename, "%sfinal", path);
					xfer = fopen(crossover, "r");
					if (dad == 0)
						{
							fp = fopen(filename,
"w");
						}
					else
						{
							fp = fopen(filename,
"a");
						}
					while (1)
						{
							c = fgetc(xfer);
							if (!feof (xfer))
								{
	
fputc(c, fp);
								}
							else
								{
	
fputs(spacer, fp);
	
break;
								}
						}
						fclose(fp);
						fclose(xfer);
				}
		}
	printf("data from the kids' files has been assembled here [%s].\n",
filename);
	sprintf(laststr, "%sfinal", path);
	printf("about to look for data here[%s].\n", laststr);
	if((last = fopen(laststr, "r")) == NULL)
		{
			printf("shane you're an idiot. set up the pipe
correctly.\n");
			exit(0);
		}
	else
		{
			printf("the file appears to have opened
correctly.\n");
		}
	while(1)
		{
			if ((fgets(buffer, 255, last)) != NULL)
				{
					printf("i should be entering text
into the widget now...\n");
					printf("here's the buffer this time
around [%s].\n");
					gtk_text_insert(GTK_TEXT(text1),
NULL, NULL, NULL, buffer, -1);
					printf("made it past the text insert
statement.\n");
				}
			else
				{
					printf("the buffer is empty.\n");
					break;
				}
		}
	fclose(last);
}


void
on_bye_clicked                        (GtkButton       *button,
                                        gpointer         user_data)
{
	gtk_main_quit();
}

void
catchall (int signum)
{
	int parentpid;
	int i;
	int x;
	int done = 1;
	
	while ((parentpid = waitpid(-1, NULL, WNOHANG)) > 0) /* catch
signals from children as they come in */
		{
			printf("child process %d just exited.\n",
parentpid);
			for(i = 0; i < 2; i++)
				{
					if (kidpid[i] == parentpid)
						{
							kidpid[i] = 1; /*
reset the exiting pids to a constant value -- in this case, one. */
						}
				}
			for(x = 0; x < 2; x++)
				{
					if (kidpid[x] != 1) /* have all pids
exited? if not, reset signal handler. */
						{
							done = 0;
							break;
						}
				}
		}
	if (done ==1)
		{
			kidsdone = 1;
			printf("the children have all exited.\n");
		}
	else
		{
			signal (SIGCHLD, (void *) catchall);
		}
	return;
}
	



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