diff -dPNur gpppon_0.3.orig/gpppon.c gpppon_0.3.ds/gpppon.c --- gpppon_0.3.orig/gpppon.c 2009-02-16 03:05:57.000000000 +0100 +++ gpppon_0.3.ds/gpppon.c 2009-10-27 12:58:00.000000000 +0100 @@ -41,7 +41,7 @@ #define GPPPON "/usr/sbin/pppd" #endif -static gchar abouttxt[] = "gpppon version 0.2 Dec 30 1999\n\ +static gchar abouttxt[] = "gpppon version 0.3 Feb 27 2009\n\ Written by John Hasler based on\n\ gpppwrap version 0.4, 01 Nov. 1999 by Guido Socher\n\ (guido.s@writeme.com)\n\ @@ -67,6 +67,85 @@ /* this is because I am lazy */ static GtkWidget *mw_txt_label; +static int +read_param(char *buf, size_t size, FILE *f) { + int i=0, c; + c = fgetc(f); + while ((c != EOF)&&(c != 0)) + { + buf[i] = c; + if (++i == (size+1)) break; + c = fgetc(f); + } + buf[i] = 0; + + return i; +} + + +/* + *Squirrel away the name of the active provider and return it on demand +*/ +gchar * +provider (int task, gchar * name) +{ + static gchar provider[256]; + if (task == TRUE) + { + strncpy (provider, name, 255); + } + return (provider); +} + +static int +get_state() +{ + int i; + DIR *procs; + struct dirent *entry; + char cmdfile[32], cmdname[256]; + FILE *f; + size_t read; + + cmdname[13] = 0; + + procs = opendir ("/proc"); + if (!procs) return 0; + + while ((entry = readdir (procs))) + { + for (i = 0; entry->d_name[i]; i++) + if (!isdigit(entry->d_name[i])) break; + + if ((!i)||(i>15)||(entry->d_name[i])) continue; + sprintf(cmdfile, "/proc/%s/cmdline", entry->d_name); + f = fopen(cmdfile, "r"); + if (!f) continue; + + if ((fread(cmdname, 1, 15, f) == 15)&&(!strcmp(cmdname, "/usr/sbin/pppd"))) { + do { + read = read_param(cmdname, 256, f); + puts(cmdname); + if ((read == 4)&&(!strcmp(cmdname, "call"))) { + read = read_param(cmdname, 256, f); + if (read < 64) { + provider (TRUE, cmdname); + } + break; + } + } while (read > 0); + + fclose(f); + gppp_state = 1; + return gppp_state; + } + fclose(f); + } + + closedir (procs); + gppp_state = 2; + return gppp_state; +} static GList * get_provider_names () @@ -205,20 +284,6 @@ } /* - *Squirrel away the name of the active provider and return it on demand -*/ -gchar * -provider (int task, gchar * name) -{ - static gchar provider[256]; - if (task == TRUE) - { - strncpy (provider, name, 255); - } - return (provider); -} - -/* * the action associated with the ppp-off button */ static gint @@ -226,19 +291,31 @@ { /*check if we are already off */ gchar *txt, *name; + gchar message[256]; + int status; + if (gppp_state == 2) { gtk_label_set (GTK_LABEL (mw_txt_label), "Forced ppp-off"); } else { - gtk_label_set (GTK_LABEL (mw_txt_label), "State is: off "); + gtk_label_set (GTK_LABEL (mw_txt_label), "Off"); } - gppp_state = 2; /*set state to off */ + txt = gtk_entry_get_text (GTK_ENTRY (entry)); name = provider (FALSE, ""); - run_cmd (GPPPOFF, name, "", ""); + status = run_cmd (GPPPOFF, name, "", ""); + if (status) { + if (gppp_state != 2) { + sprintf(message, "On, status = %d", status); + gtk_label_set (GTK_LABEL (mw_txt_label), message); + } + } else { + gppp_state = 2; /*set state to off */ + } + return (TRUE); } @@ -266,7 +343,10 @@ gtk_label_set (GTK_LABEL (mw_txt_label), "Dialing "); status = run_cmd (GPPPON, "call", txt, "updetach"); - sprintf(message, "status = %d", status); + if (status) + sprintf(message, "Off, status = %d", status); + else + sprintf(message, "On"); gtk_label_set (GTK_LABEL (mw_txt_label), message); return (TRUE); } @@ -304,7 +384,7 @@ /* for some reason this does not work with my fvwm WM: */ /*gtk_widget_set_uposition(GTK_WIDGET(mainwindow),10,110); */ /* minimum size */ - gtk_widget_set_usize (GTK_WIDGET (mainwindow), 290, 78); + gtk_widget_set_usize (GTK_WIDGET (mainwindow), 320, 78); gtk_window_set_title (GTK_WINDOW (mainwindow), "gpppon"); /* handle delete_event, usually by the 'close' option */ gtk_signal_connect (GTK_OBJECT (mainwindow), "delete_event", @@ -376,11 +456,21 @@ /* the text in the main window */ - mw_txt_label = gtk_label_new ("ppp-on/off"); + switch(get_state()) { + case 1: + mw_txt_label = gtk_label_new ("On"); + break; + case 2: + mw_txt_label = gtk_label_new ("Off"); + break; + default: + mw_txt_label = gtk_label_new ("Unknown"); + } + labelbox = gtk_hbox_new (FALSE, 0); /* make the label big enough be putting a box arround it * the hight does not matter as it is minimum size values: */ - gtk_widget_set_usize (GTK_WIDGET (labelbox), 88, 10); + gtk_widget_set_usize (GTK_WIDGET (labelbox), 120, 10); /* pack: expand, fill, padd 4 */ gtk_box_pack_start (GTK_BOX (box2), labelbox, FALSE, FALSE, 4); /* put the label into the box: */